Arduino UNO serial.read specified command

Hi Everyone

This is my first topic on this forum, I hope that thankfully to your help my problem will soon become solved. Sory for my english if I will make some mistakes. So, my problem is, when I am trying to operate diode through serial port with my UNO. I want to set the pin to HIGH state when in serial monitor I send a command =a1, and to LOW state by typping command =a0. The problem is, that it works only partialy, because if I type in send box only 1 my UNO interpretates that comand same as =a01 command. And in other way- when I want to set pin to LOW state by sending =a0 command, UNO switching LED of when i send only 0, but also when i type any other cobination of characters containing 0. I want UNO to react only for specified =a1 and =a0 commands, nothing more or less than that commands. I think that problem can be caused by using wrong conversion unit like if(stateq == char('=a1')) instead for example if(stateq == byte('=a1')) I wrote a code:

const int ledPin = 9;      // the pin that the LED is attached to
int stateq;
void setup()
{
  // initialize the serial communication:
  Serial.begin(115200);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(6,OUTPUT);
}

void loop() {


  
  if (Serial.available()) {
   
   stateq = Serial.read();
   if(stateq == char('=a1')){
  

    digitalWrite(ledPin, HIGH);
    digitalWrite(6, LOW);
   }
     if(stateq == char('=a0')){
  

    digitalWrite(ledPin, LOW);
    tone(6,1000,300);
        delay(300);
        tone(6,2100,300);
        delay(300);
        tone(6,1000,300);        
        delay(300);
        tone(6,2100,300);
        delay(300);
        tone(6,1000,300);        
        delay(300);
         tone(6,2100,300);
        delay(300);  
        
   }
   if(stateq == char('=a0')){
   digitalWrite(6, LOW);
   }
  }
}

Thanks for any help Best Regards

Karol

char('=a0')){

Three characters can't be made into a single character.

http://www.gammon.com.au/forum/?id=12112

Hello and welcome

if (Serial.available()) {//if you have at least one byte you the go read it, but wait you are sending perhpaps 3 bytes , byte corresponding to A, second byte corresponding to 1 and the carriage return

stateq = Serial.read(); //Serial.read only reads one byte!!! if(stateq == char('=a1')){//You cannot compare like this, and also keep in memory you only had read one byte

digitalWrite(ledPin, HIGH); digitalWrite(6, LOW); }