1 /* Arduino Projects for Dummies
4 * Chapter 14: Building a Remote Controlled Car
5 * Detects signals from a garden variety remote control
6 * to drive two servo motors.
8 * Uses the standard Arduino servo library and
9 * the IR Remote Library by Ken Shirriff:
10 * https://github.com/shirriff/Arduino-IRremote
11 * Adapted from code by Michael Margolis
19 const int irReceivePin = 2; // pin connected to IR detector output
20 IRrecv irrecv(irReceivePin); // create the IR library
21 decode_results results; // IR data goes here
23 const int rightMotorPin = 9; // Connected to right servo
24 const int leftMotorPin = 10; // Connected to left servo
34 boolean DEBUG = false; // set to true if you want to display the Debug output
42 irrecv.enableIRIn(); // Start the IR receiver
45 rightServo.attach(10);
47 pinMode(rightMotorPin, OUTPUT);
48 pinMode(leftMotorPin, OUTPUT);
52 if( irrecv.decode(&results) )
55 keyCode=results.value;
59 case 50174055: // Replace this code with the one from your remote!
60 Serial.println("Forward");
61 leftSpeed-=1; // Opposite values propel the wheels forward
65 case 50182215: // Replace this code with the one from your remote!
66 Serial.println("Backward");
67 leftSpeed+=1; // Opposite values propel the wheels backward
71 case 50168955: // Replace this code with the one from your remote!
72 Serial.println("Stop");
73 leftSpeed=90; // A value of 90 stops the servos from turning
77 case 50152125: // Replace this code with the one from your remote!
78 Serial.println("Turn Left"); // Wheels move in opposite directions
83 case 50135805: // Replace this code with the one from your remote!
84 Serial.println("Turn Right"); // Wheels move in opposite directions
89 case 50139885: // Replace this code with the one from your remote!
90 Serial.println("TURBO!!"); // need to move left servo to go right
91 leftSpeed=leftSpeed-50;
92 rightSpeed=rightSpeed+50;
98 Serial.println(keyCode);
100 Serial.print(leftSpeed);
102 Serial.println(rightSpeed);
106 irrecv.resume(); // Receive the next value
113 void showReceivedData()
115 if (results.decode_type == UNKNOWN)
117 Serial.println("-Could not decode message");
121 if (results.decode_type == NEC) {
122 Serial.print("- decoded NEC: ");
124 else if (results.decode_type == SONY) {
125 Serial.print("- decoded SONY: ");
127 else if (results.decode_type == RC5) {
128 Serial.print("- decoded RC5: ");
130 else if (results.decode_type == RC6) {
131 Serial.print("- decoded RC6: ");
133 Serial.print("Value = ");
134 Serial.println( results.value, DEC);
139 leftServo.write(leftSpeed);
140 rightServo.write(rightSpeed);