Control motor speed via the BT code

Hello Everyone,

I'm trying to develop the code for my 4 wheel car (Arduino uno as a main controller). The interface is established via the Bluetooth.

The code is divided in to the different states, which controls the car go forward or backwards. The state is selected via serial.read. Every state has its variable which is controlled via serial.read. And here is my frustration, I can select state via serial, but I cannot set wanted state variable. See the code below.

Any hint very much appreciated.

void setup() {
  
  //Setup Channel A
  pinMode(DIR_A,OUTPUT); //Initiates Motor Channel A pin
  pinMode(BRAKE_A, OUTPUT); //Initiates Brake Channel A pin

  //Setup Channel B
  pinMode(DIR_B, OUTPUT); //Initiates Motor Channel B pin
  pinMode(BRAKE_B, OUTPUT);  //Initiates Brake Channel B pin
  
  //MotorSpeed = 0;
  
  // Open Serial communication
  Serial.begin(9600);
  Serial.println("Your car is ready for your orders My Master!");
}

void loop() {
    //if some data are sent, reads it and saves it into state
    if(Serial.available() > 0){    
      state = Serial.read();   
      flag=0;
    }   
    // if the state is '0' the DC motor will turn off
    if (state == '0') {
        digitalWrite(BRAKE_A, HIGH); // Set brake enable on Channel A 
        digitalWrite(BRAKE_B, HIGH); // Set brake enable on Channel B
        if(flag == 0){
          Serial.println("Motors are off, My Master");
          flag=1;
        }
    }
    // if the state is '1' the car go stright
    else if (state == '1') {
              
                while(Serial.available() > 0 ){};

                MotorSpeed = Serial.read();  
               
               digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake on A Channel
               digitalWrite(DIR_A, LOW);            // setting direction to HIGH the motor will spin forward on A Channel
               analogWrite(PWM_A, MotorSpeed);     // Set the speed of the motor, 255 is the maximum value on A Channel
        
               digitalWrite(BRAKE_B, LOW);  // setting brake LOW disable motor brake on B Channel
               digitalWrite(DIR_B, HIGH);   // setting direction to HIGH the motor will spin forward on B Channel
               analogWrite(PWM_B, MotorSpeed);     // Set the speed of the motor, 255 is the maximum value on B Channel
     
          if(flag == 0){
          Serial.println("Moving forward, My Master");
          flag=1;        
        }
         Serial.println(MotorSpeed);
    }
    // if the state is '2' the car go backwards
    else if (state == '2') {
        while(Serial.available() > 0 ){}; 
        
        MotorSpeed = Serial.read();
        
        digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake on A Channel
        digitalWrite(DIR_A, HIGH);   // setting direction to LOW the motor will spin backwards on A Channel
        analogWrite(PWM_A, MotorSpeed);     // Set the speed of the motor, 255 is the maximum value on A Channel
        
        digitalWrite(BRAKE_B, LOW);  // setting brake LOW disable motor brake on B Channel
        digitalWrite(DIR_B, LOW);   // setting direction to LOW the motor will spin backwards on B Channel
        analogWrite(PWM_B, MotorSpeed);     // Set the speed of the motor, 255 is the maximum value on B Channel
       
        if(flag == 0){
          Serial.println("Moving backwards, My Master");
          flag=1;
        }
         Serial.println(MotorSpeed);
         state = 9;
    }

I can select state via serial, but I cannot set wanted state variable. See the code below.

I saw the code. I don't understand the problem. What state variable can't you set?

The post title says something about motor speed, which you seem to be reading here:

                MotorSpeed = Serial.read();

The read() method returns one byte. That is usually a character (digit), unless the sender is sending binary data.

So, either this is not what you expect, or the sender s indeed sending binary data. I have no idea which case is true.

If the sender is sending ASCII data, setting the motor speed to '1' or 'A' or 'f', makes little sense.