@PaulS: Okey, I now have this at the beginning of the loop: while(!Serial.available());
@Grumpy_Mike: Thanks for making that clear!
Anyways, i'm trying to follow your advices and i came up with this:
const int led0 = 9;
const int led1 = 11;
char incomingByte = 0;
char checkByte = 0;
char b_value[3];
char t_value[3];
void setup() {
Serial.begin(115200);
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
};
void loop() {
while(!Serial.available());
incomingByte = Serial.read();
if(incomingByte=='X') {
//collect the next three bytes in an array
for(int i=0; i < 2; i++) {
checkByte = Serial.read();
//check that only values 0-9 are indexed
if(checkByte >= '0' && checkByte <= '9') {
b_value[i] = checkByte;
//Serial.println(b_value); //debug
} else {
break;
}
}
} else if(incomingByte=='Y') {
for(int i=0; i < 2; i++) {
checkByte = Serial.read();
//check that only values 0-9 are indexed
if(checkByte >= '0' && checkByte <= '9') {
t_value[i] = checkByte;
//Serial.println(t_value); //debug
} else {
break;
}
}
} else {
incomingByte = Serial.read();
};
//int b_val = b_value[0]*100 + b_value[1]*10 + b_value[2];
//int t_val = t_value[0]*100 + t_value[1]*10 + t_value[2];
int b_val = (b_value[0] & 0xf) * 100 + (b_value[1] & 0xf) * 10 + (b_value[2] & 0xf);
int t_val = (t_value[0] & 0xf) * 100 + (t_value[1] & 0xf) * 10 + (t_value[2] & 0xf);
//analogWrite(led0, b_val);
//delay(30);
//analogWrite(led1, t_val);
//delay(30);
Serial.println(b_val); //debug
};
The idea was to have "if(checkByte >= '0' && checkByte <= '9') { .. }" make sure that only the numbers I want are indexed into the array.
But it doesn't seem to work as expected.
I get only 0 in return, when I input e.g. "X120Y000" in the serial monitor.
And I get nothing when I try Serial.println from the for loop(s).. so I guess the boolean in my if statement isn't working as I think it should?
I'm also not sure how to check that the order is correct when collecting the values from "checkByte"..
Last, if anyone has a better suggestion for making this work, i'll be happy to hear it!
thanks,