Creating an Arduino Mega Four Wheels Car

Dear all,
I am trying to make an arduino based four wheel small car while controlling it using L298N motor drive. Since L298N control two motors, I have connected the two right motors together to one side and the two left motors to the other side. I wanted to test the car movement with commands. I understand that I have switched the connection for one side, but it is ok I figured it out. I started coding and wanted to test the movement so I gave it the command to go forward at a time then another code for reverse, going right, and going left. They all worked great alone, but when I tried to create a code to test them all together while delaying 1 sec for each movement for some reason it just goes forward and again and again. I tried to do serial print to see if it actually run through the other functions of reverse, right, and left turns and it does. I also tried to add a "stop" command between them but it still just goes forward, stops, and again goes forward. The code created is as shown below:

#define LM_1 53//1
#define LM_2 52 //2
#define RM_1 50 //3
#define RM_2 51 //4
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LM_1, OUTPUT);
  pinMode(LM_2, OUTPUT);
  pinMode(RM_1, OUTPUT);
  pinMode(RM_2, OUTPUT);
 
}

void loop() {
  // put your main code here, to run repeatedly:
   Stop();
  Forward();
  delay(1000);
  Stop();
  Reverse();
  delay(1000);
   Stop();
  Left();
  delay(1000);
   Stop();
  Right();
  delay(1000);
}
  void Forward(){
   digitalWrite(LM_1, LOW);
    digitalWrite(LM_2, HIGH);
       digitalWrite(RM_1, HIGH);
    digitalWrite(RM_2, LOW);
    Serial.print("F");
  }
  void Reverse(){
 digitalWrite(LM_1, HIGH);
    digitalWrite(LM_2, LOW);
       digitalWrite(RM_1, LOW);
    digitalWrite(RM_2, HIGH);
    Serial.print("R");
  }
 void Left(){
  digitalWrite(LM_1, LOW);
    digitalWrite(LM_2, HIGH);
       digitalWrite(RM_1, LOW);
    
   digitalWrite(RM_2, HIGH);
   Serial.print("L");
 }
  void Right(){
  digitalWrite(LM_1, HIGH);
    digitalWrite(LM_2, LOW);
       digitalWrite(RM_1, HIGH);

   digitalWrite(RM_2, LOW);
   Serial.print("RI");
  }
void Stop(){
   digitalWrite(LM_1, LOW);
    digitalWrite(LM_2, LOW);
       digitalWrite(RM_1, LOW);
    digitalWrite(RM_2, LOW);
}

It goes through them all but never acts. Could someone please help me finding what went wrong and how to fix it. Thank you for your time and effort.

Do you mean that if you empty the loop() and just put Left(); or Right(); or Forward(); or Reverse(); at the end of the set up you get the right behavior

How are the motors powered? Can you describe the wiring in details?

J-M-L:
Do you mean that if you empty the loop() and just put Left(); or Right(); or Forward(); or Reverse(); at the end of the set up you get the right behavior

How are the motors powered? Can you describe the wiring in details?

Yes exactly if I commented all the other function and ran one function at a time I get the right behavior.
The circuit is something like this but instead of an arduino uno I am using arduino mega where I connected the IN1-IN4 to pins 53,52,50,51 (sorry for the bad drawing) :
I am also using a 9V battery instead of 12V but the car runs and the arduino is functioning as well. Would that be an issue? If so could you please explain why or how?

If It is not some sort of super powerful lithium ion battery in the shape of the poorly spaced 9V typical battery, then yes it’s likely the issue, not enough power to drive the motors correctly . Try with a 9V wall adapter first

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