0

I need to update my global variable once I get a 'overdtrue' response and light up my LED but only my LED is lighting up but my global variable is not updated.

Here is my code

int x = 0;

void loop() { 
if (content.indexOf("ovrdtrue") > 0)
 {
  x++;
  digitalWrite(ledPin, HIGH);

 }
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
Serial.print("X: ");
Serial.println(x);
}

The x value should be 1 but only the LED has light up. Please help.

enter image description here

Duckbenok
  • 139
  • 1
  • 4
  • 14
  • once I get a 'overdtrue' response - if (content.indexOf("ovrdtrue") > 0) - is it overdtrue or ovrdtrue? – Nick Gammon Dec 04 '17 at 07:33
  • Sorry for the confusion. it's "ovrdtrue" – Duckbenok Dec 04 '17 at 13:42
  • My guess is that there is more to this code (which actually looks like parts from an HTTP server). Your snippet is leaving out the part that could explain this behavior. Either post a working snippet with the error or post more of the actually code. – Mikael Patel Dec 04 '17 at 16:02

4 Answers4

1

It seems like I forgot to include an if else condition above the ovrdtrue.

if(content.indexOf("true") > 0)
{
  digitalWrite(ledPin, HIGH);   

}

Arduino is reading this condition even though the data received "ovrdtrue" but this contains some word "true" so this is the one getting read and not the ovrdtrue condition. I just changed the condition variable not close to the word true and it worked fine. I'm sorry for this.

Duckbenok
  • 139
  • 1
  • 4
  • 14
0

You never set the LED pin to LOW, so it will stay lit up regardless of the global variable.

Nick Gammon
  • 38,184
  • 13
  • 65
  • 124
  • Yes I want it to stay lit at the same time change my global variable x value from 0 to 1 with the digitalWrite(ledPin, HIGH); command but somehow it's ignoring the x++; and just lights up the LED – Duckbenok Dec 04 '17 at 13:48
  • You need to post more code. What pin is ledPin? Is ledPin ever set to output? Do you change x elsewhere? Do you change ledPin elsewhere? Is x declared elsewhere? Global variables definitely update inside loop so your problem is elsewhere. – Nick Gammon Dec 04 '17 at 21:12
0

Are you sure the LED is not lighted from the start? It's unclear if in the Setup you set the light on or off (or it is default on or off).

Print a Serial print inside the if statement to be sure that the if block is entered, also print before the if the value of content to be sure if 'ovrdtrue' is found in the string.

Also, since you want the LED on in this situation, explicitly set it off in the setup.

Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56
  • the LED are not lighted from the start. I think it has found the "ovrdtrue" because it is lighting the LED with the HIGH during the if and that's ok but my problem is why is it ignoring the x++ and not updating my global variable inside the if. – Duckbenok Dec 04 '17 at 13:46
0

Here is my code

your code ran without setup()?

you are conditionally incrementing "x", and if your condition doesn't hold true, the increment doesn't take place.

dannyf
  • 2,770
  • 10
  • 13