]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter14_IR_Decoder/APFD_Chapter14_IR_Decoder.ino
giov
[arduino] / books / pdummies / APFD_Chapter14_IR_Decoder / APFD_Chapter14_IR_Decoder.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  * This code is only for decoding your IR remote control
9  *
10  * Uses the standard Arduino servo library and
11  * the IR Remote Library by Ken Shirriff:
12  * https://github.com/shirriff/Arduino-IRremote
13  * Adapted from code by Michael Margolis
14  *
15  * v0.1 30.04.2013
16 */
17
18
19 #include <IRremote.h>
20
21 const int irReceivePin = 2;     // Pin connected to IR detector output
22 IRrecv irrecv(irReceivePin);    // create the IR library
23 decode_results results;         // IR results are stored here
24
25 void setup()
26 {
27   Serial.begin(9600);           // Start the serial port
28   irrecv.enableIRIn();          // Start the IR receiver
29 }
30
31 void loop() {
32   if( irrecv.decode(&results) )
33   {
34     showReceivedData();
35     irrecv.resume(); // Receive the next value
36   }
37   delay(250);
38 }
39
40 void showReceivedData()
41 {
42   if (results.decode_type == UNKNOWN)
43   {
44     Serial.println("-Could not decode message");
45   }
46   else
47   {
48     if (results.decode_type == NEC) {
49       Serial.print("- decoded NEC: ");
50     }
51     else if (results.decode_type == SONY) {
52       Serial.print("- decoded SONY: ");
53     }
54     else if (results.decode_type == RC5) {
55       Serial.print("- decoded RC5: ");
56     }
57     else if (results.decode_type == RC6) {
58       Serial.print("- decoded RC6: ");
59     }
60     Serial.print("Value = ");
61     Serial.println(results.value, DEC);  // Print the results as a decimal value
62   }
63 }