RC Car can only steer or drive and not both

I'm trying to use an UNO board and an L298N to operate an rc car with an rc receiver/transmitter(Flysky FS-GT2B) with two motors to both drive and steer/pivot.

At first I thought the code would just have the car doing 1 at a time, but this code will only let me do one task indefinitely:

#include <Servo.h>
#define rc1 A0 //rc channel 1 pin A0, steering channel
#define rc2 A1 //rc channel 2 pin A1, driving channel
#define en1 3  //motor 1 enable pin 3
#define in1 4  //motor 1 in1 pin 4
#define in2 5  //motor 1 in2 pin 5
#define in3 8  //motor 2 in3 pin 8
#define in4 9  //motor 2 in4 pin 9
#define en2 10  //motor 2 enable pin 10

int ch1; //channel 1 receiver value, steering
int ch2; //channel 2 receiver value, driving
int spd; //motor speed
int dir; //motor steering

void setup() {
  // put your setup code here, to run once:
pinMode(rc1, INPUT); //set ch10 and ch11 as input
pinMode(rc2, INPUT);
pinMode(en1, OUTPUT); //set h-bridge pins as output
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(en2, OUTPUT);
digitalWrite(in1, LOW); //set directional pins off (no movement)
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.begin(9600); //to test
}

void loop() {
  // put your main code here, to run repeatedly:
ch1 = pulseIn(rc1, HIGH); //turn on channel 1
ch2 = pulseIn(rc2, HIGH); //turn on channel 2
//Serial.print("ch1 ");
//Serial.println(ch1);
//Serial.print("ch2 ");
//Serial.println(ch2);
steer();
drive();
delay(500);
}

void drive() {
  
  if (ch2 < 1450) {
    spd = map(ch2, 2000, 1000, 25, 255);
    analogWrite(en1, spd);
    analogWrite(en2, spd);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    Serial.print(spd); 
    Serial.print("spd \n");
  } 
  else if(ch2 > 1550) {
    spd = map(ch2, 1000, 2000, 25, 255);
    analogWrite(en1, spd);
    analogWrite(en2, spd);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    Serial.print(spd); 
    Serial.print("spd \n");
  } 
  else if (ch2 < 1550 && ch2 > 1450) { 
    spd = 0;
    analogWrite(en1, spd);
    analogWrite(en2, spd);
    Serial.print(spd); 
    Serial.print("spd \n");
  }

}

void steer() {
  
  if (ch1 < 1450) {
    dir = map(ch1, 2000, 950, 25, 255);
    analogWrite(en1, dir);
    analogWrite(en2, dir);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    Serial.print(dir); 
    Serial.print("dir \n");
  }
  else if (ch1 > 1550) {
    dir = map(ch1, 1000, 2000, 25, 255);
    analogWrite(en1, dir);
    analogWrite(en2, dir);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    Serial.print(dir);
    Serial.print("dir \n");
  }
  else if (ch1 < 1550 && ch1 > 1450) { 
    dir = 0;
    analogWrite(en1, dir);
    analogWrite(en2, dir);
    Serial.print(dir); 
    Serial.print("dir \n");
  }
  
}

With this, I can only drive but not steer. If I swap where I write the steer and drive function in my loop, then the opposite happens--I only steer and can't drive. If I try to do the task that it can't do, I hear faint clicking noises from my motor at the same time intervals as my delay.

I noticed that if I put a delay between the two functions then they can both work but at the cost of them starting and stopping.

I'm honestly not too sure how to go about this and I really couldn't find anything regarding something like this that would help me other than other projects that incorporated other things outside of this project.

If somebody could help shed some light on this problem and guidance on how I can get started to troubleshoot, that'd be really great.

Fuse both functions with a speed and an angle parameter. Angle like the angle of your steering wheel. Make the car e.g. on angle 0 run straight away, on positive angle drive right and on negative angle drive left. The radius of the curve depends in your code on different values for en1 and en2.

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