]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter14_Remote_Controlled_Car/APFD_Chapter14_Remote_Controlled_Car.ino
giovedi
[arduino] / books / pdummies / APFD_Chapter14_Remote_Controlled_Car / APFD_Chapter14_Remote_Controlled_Car.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
4  * Chapter 14: Building a Remote Controlled Car
5  * Detects signals from a garden variety remote control 
6  * to drive two servo motors. 
7  * 
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
12  *
13  * v0.1 30.04.2013
14 */
15
16 #include <Servo.h>
17 #include <IRremote.h>
18
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
22
23 const int rightMotorPin = 9;  // Connected to right servo
24 const int leftMotorPin = 10;  // Connected to left servo
25
26 Servo rightServo;
27 Servo leftServo;
28
29 int rightSpeed=90;
30 int leftSpeed=90;
31
32 long keyCode=0;
33
34 boolean DEBUG = false; // set to true if you want to display the Debug output
35
36 void setup()
37 {
38   if(DEBUG){
39     Serial.begin(9600);
40   }
41
42   irrecv.enableIRIn();              // Start the IR receiver
43
44   leftServo.attach(9);
45   rightServo.attach(10);
46   
47   pinMode(rightMotorPin, OUTPUT);
48   pinMode(leftMotorPin, OUTPUT);
49 }
50
51 void loop() {
52   if( irrecv.decode(&results) )
53
54   {
55     keyCode=results.value;
56     if(keyCode != -1)
57     {
58       switch (keyCode){
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
62         rightSpeed+=1; 
63         break;
64         
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
68         rightSpeed-=1; 
69         break; 
70         
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
74         rightSpeed=90; 
75         break; 
76
77         case 50152125:  // Replace this code with the one from your remote!
78         Serial.println("Turn Left"); // Wheels move in opposite directions
79         leftSpeed-=1;
80         rightSpeed-=1;  
81         break; 
82         
83         case 50135805:  // Replace this code with the one from your remote!
84         Serial.println("Turn Right");  // Wheels move in opposite directions
85         leftSpeed+=1;
86         rightSpeed+=1;
87         break; 
88         
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;
93         break; 
94
95       }
96       
97      if(DEBUG){
98       Serial.println(keyCode);
99       showReceivedData();
100       Serial.print(leftSpeed);
101       Serial.print(", ");
102       Serial.println(rightSpeed);
103      }
104      
105     }
106     irrecv.resume(); // Receive the next value
107   }
108
109   updateMotors();
110   delay(10);
111 }
112
113 void showReceivedData()
114 {
115   if (results.decode_type == UNKNOWN)
116   {
117     Serial.println("-Could not decode message");
118   }
119   else
120   {
121     if (results.decode_type == NEC) {
122       Serial.print("- decoded NEC: ");
123     }
124     else if (results.decode_type == SONY) {
125       Serial.print("- decoded SONY: ");
126     }
127     else if (results.decode_type == RC5) {
128       Serial.print("- decoded RC5: ");
129     }
130     else if (results.decode_type == RC6) {
131       Serial.print("- decoded RC6: ");
132     }
133     Serial.print("Value = ");
134     Serial.println( results.value, DEC);
135   }
136 }
137
138 void updateMotors(){
139   leftServo.write(leftSpeed);
140   rightServo.write(rightSpeed);
141 }