My code compiles but bricks any Electron


#1

When I upload the code below any electron i have goes red flashing. It should NOT because its on manual mode.

// For testing code
SYSTEM_MODE(MANUAL);

//Sensor Code
/*
  #if defined(PARTICLE)
  SYSTEM_THREAD(ENABLED)
  #endif
*/

#include "Adafruit_CCS811.h"

Adafruit_CCS811 ccs;

void setup() {
  Serial.begin(9600);
  // These two control the LED
  pinMode(A3, OUTPUT);
  pinMode(A1, OUTPUT);
  // These two complete a pullup resistor
  pinMode(B5, INPUT_PULLUP);
  pinMode(B3, OUTPUT);
  //Part of the pullup network
  digitalWrite(B3, LOW);

  pinMode(D2, OUTPUT);
  digitalWrite(D2, LOW);


  float temp = ccs.calculateTemperature();
  ccs.setTempOffset(temp - 25.0);
}

void loop() {

  // Code for rocker switch to shutoff
  if (analogRead(A7) < 100 ) {
    System.sleep(SLEEP_MODE_SOFTPOWEROFF);
  }

  // Seeming non-invertered logic

  if (digitalRead(B5)) {
    buttonOn();
  }   else {
    buttonOff();
  }

  if (ccs.available()) {
    float temp = ccs.calculateTemperature();
    if (!ccs.readData()) {
      Serial.print("CO2: ");
      Serial.print(ccs.geteCO2());
      Serial.print("ppm, TVOC: ");
      Serial.print(ccs.getTVOC());
      Serial.print("ppb   Temp:");
      Serial.println(temp);
    }
    else {
      Serial.println("ERROR!");
      while (1);
    }
  }
  delay(500);
}



void buttonOn() {
  digitalWrite(A3, LOW);
  digitalWrite(A1, HIGH);
}

void buttonOff() {
  digitalWrite(A3, LOW);
  digitalWrite(A1, LOW);
}

#2

Hi @ddres0605

It doesn’t look like you ever call ccs.Begin(); in your code, so perhaps the library is causing an error?

System mode manual doesn’t mean that errors can’t cause a SOS red flashing LED pattern. There can still be code errors that cause bad things to happen.


#3

Does calling the begin function inside a conditional count as calling it normally?


#4

Hi @ddres0605

There is no call to ccs.Begin() in the code you posted. I actually cut and pasted it into an editor to be sure.

If you look at the Adafruit example code, it does this:

 if(!ccs.begin()){
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }

which is what I’m sure you think you are doing, but it is not the code you posted.