How to validate using bytes? (in if's for example)

How to validate using bytes?

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

Cheers!

I use type char.

char x='a';
if (x == 'a') do something;

just do

if (byteread =='>')

if you really really want a byte then you can use

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.

Cool, thanks a lot both of you, now, did somebody know why it's sendining me this error: "'byteread' was not declared in this scope"

This is the complete code:

int i = 0;
String d1 = "SomethingSomething";
LinkedList<int> KeyNontesNode = LinkedList<int>();

byte byteRead = '<';

void setup() {                
  Serial.begin(9600);
}

void loop() {
  byteRead = '<';
  if (Serial.available()) 
  {
    if(Serial.read() != ' '){
      byteRead = Serial.read();
    }
    Serial.write(byteRead);
  }
  
  if(byteread == '>'){
    tone(9,NOTE_D7,10);
  }

  i = KeyNontesNode.size();
  
  if ((i >= 5) and ((KeyNontesNode.get(KeyNontesNode.size() - 1)) == '>'))
  {
    
  }
  
}

Thanks in advance!

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.

...R

Obsdark:
Cool, thanks a lot both of you, now, did somebody know why it's sendining me this error: "'byteread' was not declared in this scope"

You have both byteread and byteRead in your code, but only the latter is declared.