I'm communicating with an arduino over an ethernet shield from my android phone. The arduino is then relaying the instruction over bluetooth to another arduino which then carries out an action. I'm reading the instruction from the ethernet shield into a String object and then using switch case to evaluate each character in the instruction as follows:
/*Commands from the web and commands sent and received to peripheral devices
follow the following format:
1st character:
'a' = manually turn an outlet ON of OFF
'b' = timer control of outlet
2nd character:
'a' = Outlet 1
'b' = Outlet 2
'c' = Outlet 3
'd' = Outlet 4
3rd character (manual control):
'a' = ON
'b' = OFF
3rd through 10th character (timer control):
HHMM = time on
HHMM = time off
so "aba" would manually turn on outlet 2 and "acb" would manually turn
off outlet 3.
*/
so I can send something like:
http://192.168.1.148/?aaa/
where '?' signifies the start of the command and '/' signifies the end of the command. My command string should contain "aaa" once I finish reading the characters in.
Then I use a switch case like this:
switch(sent.charAt(0))
{
case 'a':
Serial.print('?');
Serial.print(sent);
Serial.print('/');
break;
//default:
//break;
}
Once I finish executing the instruction I clear the command string by using:
sent = "";
The problem is that everything works fine the first time I send a command after a reset but the second time it hangs at the switch case....actually it just doesn't seem to find the first letter of the command string. I have verified that my command string contains the proper characters, but it is almost as if the
sent.charAt(0)
is not referencing the first letter of the string anymore. I can give you more of the code if it would help. Mostly I just want to know if charAt(0) always references the first letter of the string....even after clearing the string with the
string = "";
.