Regarding Arduino UNO , L298N to control 12V motor issue

Hello

i built a radio control tank that controlled by Arduino UNO. It is working properly except for turning left and right.

My tank is two motors type. Tank can move forward and backward without any problem. i have tested following setup for turning left or right

for turn left -> left motor move backward and right motor move forward

for turn right -> left motor move forward and right motor move backward

or

for turn left -> right motor move forward

for turn right -> left motor move forward

However, the tank keep moving like 1mm travel distance for turning left or right. It does not make any sense because it worked fine to move forward and backward. However, it is not working properly for turning left or right.
Anyone experienced same issue as me. If so, could anyone please give me hints for resolving this problem.

My code show as below


#include <Servo.h>

const int RCH_1_PIN = 10;                 /*Define varible RCH_1_PIN as Arduino PIN 10*/ 
const int RCH_3_PIN = 9;                  /*Define variable RCH_3_PIN as Arduino PIN 9*/
const int RCH_2_PIN = 11;                 /*Define variable RCH_2_PIN as Arduino PIN 11*/

int val0 = 0;
int val90 = 90;
int val180 = 180;
const int IN1 = 2;                    /*Define variable A_2_PIN as Arduino PIN2*/ 
const int IN2 = 3;                    /*Define variable A_3_PIN as Arduino PIN3*/
const int IN3 = 5;                    /*Define varialbe A_5_PIN as Arduino PIN5*/
const int IN4 = 6;                    /*Define variable A_6_PIN as Arduino PIN6*/
int ch1 = 0;                          /*Initialize ch1*/
int ch2 = 0;                          /*Initialize ch2*/  
int ch3 = 0;                          /*Intitialize ch3*/



void setup() {
  // put your setup code here, to run once:

    
   Servo servoMotor1;                     /*Create servo obect to control servo motor*/

   int posServo1 = 0;                     /*Define variable for the position of the servo*/
   
   servoMotor1.attach(12);                /*Servo1 attached to Arduino PIN 12*/

   pinMode(RCH_1_PIN, INPUT);             /*Set Arduino PIN 10 as INPUT for receive signal from RC receiver CH1*/

   pinMode(RCH_3_PIN, INPUT);             /*Set Arduino PIN 9 as INPUT for receive signal from RC receiver CH3*/

   pinMode(RCH_2_PIN, INPUT);             /*Set Arduino PIN 11 as INPUT for receive signal from RC receiver CH2*/

   pinMode(IN1, OUTPUT);                 /*Set Arduino PIN 2 as OUTPUT to control L298N driver module IN1*/

   pinMode(IN2, OUTPUT);                 /*Set Arduino PIN 3 as OUTPUT to control L298N driver module IN2*/

   pinMode(IN3, OUTPUT);                 /*Set Arduino PIN 5 as OUTPUT to control L298N driver module IN3*/

   pinMode(IN4, OUTPUT);                 /*Set Arduino PIN 6 as OUTPUT to control L298N driver module IN4*/

   Serial.begin(9600);

   /*MOTOR turn OFF in initial stage*/
   
   //IN1 and IN2 for motor 1 
   digitalWrite(IN1, LOW);
   digitalWrite(IN2, LOW);

   //IN3 and IN4 for motor 2 
   digitalWrite(IN3, LOW);   
   digitalWrite(IN4, LOW);
   
}

void loop() {
  // put your main code here, to run repeatedly:

   Servo servoMotor1;
   int posServo1 = 0;
   ch1 = pulseIn(RCH_1_PIN, HIGH, 25000);

   Serial.print("Channel 1:");
   Serial.println(ch1);

   ch3 = pulseIn(RCH_3_PIN, HIGH, 25000);

   Serial.print("Channel 3:");
   Serial.println(ch3);

   ch2 = pulseIn(RCH_2_PIN, HIGH, 25000);

   Serial.print("Channel 2:");
   Serial.println(ch2);
   

   delay(100);

   /*RC controller RIGHT HAND side stick (NEUTRAL, LEFT AND RIGHT)*/
   if((ch1 >= 1450) && (ch1 <= 1550))          /*tank stopped*/
   {

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);

      delay(10);

   }
   else if (ch1 <1450)                      /*tank turn turn left*/
   {

      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);

      delay(10);  
   }
   else if (ch1 > 1550)                     /*tank turn right*/
   {

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH); 
      digitalWrite(IN3, HIGH);    
      digitalWrite(IN4, LOW); 

      delay(10);
   }
   /*else
   {

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);

      delay(10);
   }*/

   /*RC controller LEFT HAND SIDE stick (NEUTRAL, FORWARD AND BACKWARD)*/
   
   if ((ch3 >=1450) && (ch3 <=1550))        /*tank stopped*/
   {
   
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);

      delay(10);
    
   }
   else if((ch3 > 1550) && (ch3 < 2000))                          /*tank move forward*/
   {

      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);

      delay(10); 
   }
   else if((ch3 > 1000) && (ch3 < 1450))                          /*tank move backward*/
   {

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);    

      delay(10);
   }
   else
   {

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);

    
   }

   /*RC controller RIGHT HAND SIDE stick (NEUTRAL, FORWARD AND BACKWARD)*/
   
   if ((ch2 >=1450) && (ch2 <=1550))     /*servo in centre position*/
   {

      servoMotor1.write(val0);

      delay(10);
   }
   else if (ch2 > 1550)                       /*Turn servo 90 degree*/
   {

      servoMotor1.write(val90);

      delay(10);
   }
   else if (ch2 < 1450)
   {

      servoMotor1.write(val180);              /*Turn servo 180 degree*/

      delay(10);
    
   }
   else
   {

      servoMotor1.write(val0);               /*servo in centre position*/

      delay(10);

      
   }
}

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

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