Rapid flashing cyan and red and no ability to reflash

I’ve been playing around with the spark core quite a bit and with the program I’m running, I’ve run into 2 different issues:

  1. When trying to re-flash after changing code, my device won’t recognize that I am trying to flash to it. The program keeps on running and the LED never goes magenta. My only way to re-flash is to completely reset wifi settings or factory reset and reconnect.

  2. My program also stops running correctly about a minute or two in and starts rapidly flashing cyan then quickly flashes red then goes back to cyan and the cycle repeats endlessly.

I tried looking through other posts to find solutions but nothing I read or tried has worked for me. Adding or taking away delay time to help with re-flashing did not work either.

Here is the code for reference:

int anlgSensor = A2;    // analog input pin for the gas sensor
int anlgValue;          // variable to store the value coming from the sensor
int alertPin = A3;      // optional audible alert
int heater = A0;
int digSensor = A1;     //digital sensor pin
int digAlert;           //stores sensor value
char* message;
int ppm;
int led = D7;

void setup() {
 
 /* message = "Warming";
  Spark.variable("sensorValue", &anlgValue, INT);
  Spark.variable("ppm", &ppm, INT);
  Spark.variable("mess", message, STRING);
  Spark.variable("sensorAlert", &digAlert, INT);
 */
  
  pinMode(heater, OUTPUT);
  pinMode(digAlert, INPUT);
  pinMode(digSensor,INPUT);	          // set sensor pin as INPUT
  digitalWrite(alertPin,LOW);
  digitalWrite(heater, HIGH);
  
  pinMode(led, OUTPUT);                   // turn the heater on
  digitalWrite(led, HIGH);              
  delay(500);
  digitalWrite(led,LOW);
}

void loop() {
  /* digsensorAlert=(digitalRead(digSensor));
  if(digAlert > 0){
    message = "ALARM";
  }
  else{
    message = "Safe";
  } 
  */
      
  digitalWrite(led, HIGH);
  anlgValue= analogRead(anlgSensor);       // analogRead() returns 12 bit (0-4095) value max is 3.3V
  float voltage = anlgValue * 3.3 / 4095;  // voltage = sensorValue x (3.3/4095)
  ppm = (voltage - 0.5) * 100; 	
  if (ppm > 100) {                         //if V>1.5
    analogWrite(alertPin,ppm);
  }
  else analogWrite(alertPin,0);
 
  Spark.publish("ppm",String(ppm));
  delay(500);                                    
  digitalWrite(led, LOW);                  // Spark.publish interrupts OTA connection
  delay(500);
}

