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);
}