How to code this situation

I’m trying to program a Walking robot using a Arduino UNO R3, L298N driver module, and a HC-05 bluetooth
module.

This is my code:

int enA = 9;
int in1 = 4;
int in2 = 5;
int enB = 10;
int in3 = 6;
int in4 = 7;

int val = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //Read input from bluetooth module:
  if ( Serial.available())
  {
    val = Serial.read();
  }


  //Input key switch
  switch (val) {
    case '0':
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      //Code when no key is pressed
      break;
    case '1':
      // Set Motor A forward
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      // Set Motor B forward
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);//Code when UP key is pressed
      for (int i = 70; i < 256; i++)
      {

        analogWrite(enA, i);
        analogWrite(enB, i);
        Serial.println("<Forward>");
        if (i = 254)
        {
          i = 254;
        }
      }

      Serial.println("<Setting Motor Forward>");
      break;
    case '2':
      // Set Motor A backward
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      // Set Motor B backward
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
      for (int i = 70; i < 256; i++)
      {

        analogWrite(enA, i);
        analogWrite(enB, i);
        delay(5000);
        Serial.println("<Backward>");
      }

      //Code when DOWN key is pressed
      Serial.println("<Backward>");
      break;
    case '3':
      analogWrite(enA, 180); //speed 0 - 255
      analogWrite(enB, 180); //speed 0 - 255
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
      Serial.println("<Left>");
      //Code when LEFT key is pressed
      break;
    case '4':
      //Code when RIGHT key is pressed
      analogWrite(enA, 180); //speed 0 - 255
      analogWrite(enB, 180); //speed 0 - 255
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);
      Serial.println("<right>");
      break;
    case '5':
      //Code when X key is pressed
      analogWrite(enA, 0);
        
      break;
    case '6':
      //Code when O key is pressed
      analogWrite(enB, 0);
      break;
    default:
      analogWrite(enA, 255);
        analogWrite(enB, 255);
      // default code (should never run)
  }
}

I have this problem. First when I send the command 1 my motor runs forward and when I give another command say 2 the motor doesnot change backward.

How to code this pls help me…

I have attached the ino file below…

BT_controller.ino (2.49 KB)

I dont see a smoking gun. Does it work if you dont actually connect the motors? (Maybe use a pair of leds (with series resistors) in place of motor, one connected with each polarity, so one led woukd go on if it would he rotating forward, and the other if it would be rotating in reverse. That will tell you if its a code problem or hardware problem (noise from the motors getting onto power rail (inductive loads do that) or EMI)

Many people recommend using a separate power supply for inductive loads like motors.

if (i = 254)
        {
          i = 254;
        }

Oops two times!

= is assignment, == is testing a condition.
There is no point in testing a condition or assigning a value when it is followed up by assigning the same value.

Also: the switch..case gets run whether a character is received or not, but the variable val does not get changed.

Lines such as delay(5000) unnecessarily slow down the program and waste the considerable power of the Arduino.

Why analogWrite is being used on pins called enA and enB is beyond me. Is this intended to control the speed?

I don't know that these things will cause the observed behavior but they look suspicious.

vaj4088:
...
Why analogWrite is being used on pins called enA and enB is beyond me. Is this intended to control the speed?
...

yes, i recognize that code, it's a 6-pin L298N motor driver.
in1,in2 controls the direction for M1(orA); in3,in4 for M2(orB), and analogWrite is for the PWM speed on the respective motors.

for OP, you need more robust code on Serial input than just;

  if ( Serial.available())
  {
    val = Serial.read();
  }

have a look at Robin2’s tutorial on Serial Input basics, i was where you are now on a similar project and learning the basics of Serial.available and Serial.read (which is valid on Bluetooth as well) was very necessary.

also, for ;

int enA = 9;

and the others, they are pin assignments and not variables, so you can use the const keyword, and learn to use byte for values that are less than 256 - you save memory usage.
so, it should be;

const byte enA = 9;

and same for the others.