I am fairly new to Arduino programming. I want to be able to make an IR controlled car. My IR sensor and all motors work and I have used a decoder program to collect the codes from the buttons: front, left, right. The problem is that in the Serial Monitor, I have the code received printed out on the screen, but at random times, random code symbols (I believe it is machine code) are printed and the motors stop or hesitate. How do I stop that from happening and make the motors move smoothly? Here is the code:
#include <IRremote.h>
//The codes from the remote(pre recorded)
#define forward 16718055
#define right 16734885
#define left 16716015
int IRpin = 11; // infrared reciever pin
IRrecv irrecv(IRpin);
decode_results results;
int leftMotor = 4; //left motor pin
int rightMotor = 5; //right motor pin
long IReceived = 0; //Holds IR data
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); //Start the receiver
pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
}
void loop() {
if (irrecv.decode(&results) == true){ //If a signal is recieved
IReceived = results.value; //Transfer the signal code to a long variable
Serial.print("Received... " + (IReceived));
irrecv.resume(); //Resume IR reading
}
switch (IReceived){
case forward: //If the forward code is received
digitalWrite(rightMotor, HIGH);
digitalWrite(leftMotor, HIGH);
Serial.println("Forward");
break;
case right: //If the right code is received
digitalWrite(rightMotor, LOW);
digitalWrite(leftMotor, HIGH);
Serial.println("Right");
break;
case left: //If the left code is received
digitalWrite(rightMotor, HIGH);
digitalWrite(leftMotor, LOW);
Serial.println("Left");
break;
default: //If any other code is received
digitalWrite(rightMotor, LOW);
digitalWrite(leftMotor, LOW);
}
}