Serial.read and so on

Yep, I have an issue with serial.read as the subject suggests!

I'm new to the forums, but I've been following for a while. My search results didn't really give me what I was after, that I could see anyway!

Essentially, If I want to send a value via serial to my arduino, I'm using lvar = Serial.read()-48;
Which works fine - it gives me a nice single digit int out that I'm expecting, however I'm quite keen to send ints all the way up to 1023 and down to -1023, as well as have a second value, rvar, which I can't seem to get working?

Has anyone got any pointers to show me the right direction?

Regards,

hxx

char inData[24]; // or some reasonable size
int index = 0;

void loop()
{
   // See if there is any serial data to read
   if(Serial.available() > 0)
   {
       char aChar = Serial.read();
       if(aChar == '<') // Start of a new packet
       {
           index = 0;
           inData[index] = '\0'; // NULL the array
       }
       else if(aChar == '>')
       {
           // End of packet. Parse inData and do something
       }
       else // Somewhere in the packet
       {
           inData[index] = aChar; // Store the character
           index++; // Advance the pointer
           inData[index] = '\0'; // Keep the array NULL terminated
       }
   }
}

This code will read in data like <1023, -1023>, and store "1023, -1023" in inData. Then, you can use strtok to split the string into tokens ("1023" and "-1023"). Then, you can use atoi to convert the tokens to integers.

Alternately, if you intend to send the binary-encoded values of -1023 -> 1023 (instead of the string representations) you'll need to do some highhbyte/lowbyte calls to split it up, send it over serial in 2 separate bytes, then reassemble it at the receiving end (bitshift the first byte by 8 places and OR it with the second byte).

If you need more specifics I (and others) can help but this might be a good enough start.
Or Paul's code will probably do the trick if you're justing sticking to string representation...