f3331335674e283235aea497049329b60ad5ccc3
[svn42.git] / rf433ctl / IRremote / examples / IRrelay / IRrelay.pde
1 /*
2  * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
3  * An IR detector/demodulator must be connected to the input RECV_PIN.
4  * Version 0.1 July, 2009
5  * Copyright 2009 Ken Shirriff
6  * http://arcfn.com
7  */
8
9 #include <IRremote.h>
10
11 int RECV_PIN = 11;
12 int RELAY_PIN = 4;
13
14 IRrecv irrecv(RECV_PIN);
15 decode_results results;
16
17 // Dumps out the decode_results structure.
18 // Call this after IRrecv::decode()
19 // void * to work around compiler issue
20 //void dump(void *v) {
21 //  decode_results *results = (decode_results *)v
22 void dump(decode_results *results) {
23   int count = results->rawlen;
24   if (results->decode_type == UNKNOWN) {
25     Serial.println("Could not decode message");
26   } 
27   else {
28     if (results->decode_type == NEC) {
29       Serial.print("Decoded NEC: ");
30     } 
31     else if (results->decode_type == SONY) {
32       Serial.print("Decoded SONY: ");
33     } 
34     else if (results->decode_type == RC5) {
35       Serial.print("Decoded RC5: ");
36     } 
37     else if (results->decode_type == RC6) {
38       Serial.print("Decoded RC6: ");
39     }
40     Serial.print(results->value, HEX);
41     Serial.print(" (");
42     Serial.print(results->bits, DEC);
43     Serial.println(" bits)");
44   }
45   Serial.print("Raw (");
46   Serial.print(count, DEC);
47   Serial.print("): ");
48
49   for (int i = 0; i < count; i++) {
50     if ((i % 2) == 1) {
51       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
52     } 
53     else {
54       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
55     }
56     Serial.print(" ");
57   }
58   Serial.println("");
59 }
60
61 void setup()
62 {
63   pinMode(RELAY_PIN, OUTPUT);
64   pinMode(13, OUTPUT);
65     Serial.begin(9600);
66   irrecv.enableIRIn(); // Start the receiver
67 }
68
69 int on = 0;
70 unsigned long last = millis();
71
72 void loop() {
73   if (irrecv.decode(&results)) {
74     // If it's been at least 1/4 second since the last
75     // IR received, toggle the relay
76     if (millis() - last > 250) {
77       on = !on;
78       digitalWrite(RELAY_PIN, on ? HIGH : LOW);
79       digitalWrite(13, on ? HIGH : LOW);
80       dump(&results);
81     }
82     last = millis();      
83     irrecv.resume(); // Receive the next value
84   }
85 }\r