I just just bought the arduino and iv been reading some books and throwing different things into the coding but im kinda stuck. I have no coding experience, only been reading and googleling to try to understand more so forgive me if i don't understand everything.
So i started building a remote controlled car using IR sensor as a remote. But i seem to be having some problems. It listens to the first input press of the remote but after that everything else is ignored. As soon as i restart it it would do the same again. Also It starts off with the wheels moving which i was hoping it wouldn't at least until i actually pressed a button on the remote. I have a remote that throws out steady numbers so i don't believe its a problem with that. Its most likely my lack of knowledge so if anyone can help me and see what i did wrong it would be greatly appreciated. Also feel free to throw guides at me or name of books i should read that could help.
Thank You!
#include <Servo.h>
#include <IRremote.h>
IRrecv irrecv(3);
decode_results results;
const int leftMotorPin = 10;
const int rightMotorPin = 11;
Servo rightServo;
Servo leftServo;
int rightSpeed = 90;
int leftSpeed = 90;
long keyCode = 0;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); //This Starts the IR Receiver//
leftServo.attach(10);
rightServo.attach(11);
pinMode(rightMotorPin, OUTPUT);
pinMode(leftMotorPin, OUTPUT);
}
void loop()
{
if(irrecv.decode(&results))
{
keyCode=results.value;
if(keyCode != -1)
{
switch (keyCode)
{
case 2774139390: //Code from IR Remote
Serial.println("Forward");
leftSpeed-=1;
rightSpeed+=1;
break;
case 2774172030: //Code from IR Remote
Serial.println("Backward");
leftSpeed+=1;
rightSpeed-=1;
break;
case 2774147550: //Code from IR Remote
Serial.println("Stop");
leftSpeed=90;
rightSpeed=90;
break;
case 2774188350: //Code from IR Remote
Serial.println("Turn Left");
leftSpeed-=1;
rightSpeed-=1;
break;
case 2774155710: //Code from IR Remote
Serial.println("Turn Right");
leftSpeed+=1;
rightSpeed+=1;
break;
case 2774162085: //Code from IR Remote
Serial.println("Turbo");
leftSpeed=leftSpeed-50;
rightSpeed=rightSpeed+50;
break;
}
}
}
updateMotors();
delay(10);
}
void updateMotors()
{
leftServo.write(leftSpeed);
rightServo.write(rightSpeed);
}
ir_car3.ino (1.79 KB)