Why is my Spark Core resetting itself mid loop? Video and code included

My core seems to be resetting itself the second time I push a button, as if it is jumping out of the loop. Sorry for the code being all over the place.
Video: http://youtu.be/OCxk3PiU1B8


#define FEED_ID "x"
 #define XIVELY_API_KEY "x"

TCPClient client;

int button1 = D4;                       // button is connected to D0
int button2 = D3;                       // button is connected to D1
int button3 = D2;                       // button is connected to D2
int button4 = D1;                       // button is connected to D3
int button5 = D0;                       // button is connected to D4
int LED = D6;                          // LED is connected to D6
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, OUTPUT);               // sets pin as output
  pinMode(button1, INPUT_PULLDOWN);    // sets pin as input
  pinMode(button2, INPUT_PULLDOWN);    // sets pin as input
  pinMode(button3, INPUT_PULLDOWN);    // sets pin as input
  pinMode(button4, INPUT_PULLDOWN);    // sets pin as input
  pinMode(button5, INPUT_PULLDOWN);    // 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);
  
  // Go here for live feed: https://api.spark.io/v1/devices/x/events?access_token=x
}

void loop()
{
  val = digitalRead(button5);          // read the input pin
  digitalWrite(LED, val);             // sets the LED to the button's value
if (digitalRead(button5) == HIGH) 
{
    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(LED, val);             // sets the LED to the button's value
if (digitalRead(button4) == HIGH) 
{
    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(LED, val);             // sets the LED to the button's value
if (digitalRead(button3) == HIGH) 
{
    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(LED, val);            // sets the LED to the button's value
if (digitalRead(button2) == HIGH) 
{
    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(LED, val);             // sets the LED to the button's value
if (digitalRead(button1) == HIGH) 
{
    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); 
   //Serial.println("Connecting to server...");
    if (client.connect("api.xively.com", 8081)) 
    {
        // 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);        
    } 
    else 
    {
        // Connection failed
        //Serial.println("connection failed");
        ledStatus(3, 200);
    }
  }  
void ledStatus(int x, int t)
{
    for (int j = 0; j <= x-1; j++)
    {
        digitalWrite(ledD, HIGH);
        delay(t);
        digitalWrite(ledD, LOW);
        delay(t); 
   }

}

Hi @mikesr20,

Hmm, it looks like you’re not cleaning up your client object after you connect, so the second time around it would probably get confused. I would recommend throwing in a

client.flush();
client.stop();

after your web request.

Hope that helps!
Thanks,
David

2 Likes