I'm looking to take incoming serial data and separate it out into 3 commands.
I can change the incoming data to whatever works best, but right now I should be receiving "X123 Y123 T1" for example. The "X" and "Y" here will be anything from 0 to 180 so the length may vary from 1 to 3 digits. The T will always be 0 or 1.
I've been exploring serial.parsInt and serial.read but I'm wondering how to use them best or if there is a less clunky way of gathering my coordinates and chopping them up correctly.
Is there a prettier more simple way of taking "(X123 Y45 T0)" and only sending ServoX "123", ServoY "45" and ServoT "0"?
Bad sudo code example but something like:
X = Numbers after X
Y = Numbers after Y
T = Number after T
void setup() {
Serial.begin(115200);
}
void loop() {
static bool isMessage = false;
if (Serial.available() > 0)isMessage = true;
while (isMessage) {
if (Serial.read() == "X") { // this take first byte and compare to symbol "X"
X = Serial.parseInt(); //from second position are numbers. they ends with "Y"
Y = Serial.parseInt(); // so it not need to be read. but caution if space before "Y"
T = Serial.parseInt(); // here ends numbers with "\r" symbol
}
else {
while (Serial.available() > 0)Serial.read();
isMessage = false;
}
}
}
This is beautiful and clean like I was hoping.
So to try and understand this a bit better for future use, I have a follow up question.
So, "X = Serial.parseInt();" will take the number after X of course, but it stops at Y? So you can then just run parse again and it auto stops at the next non int?
I guess to phrase it differently. I knew the data before the Int was basically thrown away, but I never got a clear answer on what it does with data after the int.
Thanks again. I "think" I understand I just want to make sure I have the concept right.
I think I understand. I will play with it to see how it operates exactly. Thank you very much.
Guess I needed an example of multiple Int's in a line separated by characters.
The problem is synchronizing the the program to the arrival of the data. As shown, you do not know the end of the current message until you read the "X" at the beginning of the next message.