Simple RC robot

Hello,

I’ve been trying to make an rc robot/car using arduino that reads values from two channels on an rc receiver and maps those values to the left side motors and right side motors.

ex. The left joy stick will control the speed of the left motors and the right joystick will control the speed of the right motors.

However, the arduino seems slow to respond to any rc controller input. Sometimes the left side speed will update but the arduino will ignore any right side input. Sometimes, when moving both joysticks forward, only one side will respond while the other side fails to begin moving until many seconds later.

I am 99% sure the problem exists in the code as I have very little experience in programming. My code is attached.

Steve_code_test2.ino (1.37 KB)

More members will see your code if posted properly. Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

To save time as I was intrigued I’ve posted the code here

int enA = 9;
int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;
int enB = 8;
int channel3 = 3;
int channel2 = 2;

void setup() {
  // put your setup code here, to run once:
pinMode(channel3, INPUT);
pinMode(channel2, INPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
int ch3 = pulseIn(channel3, HIGH, 250000);
int ch2 = pulseIn(channel2, HIGH, 250000);

if(ch3 > 1600){
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        speedleft = map(ch3, 1600, 2000, 0, 255);
        analogWrite(enA, speedleft);
}
else if(ch3 < 1300){
       digitalWrite(in1, LOW);
       digitalWrite(in2, HIGH);
       speedleft = map(ch3, 1300, 995, 0, 255);
       analogWrite(enA, speedleft);
}
// comment by JE:
//this will only execute if ch3 < 1600 && ch3 >1300

else if(ch2 > 1600){
       digitalWrite(in3, HIGH);
       digitalWrite(in4, LOW);
       speedright = map(ch2, 1600, 2000, 0, 255);
       analogWrite(enB, speedright);
}
else if(ch2 < 1300){
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
       speedright = map(ch2, 1300, 995, 0, 255);
       analogWrite(enB, speedright);
}
else{
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      digitalWrite(enA, 0);
    }
  
}

I found it a bit confusing that in1 … in4 are defined as outputs

While many will say code should be self-documenting I think you would find it helpful to add comments.

Perhaps mine above will explain your issue?

Here is my code updated with comments and posted properly.

I found it a bit confusing that in1 … in4 are defined as outputs

These are outputs because they are the pins that are outputting data to the h-bridge motor controller. These 4 pins are used to control the motor’s direction.

Here is my code:

//defining the ports used to send data to the h-bridge motor controller
int enA = 9;
int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;
int enB = 8;

//variables used for the speed of the left and right motors
int speedleft = 0;
int speedright = 0;

//defines the pins used to read data from the radio receiver
int channel3 = 3;
int channel2 = 2;

void setup() {
//data coming in from the radio reciever
  pinMode(channel3, INPUT);
  pinMode(channel2, INPUT);

//data going out to the h-bridge controller  
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  
  Serial.begin(9600);
}

void loop() {

//reads the pulses from the radio reciever 
  int ch3 = pulseIn(channel3, HIGH, 250000);
  int ch2 = pulseIn(channel2, HIGH, 250000);


//left side forward and reverse speed control
  if (ch3 > 1600) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    speedleft = map(ch3, 1600, 2000, 0, 255);
    analogWrite(enA, speedleft);
  }
  else if (ch3 < 1300) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    speedleft = map(ch3, 1300, 995, 0, 255);
    analogWrite(enA, speedleft);
  }

//right side forward and reverse speed control
  else if (ch2 > 1600) {
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    speedright = map(ch2, 1600, 2000, 0, 255);
    analogWrite(enB, speedright);
  }
  else if (ch2 < 1300) {
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    speedright = map(ch2, 1300, 995, 0, 255);
    analogWrite(enB, speedright);
  }
  else {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(enA, 0);
  }







}

Work carefully through the logic of all those else ifs. Remember that it only goes on to the else if the test in the if before it was not true. Your ch2 ifs should NOT be dependent on the value of the ch3 read. But they are now.

Steve