I have been trying for a couple hours to be able to check what a user input is and then have a proper input. I made it really simple to see what was the problem but I kept getting the same result which was the condition "HI".
My goal is to have a input from the serial monitor and if the input is a defined command and then something will happen(servos move a certain way).
But, I have had trouble checking what the input was? It always says it is never a number even when it is less than 3 it just always says the string "HI" no matter the input.
int val = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
val = Serial.read();
if(val == 3) {
Serial.write(5);
}
else {
Serial.write("HI"); // used to see if the other condition would always be failed
}
}
}
Nope, I changed it to a char.
Also, do you know if its possible to read more than the first byte? I want to use commands like back or forward but they are more than one byte and .read() only reads the first byte.
Thanks, again.
Not sure on that, sorry... you might have to build a string a character at a time, but I'm not sure.
But why not just make do with one character?.... I steer a robot over wireless usb (wixels) using the numeric 8-6-2-4 for direction and iirc I used 5 for something too- stop probably.
BrandonYoung:
Also, do you know if its possible to read more than the first byte? I want to use commands like back or forward but they are more than one byte and .read() only reads the first byte.
You could have Serial.available() wait until at least 2 characters are available and then call Serial.Read() back to back.
You could use the realBytes() to get a specific number of bytes
You could use readBytesUntil() with a terminator (like CR or NL) for more reliable operation:
BrandonYoung:
Nope, I changed it to a char.
Also, do you know if its possible to read more than the first byte? I want to use commands like back or forward but they are more than one byte and .read() only reads the first byte.