Pages: [1]   Go Down
Author Topic: String object charAt(index) question.  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
/*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:
Code:
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:

Code:
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:

Code:
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
Code:
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
Code:
string = "";
.
Logged

Arduino Uno;
Mega328

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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.
« Last Edit: September 09, 2012, 08:45:24 pm by jerseyguy1996 » Logged

Arduino Uno;
Mega328

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3359
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Well, if you actually want help...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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!
Logged

Arduino Uno;
Mega328

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-D
Logged

Arduino Uno;
Mega328

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: