Motors don't work with hc-05

I am making an RC car with hc-05 but hc-05 doesn't always take input so motors don't turn on. The problem isn't with the circuit because the motors turn on when I don't use if(). in Serial Monitor I see the number I sent from my phone but there is no text "forward".
here is my code:

#include <SoftwareSerial.h>
SoftwareSerial EEBlue(2, 3); // RX | TX
const int forwardPin = 8;     //motor1_m
const int backwardPin = 12;   //motor1_p
   
void setup()
{
   
  Serial.begin(9600);
  EEBlue.begin(9600);  //Default Baud for comm, it may be different for your Module. 
  Serial.println("The bluetooth gates are open.");

  pinMode(forwardPin,OUTPUT);
  pinMode(backwardPin,OUTPUT);
  pinMode(forwardPin2,OUTPUT);
  pinMode(backwardPin2,OUTPUT);
}
     
void loop()
{
   
  // Feed any data from bluetooth to Terminal.
  if (EEBlue.available()){
    Serial.write(EEBlue.read());
    if(EEBlue.read() == 1){
      Serial.println("forward");
      digitalWrite(8, HIGH);
      digitalWrite(12, LOW);
    }
  }
  // Feed all data from termial to bluetooth
  if (Serial.available()){
     EEBlue.write(Serial.read());
  }
}

I found most of this code online and do not claim it as my own.

After you have read a character, it is gone from the buffer.

Read the character and store it in a variable. Use that variable for the echo and the if statement.

The data that you read may be a character so that should be
if(EEBlue.read() == '1'){

In most cases you will be reading an ascii character '1' and not the numerical value 1 and if that is the case, then the if statement should vbe:

if(EEBlue.read() == '1'){

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.