Help with code caught in loop

I have this code and it keeps getting caught in the first if() statement. Any chance I could get some help?

TCPClient client;

int button1 = D2;                       // button is connected to D2
int button2 = D3;                       // button is connected to D3
int button3 = D4;                       // button is connected to D4
int button4 = D5;                       // button is connected to D5
int button5 = D6;                       // button is connected to D6
int led1 = D0;                          // LED is connected to D0
int led2 = D1;
int led3 = A0;
int led4 = A1;
int led5 = A4;
int led_low = A5;
int ledD = D7;
int val = 0;                           // variable to store the read value
int starvalue = 0;
int Button1Count = 0;
int Button2Count = 0;
int Button3Count = 0;
int Button4Count = 0;
int Button5Count = 0;
char resultstr[64];

void setup()
{
  pinMode(led_low, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);               // sets pin as output
  pinMode(led3, OUTPUT); 
  pinMode(led4, OUTPUT); 
  pinMode(led5, OUTPUT); 
  pinMode(button1, INPUT_PULLUP);    // sets pin as input
  pinMode(button2, INPUT_PULLUP);    // sets pin as input
  pinMode(button3, INPUT_PULLUP);    // sets pin as input
  pinMode(button4, INPUT_PULLUP);    // sets pin as input
  pinMode(button5, INPUT_PULLUP);    // sets pin as input
  Spark.variable("Button1Count" , &Button1Count, INT);
  Spark.variable("Button2Count" , &Button2Count, INT);
  Spark.variable("Button3Count" , &Button3Count, INT);
  Spark.variable("Button4Count" , &Button4Count, INT);
  Spark.variable("Button5Count" , &Button5Count, INT);
  Spark.variable("starvalue" , &starvalue, INT);
  Spark.variable("result", &resultstr, STRING);
  pinMode(ledD, OUTPUT);
  ledStatus(5, 100);
  

}

void loop()
  {
  //analogWrite(led1, 255);   // Turn ON the LED
  //delay(100);               // Wait for 1000mS = 1 second
  analogWrite(led1, 75);    // Turn OFF the LED
  analogWrite(led2, 75);    // Turn OFF the LED
  analogWrite(led3, 75);    // Turn OFF the LED
  analogWrite(led4, 75);    // Turn OFF the LED
  analogWrite(led5, 75);    // Turn OFF the LED
  analogWrite(led_low, 255);    // Turn on the LED
  delay(100);               // Wait for 1 second
  

  val = digitalRead(button5);          // read the input pin
  //digitalWrite(led1, val);             // sets the LED to the button's value
if (digitalRead(button5) == LOW) 
{
    analogWrite(led5, 255);
    analogWrite(led4, 255);
    analogWrite(led3, 255);
    analogWrite(led2, 255);
    analogWrite(led1, 255);
    Button5Count ++;
    sprintf(resultstr,"{\"5 stars\":%d}", 5);
    Spark.publish("5 Stars",resultstr);
    starvalue = 5;
    xivelyStars(starvalue);
    delay(1000);
    
//}
  
  val = digitalRead(button4);          // read the input pin
  //digitalWrite(ledD, val);             // sets the LED to the button's value
if (digitalRead(button4) == LOW) 
{
    analogWrite(led5, 0);
    analogWrite(led4, 255);
    analogWrite(led3, 255);
    analogWrite(led2, 255);
    analogWrite(led1, 255);
    Button4Count ++;
    sprintf(resultstr, "{\"4 Stars\":%d}", 4);
    Spark.publish("4 Stars",resultstr);
    starvalue = 4;
    xivelyStars(starvalue);
    //delay(1000);
}
    val = digitalRead(button3);          // read the input pin
  //digitalWrite(ledD, val);             // sets the LED to the button's value
if (digitalRead(button3) == LOW) 
{
    analogWrite(led5, 0);
    analogWrite(led4, 0);
    analogWrite(led3, 255);
    analogWrite(led2, 255);
    analogWrite(led1, 255);
    Button3Count ++;
    sprintf(resultstr, "{\"3 Stars\":%d}", 3);
    Spark.publish("3 Stars",resultstr);
    starvalue = 3;
    xivelyStars(starvalue);
    //delay(1000);
}

    val = digitalRead(button2);          // read the input pin
    //digitalWrite(ledD, val);            // sets the LED to the button's value
if (digitalRead(button2) == LOW) 
{
    analogWrite(led5, 0);
    analogWrite(led4, 0);
    analogWrite(led3, 0);
    analogWrite(led2, 255);
    analogWrite(led1, 255);
    Button2Count ++;
    sprintf(resultstr, "{\"2 Stars\":%d}", 2);
    Spark.publish("2 Stars",resultstr);
    starvalue = 2;
    xivelyStars(starvalue);
    //delay(1000);
}

val = digitalRead(button1);          // read the input pin
  //digitalWrite(ledD, val);             // sets the LED to the button's value
if (digitalRead(button1) == LOW) 
{
    analogWrite(led5, 0);
    analogWrite(led4, 0);
    analogWrite(led3, 0);
    analogWrite(led2, 0);
    analogWrite(led1, 255);
    Button1Count ++;
    sprintf(resultstr, "{\"1 Stars\":%d}", 1);
    Spark.publish("1 Stars",resultstr);
    starvalue = 1;
    xivelyStars(starvalue);
    //delay(1000);
}
}

  void xivelyStars(int starvalue) {
      

  ledStatus(5, 100); 

    /*if (client.connect("api.xively.com", 8081))
    {
       //Serial.println("connection established");
        // Connection succesful, update datastreams
        ledStatus(5, 100); 
        client.print("{");
        client.print("  \"method\" : \"put\",");
        client.print("  \"resource\" : \"/feeds/");
        client.print(FEED_ID);
        client.print("\",");
        client.print("  \"params\" : {},");
        client.print("  \"headers\" : {\"X-ApiKey\":\"");
        client.print(XIVELY_API_KEY);
        client.print("\"},");
        client.print("  \"body\" :");
        client.print("    {");
        client.print("      \"version\" : \"1.0.0\",");
        client.print("      \"datastreams\" : [");
        client.print("        {");
        client.print("          \"id\" : \"starvalue\",");
        client.print("          \"current_value\" : \"");
        client.print(starvalue); //adjustment for some weird reason..
        client.print("\"");
        client.print("        }");
        client.print("      ]");
        client.print("    },");
        client.print("  \"token\" : \"0x123abc\"");
        client.print("}");
        client.println();

        ledStatus(3, 100);
    }
    */

    if (client.connect("rater.jdimitrov.com", 80))
    {
       //Serial.println("connection established");
        // Connection succesful, update datastreams

        //ledStatus(5, 100); 
        analogWrite(led_low, 0);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 255);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 0);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 255);    // Turn on the LED
        delay(100);               // Wait for 1 second
        client.print("GET /api/submit/");
        client.print(Spark.deviceID());
        client.print("/");
        client.print(starvalue);
        client.println(" HTTP/1.1");
        client.println("Host: rater.jdimitrov.com");
        client.println("Accept: text/html, text/plain, application/json");
        client.println("User-Agent: Arduino/1.0");
        client.println("Content-Length: 0");
        client.println();
    
        //ledStatus(3, 100);
        analogWrite(led_low, 0);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 255);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 0);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 255);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 0);    // Turn on the LED
        delay(100);               // Wait for 1 second
         analogWrite(led_low, 255);    // Turn on the LED
        delay(100);               // Wait for 1 second
        analogWrite(led_low, 0);    // Turn on the LED
        delay(100);               // Wait for 1 second
    }
    else 
    {
        // Connection failed
        Spark.publish("ERROR","Connection Failed...");
        //Serial.println("connection failed");
        ledStatus(5, 20);
    }

    delay(1000);
    client.flush();
    client.stop();
    


  }  
