# Help with Reading and converting serial string

Hello,

I’m working on a project using 4D labs, 3.2 uLCD with touch screen where I want to the screen to send serial data something like:

o1234

The o is to tell the arduino its for the on time variable for teh PWM, and the 1234 is umber I want to be palced in the variable as well as limiting the incoming number to 0-9999, right now though I’m just using the terminal window from arduino to try and get the arduino to only read the RX buffer when the o is detected, and to take the individaul ascii characters and combine them into an integer, here’s what I have so far:

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

int str2int(){
int n, x;
int str[4];

n = 4;
while(n–){
if (Serial.available() > 0) //str 0 = 102 and str 3 = 101 str 5 = 100
}

/if (str[3] >= 48 && str[3] <=57){
x = ((str[3]-48) << 10)- ((str[3] -48) * 24);
if (str[2] >= 48 && str[2] <=57){
x = x + (( str[2] - 48) << 7) - ((str[2] - 48) * 28);
if (str[1] >= 48 && str[1] <=57){
x = x + ((( str[1] - 48 ) << 3) + ((str[1] - 48) * 2));
/
if (str[3] >= 48 && str[3] <=57){
x = ( str[3]-48 ) ;
}
/* }
}
}*/
return x;
}

void loop() {

if (Serial.available() > 0){
Serial.println(str2int());
}
}

So far I can get to check for the o, recognize the o, and start reading the actual digits… the problem is I have yet to even be able to get the ascii numbers 48-57 or 0-9, I always get 0, -1, or some random number from 1 - 35652 or even negative numbers.

Any help with getting the arduino to read the digits, and combine them into the equivilant combined integer would be greatly apreciated.

If you just want the total value of the characters

``````// after getting the 'o'
n = 4;
val = 0;
while(n--) {
while (Serial.available() == 0); // wait for a character
val *= 10;