Arduino not executing the right instruction

Hi Good day :). I am working on a arduino bluetooth car. I used hc-05 bluetooth module and l298n motor driver. I also used a custom app connected to the hc-05. I attached an LED as a control. The problem is I can turn the LED on and off but when I try to click a button on my app the motor runs but not correctly. The serial monitor shows the correct character that my app sends. Here is a table of what it does.

Forward button = moves low power clockwise
Brake button = moves mid power anti-clockwise
Reverse button = moves high power anti-clockwise
*For the left and right I used a servo motor to the front wheels

I tried reconfiguring the app by switching the buttons but it still had the same effect. The app sends a single digit coresponding to the instruction.

Forward = 1
Backward = 4
Reverse = 7

Here is the code, I did not add the led and servo to make it simple.

int enA = 5;
int in1 = 7;
int in2 = 8;
char data;

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

void loop()
{
if(Serial.available()>0){
    data = Serial.read();
    Serial.print(data); //so I can see it in the serial monitor
}

switch(data){

case '1': //Forward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    analogWrite(enA, 255);
    
case '4': //Brake
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    analogWrite(enA, 255);

case '7': //Reverse
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(enA, 255);
}
}

Looking forward to your reply. :slight_smile: :slight_smile: :slight_smile:

I'd have to guess that your wiring is wrong. If you simplify that program to just go forward 2 seconds, stop 2 seconds then reverse 2 seconds does that work correctly? E.g.

void loop()
{
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    analogWrite(enA, 255);
   
    delay(2000);

    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    analogWrite(enA, 255);

    delay(2000);

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(enA, 255);

    delay(2000);
}

Steve

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