ScruffR: Reformatted your code block this way

 ```cpp
 code block here
if (ppm > 100) {                         //if V > 1.5
    analogWrite(alertPin,ppm);
  }

I doubt that this is your issue, but analogWrite() does only accept values in the range of 0…255, but your ppm could exceed this range.


Since I’m a bit superstitious and avoid the use of String wherever possible, could you try this instead

void loop()
{
  char szPPM[16];
  ...
  sprintf(szPPM, "%d", ppm);
  Spark.publish("ppm", szPPM);
  //Spark.publish("ppm", String(ppm).c_str());  // alternitive way if you desperately want String ;-)
  ...
}

Yes and I think there is a function which can limit the value to within that range correct? Either way my values are all within range, so that isn’t causing the problem.

Have you seen my addition about the use of String?

I’ve seen several cases, where avoiding it did cure some odd behaviour.

I tried the different string format but that didn’t work. Not sure exactly how to implement the other code you showed there.

Also while re-flashing I will get the message Error: 500.

What exactly didn’t work?
Did it have just the same issue or did something else go wrong?

I guess you are refering to this

  Spark.publish("ppm", String(ppm).c_str());

Just use this instead of the respective line in your original code.


The HTTP 500 might just have been a glitch that should be gone already - just retry.
If it was not an HTTP error, could you post a screenshot or a copy

Yes I just replaced that line of code with
Spark.publish("ppm", String(ppm).c_str());
The same problems were occurring. I couldn’t reflash and the stream of published data stopped about a minute in and the blinking cyan started up.

That’s odd then! And the sprintf() aproach, did this do the same thing, of did it cause some other issue?

One more thing about the publishing, there is a rate limit of one publish per second. If it should happen that your two delay(500) are just a bit fast, you might hit that rate limit.
To be on the save side, increase this value a bit (e.g. 2x 750).

This shows the error I got.

I also attempted increasing delay time but that didn’t cause any change.

I also tried the sprintf approach but I wasn’t quite sure how to do it. I got some compiler errors trying to do it.

I have now flashed your code to one of my Cores and can confirm your issue.

So I had a closer look at the things that I just assumed correct and noticed some problems with your pin mapping.
e.g you have not assigned a value to digAlert but you do pinMode(digAlert, INPUT);

I’ll have to have a closer look and try to get your code running on my Core.

Ok thanks for the help with this.

I believe when changing names of pins I got that one wrong so it should be pinmode(anlgSensor, INPUT);

I’m not exactly sure why but for the first time, my core has gone about 10 minutes now without losing connection at all. The only change I made was setting the delay times in the loop to 700. This is strange because it didn’t work the first or second time with that same amount of delay.

OK, now I’ve got it working just fine - even OTA works reliably again.

There were several (possible) problems.
Apart from the non critical ones like value range and pin assignment, the main problem was the ill-fated combination of delay() and Spark.publish().
So I have removed your delays and replaced it with a “soft delay” that allows the cloud-keeping to occure more often and only runs your code once per second. This way OTA gets more chance against Spark.publish() to get serviced.
I haven’t quite got to the bottom of the red flashing, but it could have to do with the fact that the cloud-keeping gets done rather infrequently and hence building a backlog of unfinished publish() requests might result in an overrun.

But this code runs now for about 20min without any issue. It also demonstrates the use of the non-String approach.


#define RETRYTIME 1000

// OUTPUTs
const int led        = D7;
const int heater     = A0;
const int alertPin   = A3;    // optional audible alert

// INPUTs
const int digSensor  = A1;    // digital sensor pin
const int anlgSensor = A2;    // analog input pin for the gas sensor


int anlgValue;                // variable to store the value coming from the sensor
int digAlert;                 // stores sensor value
int ppm;
char szPPM[8];

unsigned long ms;

void setup() 
{
  pinMode(led       , OUTPUT);                  
  pinMode(heater    , OUTPUT);
  pinMode(alertPin  , OUTPUT);
  
  pinMode(digSensor , INPUT);   // set sensor pin as INPUT
  pinMode(anlgSensor, INPUT);
  
  
  digitalWrite(alertPin, LOW);
  digitalWrite(heater  , HIGH);
  
  digitalWrite(led     , HIGH);              
  delay(500);
  digitalWrite(led     , LOW);
  
  ms = millis();
}

void loop() 
{
  if (millis() - ms > RETRYTIME)
  {
      digitalWrite(led, !digitalRead(led));      // toggle the LED
    
      anlgValue = analogRead(anlgSensor);        // analogRead() returns 12 bit (0-4095) value max is 3.3V
      
      float voltage = anlgValue * 3.3 / 4095.0;  // voltage = sensorValue x (3.3/4095)
      ppm = constrain((voltage - 0.5) * 100, 0, 255); 	
    
      if (ppm > 100)                             // if V>1.5
        analogWrite(alertPin, ppm);
      else 
        analogWrite(alertPin, 0);
     
      sprintf(szPPM, "%d", ppm);
      Spark.publish("ppm", szPPM);
      //Spark.publish("ppm", String(ppm).c_str());
      //Spark.publish("ppm", String(ppm);

      ms = millis();
  }
}

That’s working for me as well! The rest of what I’m adding to the program shouldn’t change any of the functionality for this chunk either so I should be ok now. Thanks for bearing with me.

1 Like