void ledStatus(int x, int t)
{
    for (int j = 0; j <= x-1; j++)
    {
        digitalWrite(ledD, HIGH);
        delay(t);
        digitalWrite(ledD, LOW);
        delay(t); 
   }

I forgot to add that it will wait for a button to be pushed, after the button is pushed it gets caught in the first if loop.

@mikesr20, you seem to have commented out the “}” after your first if statement. Any reason? Also, you may want to add Serial.print() debug statements that will give you a better idea of where your code hangs or loops. :smile:

1 Like

I think that was an accident, I was playing with the code to see if I could figure it out. I commented out the entire button5 if loop and it works great now. not sure why that one loop makes it constantly trip, Im thinking there is a hardware issue.

Is it possible to erase the code without a hard reset just using the buttons? When it gets caught in the loop I cant push updated code to it so I have to hard reset and reprogram to wifi. If I could erase the code with button pushes that would save some time.

No, unfortunately there is no easy way (well, no easy way if you can’t use the DFU-Util). I like to put a check in my setup that looks for a pin being pulled high or low and if so goes into a loop for reprogramming so that way, if I ever mess anything up I can pull that pin high or low and then reset the core and I can then re-program it!

Currently a Factory Reset is the only hard reset using buttons, but it also erases your Wi-Fi credentials which takes time to restore.

What I like to do is use the Spark CLI, put my Core into DFU Mode and run the following command:
spark flash --usb tinker

This factory resets the firmware to Tinker locally over USB without erasing the Wi-Fi credentials.

Installing Spark CLI and getting DFU UTIL to work will take some time on Windows, and less time on a Mac to get installed… but it’s WELL worth it once you have it done. If you are reading this… and don’t have it yet, I urge you to check it out :smile:

1 Like

Hi @mikesr20,

Another workaround we often see is setting up one of your pins to be a ‘sanity’ jumper, for example, while pin XX is low, then just run Spark.process() forever. This will let you bypass logic that’s preventing your core from OTA-ing, and lets you build in a safeguard that’s easier than a factory reset. :slight_smile:

Thanks,
David

This comes up quite often, is this something we should build into the firmware? E.g. if the mode button is pressed within 1s of startup and held for 1s it will put the core in safe mode? We could provide hooks so that users could also write custom triggers for safe mode. And we could document how to code firmware so that safe mode can be triggered remotely.

2 Likes

@mdma, YES!!! Loosing control of a core with only the option of a factory reset is crazy limiting :smile:

3 Likes