[Solved] Spark flashes but won't breathe for my app - light goes off instead

My spark was working well before tonight. I made a few tweaks to surrounding hardware and after turning it back on, it booted up then shut off. I disconnected the spark from all other hardware and it still is having the same issue. It will turn on and breathe just fine with other simple apps, so I’m guessing there is code in mine that is crashing it but Im not quite sure what it is. I would really appreciate any insight anyone can provide. Thanks. Here is my app code.

int potVal = 0;       
int finalPotVal = 0;
float hour = 3600000;
float wait = 0;
int load = 0;
int bed = 0;

int sensorPin = A0;
int potIn = A1;
int buttonPin = 2;   // the number of the input pin
int flipSwitch = 4;
int buzzPin = 5;
int ledPin = 6;       // the number of the output pin

int buttonState = 0;
int flipState = LOW;
int buzzState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)


void setup()
{
    RGB.brightness(0);
  pinMode(buttonPin, INPUT);
  pinMode(flipSwitch, INPUT);
  pinMode(buzzPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
 
  
}



void beep(){
unsigned long currentMillis = millis();
 if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (buzzState == HIGH)
      buzzState = LOW;
    else
      buzzState = HIGH;

    // set the LED with the ledState of the variable:
    digitalWrite(buzzPin, buzzState);
  }
}
void loop()
{
  buttonState = digitalRead(buttonPin);
  flipState = digitalRead(flipSwitch);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  ;
  
  
  switch (digitalRead(2)) {
  case 0:

     potVal = analogRead(potIn);            
 
finalPotVal = map(potVal, 0, 6000, 0, 16000);
  
  if (finalPotVal >= 0 && finalPotVal < 400) {wait=hour*.3;digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 401 && finalPotVal < 850) {(wait=hour*.75);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 851 && finalPotVal < 1850) {(wait=hour*1);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 1851 && finalPotVal < 2400) {(wait=hour*4);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 2401 && finalPotVal < 2800) {(wait=hour*4.5);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 2801 && finalPotVal < 3300) {(wait=hour*5);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 3301 && finalPotVal < 3600) {(wait=hour*5.3);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 3601 && finalPotVal < 3900) {(wait=hour*5.6);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 3901 && finalPotVal < 4200) {(wait=hour*6);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 4201 && finalPotVal < 4340) {(wait=hour*6.25);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 4341 && finalPotVal < 4550) {(wait=hour*6.5);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 4551 && finalPotVal < 4610) {(wait=hour*6.75);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 4611 && finalPotVal < 4705) {(wait=hour*7);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 4706 && finalPotVal < 4830) {(wait=hour*7.25);digitalWrite(ledPin,LOW);}
  if (finalPotVal > 4831 && finalPotVal < 4920) {(wait=hour*7.5);digitalWrite(ledPin,HIGH);}
  if (finalPotVal > 4920 ) {(wait=hour*8);digitalWrite(ledPin,LOW);}
  
  
 
 

  
Serial.print("      Time Delay:");
//Serial.print(wait);
Serial.print(finalPotVal); //for testing actual pot val
Serial.print("    Button State:");
Serial.println(buttonState);
delay(100);
    break;

  case 1:
    digitalWrite(6, HIGH);
   
  delay(wait);
   
   for(int x=0; x<500000; x++){
    load = analogRead(sensorPin);  



if(flipState==HIGH) {bed = 4000;}
else{bed = 950;};
    


if(load > bed) {(beep());
}
else{digitalWrite(buzzPin,LOW);}

Serial.print("Weight:");
Serial.print(load);
    Serial.print("    Button State:");
Serial.println(buttonState);
    delay(10);

} 

  }
 
}

My Core just did the same thing. I could not get it to work despite resets, etc. My fix was to plug it into my laptop on USB and it came back to life immediately. Have you tried that random and unlikely fix?

Thanks for the reply. I tried that and it still goes off after booting up with my particular app. But it turns on just fine when I flash other apps to it. Very odd.

Hi @criff415

I think a lot of the types you are using are non-standard and you would be better off using the normal ones. For instance, you are using long to store some values from millis(); but it returns unsigned long, not long which is a signed type and can’t hold as large a value. Comparisons will fail when the values of time get large enough that the MSB is set.

You are also using float to hold your hours and wait and then calling delay(wait); with a very large float number when delay() takes an unsigned long, not a float.

I can’t say for sure that these are causing your problems and the compiler maybe bailing you out, but you should really try to use the correct type for these calls.

Can you try changing these and report back?

1 Like

@criff415 I believe your problem is one line in setup()
Comment this out and it should breathe once more! :wink:

RGB.brightness(0);

1 Like

Awesome! I think both of the comments helped solve the problem. I really appreciate you guys taking the time to look through my beginner code. I was making an alarm clock, so a while back I added the code to see if that would dim the light. It didnt work at the time and I forgot about it. Thanks!!!

3 Likes