/* Arduino Projects for Dummies * by Brock Craft * * Chapter 14: Building a Remote Controlled Car * Detects signals from a garden variety remote control * to drive two servo motors. * * Uses the standard Arduino servo library and * the IR Remote Library by Ken Shirriff: * https://github.com/shirriff/Arduino-IRremote * Adapted from code by Michael Margolis * * v0.1 30.04.2013 */ #include #include const int irReceivePin = 2; // pin connected to IR detector output IRrecv irrecv(irReceivePin); // create the IR library decode_results results; // IR data goes here const int rightMotorPin = 9; // Connected to right servo const int leftMotorPin = 10; // Connected to left servo Servo rightServo; Servo leftServo; int rightSpeed=90; int leftSpeed=90; long keyCode=0; boolean DEBUG = false; // set to true if you want to display the Debug output void setup() { if(DEBUG){ Serial.begin(9600); } irrecv.enableIRIn(); // Start the IR receiver leftServo.attach(9); rightServo.attach(10); pinMode(rightMotorPin, OUTPUT); pinMode(leftMotorPin, OUTPUT); } void loop() { if( irrecv.decode(&results) ) { keyCode=results.value; if(keyCode != -1) { switch (keyCode){ case 50174055: // Replace this code with the one from your remote! Serial.println("Forward"); leftSpeed-=1; // Opposite values propel the wheels forward rightSpeed+=1; break; case 50182215: // Replace this code with the one from your remote! Serial.println("Backward"); leftSpeed+=1; // Opposite values propel the wheels backward rightSpeed-=1; break; case 50168955: // Replace this code with the one from your remote! Serial.println("Stop"); leftSpeed=90; // A value of 90 stops the servos from turning rightSpeed=90; break; case 50152125: // Replace this code with the one from your remote! Serial.println("Turn Left"); // Wheels move in opposite directions leftSpeed-=1; rightSpeed-=1; break; case 50135805: // Replace this code with the one from your remote! Serial.println("Turn Right"); // Wheels move in opposite directions leftSpeed+=1; rightSpeed+=1; break; case 50139885: // Replace this code with the one from your remote! Serial.println("TURBO!!"); // need to move left servo to go right leftSpeed=leftSpeed-50; rightSpeed=rightSpeed+50; break; } if(DEBUG){ Serial.println(keyCode); showReceivedData(); Serial.print(leftSpeed); Serial.print(", "); Serial.println(rightSpeed); } } irrecv.resume(); // Receive the next value } updateMotors(); delay(10); } void showReceivedData() { if (results.decode_type == UNKNOWN) { Serial.println("-Could not decode message"); } else { if (results.decode_type == NEC) { Serial.print("- decoded NEC: "); } else if (results.decode_type == SONY) { Serial.print("- decoded SONY: "); } else if (results.decode_type == RC5) { Serial.print("- decoded RC5: "); } else if (results.decode_type == RC6) { Serial.print("- decoded RC6: "); } Serial.print("Value = "); Serial.println( results.value, DEC); } } void updateMotors(){ leftServo.write(leftSpeed); rightServo.write(rightSpeed); }