I'm new to Arduino "C" coding and need full sketch for IR remote rover car please!

Hi!
I just started with Arduino and built a mini rover from DFRobotShop. I also purchased the shield that goes with this project. I followed the instructions and everything works fine. I was even able to get all the different codes from a remote control to use with the rover. The problem I have is that the instructions only provide a sample code that control the car going forward (with the push of a button from the remote). However, the code stops at that and I don’t know how to continue writing the code on the same sketch to make at least four other buttons work (i.e., backward, turn right, turn left, stop)
Here’s the sketch code (notice the whole block to make one button work; I need the code for the other four buttons (i.e., where do I start copying and modifying the code and where do I end?):

#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
void setup()
{
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
if (irrecv.decode(&results))
{
if(results.value == 16748655) // This code will vary based on your remote control
{
analogWrite (E1,255);
digitalWrite(M1,HIGH);
analogWrite (E2,255);
digitalWrite(M2,HIGH);
delay(1000);
}
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
irrecv.resume(); // Receive the next value
}
}

Thank you so much in advance for your help!

We assume you mean this kit?

You’ll likely need to do a bit of learning to use the other buttons / arrows. Each application is different, as well as each IR remote.
Your first task will be to use the IR detection sample program to get the codes for each of the buttons you want to use on the IR remote control.
Keep a note of these somewhere. The sample code is under “Determining IR Codes”:

Next, you need to merge the IR code with the w/a/s/d movement code. Rather than waiting for a serial command, the code will need to be listening for specific IR codes. It’s up to you if you want to add some lines to equate the IR code with one of the default letters w/a/s/d or rewrite the program to use the IR codes.

Unfortunately don’t have the time to create the code for you, but hope this helps give you a start. Perhaps another member might be able to offer some additional tips / tricks.

Hello there @norbac

To sum up what @cbenson said what you have to do is pretty much merging the Motor Controller W/A/S/D Computer Control Code and the Sample IR code w/ Motor Control.

Something like this:

#include <IRremote.h>
#include <IRremoteInt.h>

int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

void setup(){
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

void loop(void){
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
if (irrecv.decode(&results)){
switch(results.value){ // Perform an action depending on the command
case ‘a’://Move Forward // Change a for the corresponding value
forward (leftspeed,rightspeed);
break;
case ‘s’://Move Backwards // Change s for the corresponding value
reverse (leftspeed,rightspeed);
break;
case ‘a’://Turn Left // Change a for the corresponding value
left (leftspeed,rightspeed);
break;
case ‘d’://Turn Right // Change d for the corresponding value
right (leftspeed,rightspeed);
break;
default:
stop();
break;
}
irrecv.resume(); // Receive the next value
}
}

void stop(void){
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}

void forward(char a,char b){
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}

void reverse (char a,char b){
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}

void left (char a,char b){
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}

void right (char a,char b){
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}

What I mean with “the corresponding values” are the specific IR codes for each direction (like the 16748655 from the original code), you can find those by using the Determining IR Codes of the user guide.

I just merged the two codes but I may have missed something while doing it so in case it doesn’t work try to look at the two original codes. I hope that can help you out, let us know how it goes :wink:

1 Like

Thank you so much Geraldine! It worked like a charm! I had to do a few minor adjustments to one of the motors (i.e., reverse polarity) and some to the code, but other than that the code did work! Now the little rover can mover forward, backward, turn left & right! Thanks again! :smiling_face_with_three_hearts::smiling_face_with_three_hearts:

1 Like