2

Hate to post this, but I am googled out and tried many different options with no success.

Basically I would like to GET a page with the status of 10 lights and turn on/off the corresponding lights. The Arduino is getting the results and posting the string in the serial monitor, but the string is not triggering my if statements. Not sure if I am using the wrong function to parse the string or my if statements are incorrect.

Here is the output of the GET, which posts in the serial monitor (printing readString):

Light1:1 Light2:0 Light3:0 Light4:0 Light5:0 Light6:1 Light7:0 Light8:1 Light9:0 Light10:0

Here is the last thing I tried in the sketch, with an if statement for each light:

if (readString.indexOf("Light1:1">0)) 
{
    digitalWrite(light1, HIGH);
}
else
{ 
    digitalWrite(light1, LOW);  
}

What function would you use? What am I doing wrong? Restructuring the output of the page is also an option.

2
  • 1
    Seems like an easier format would just be 10 1s or 0s all in a row...that's easier to parse (but less flexible or extensible).
    – nneonneo
    Sep 12, 2012 at 2:34
  • I tried changing the output to just 1s and 0s. Seems more efficient and flexibility isn't an issue here. I used readString.substring(1,1) == "1" for each position, still no luck. Sep 12, 2012 at 20:32

2 Answers 2

2

Possible typo... you've got your comparison inside the .indexOf() call. Did you mean

if (readString.indexOf("Light1:1") >= 0) 
1
  • Yahtzee! :-) You were correct my indexOf statement was not formatted correctly. Sep 12, 2012 at 23:51
1

The string will start at position 0. Use >= 0

2
  • and you'll get a value of -1 if not found.
    – ViennaMike
    Sep 12, 2012 at 16:31
  • Tried the solution, still no luck. Sep 12, 2012 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.