String object charAt(index) question.

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 = "";.

I have a depressing feeling you're using the String class rather than dealing with character arrays directly. If you are using String, I suggest you think about getting rid of it. Either way, it'll be difficult to guess why your sketch is going wrong until you post a complete sketch that demonstrates the problem.

Yes I was using the String class because I felt that the various methods would be useful. I am hoping to figure it out this way so that I can avoid working with string arrays.........lazy.....I know. I added Serial.println(sent.indexOf('a')); to get a better idea of what was going on. Interestingly enough the first time through the routine it shows the index of the character 'a' as zero/0 and the second time through the routine it shows the index of the character 'a' as -1. Why in the world would it not always index the first letter in the string to zero???? I can post my entire sketch but it is pretty ugly looking.

jerseyguy1996:
I can post my entire sketch but it is pretty ugly looking.

Well, if you actually want help...

Why in the world would it not always index the first letter in the string to zero????

-1 means that the character was not in the string. It does not mean that it was at position -1.

jerseyguy1996:
Interestingly enough the first time through the routine it shows the index of the character 'a' as zero/0 and the second time through the routine it shows the index of the character 'a' as -1. Why in the world would it not always index the first letter in the string to zero????

It is a good idea to read the documentation before using library functions that you are unfamiliar with.

PaulS:

Why in the world would it not always index the first letter in the string to zero????

-1 means that the character was not in the string. It does not mean that it was at position -1.

Thanks for pointing that out. Turns out I was assigning a NULL character to the first position in the string which I am guessing made it impossible to find any characters that came after it because a NULL character signifies the end of the string. I moved some things around and now the sketch is working as intended. Thanks!

Arrch:

jerseyguy1996:
I can post my entire sketch but it is pretty ugly looking.

Well, if you actually want help...

I'm usually embarrassed by the spaghettiness (is that a word?) of my code :smiley:

I'm usually embarrassed by the spaghettiness (is that a word?) of my code

Yes, it is. If you post your code, we promise not to laugh at it (at least, not too long). Code reviews are a great way to learn to write better code.