I mean, how to validate if a char and a byte "are the same ones", that for specific chars, like for the use of read some console input keyboard and compare it to some specific char, so to have something happened if that byte, converted to char (or String of size 1), is equal to that char.
for example:
byte byteRead;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available())
{
byteRead = Serial.read();
Serial.write(byteRead);
}
if(byteread == byte('>')){ //Aparently, doesn't work
//Something to do with the code
}
}
I'm using Arduino UNO and Windows 10
Any help would be gladly apreciated, Thanks in advance
if (byteread == ((byte) '>')) but the compiler will do it for you
sometimes though it is best to declare byteread as an int though because that's what Serial.read() will return.
That might be important when Serial.read returns -1 if no data is available which will be 0xFFFF if you store that only into a byte you will get 0xFF which could be an authorized byte sent on the serial line.
so to have something happened if that byte, converted to char (or String of size 1), is equal to that char.
This throws up some subtleties that can catch the unwary.
Firstly byte is an unsigned type, char is signed, so some byte values are not representable as char and
vice-versa and cannot be converted. So for instance:
char c = -5 ;
byte b = somefunction () ;
if (b == c) // never true
if ((char) b == c) // can be true as b is forced to be interpreted as signed.
And secondly a string of length 1 is not a char or a byte, its a sequence and is represented by a pointer
to memory, and you'd not be comparing like with like in C. Other languages handle things differently
if (c == "2") // undefined - all depends on the address of the memory in which the string "2" is stored.
Maybe have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.
It is not a good idea to use the String (capital S) class as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.