to github
[svn42.git] / rf433ctl / IRremote / examples / IRrecord / IRrecord.pde
1 /*
2  * IRrecord: record and play back IR signals as a minimal 
3  * An IR detector/demodulator must be connected to the input RECV_PIN.
4  * An IR LED must be connected to the output PWM pin 3.
5  * A button must be connected to the input BUTTON_PIN; this is the
6  * send button.
7  * A visible LED can be connected to STATUS_PIN to provide status.
8  *
9  * The logic is:
10  * If the button is pressed, send the IR code.
11  * If an IR code is received, record it.
12  *
13  * Version 0.11 September, 2009
14  * Copyright 2009 Ken Shirriff
15  * http://arcfn.com
16  */
17
18 #include <IRremote.h>
19
20 int RECV_PIN = 11;
21 int BUTTON_PIN = 12;
22 int STATUS_PIN = 13;
23
24 IRrecv irrecv(RECV_PIN);
25 IRsend irsend;
26
27 decode_results results;
28
29 void setup()
30 {
31   Serial.begin(9600);
32   irrecv.enableIRIn(); // Start the receiver
33   pinMode(BUTTON_PIN, INPUT);
34   pinMode(STATUS_PIN, OUTPUT);
35 }
36
37 // Storage for the recorded code
38 int codeType = -1; // The type of code
39 unsigned long codeValue; // The code value if not raw
40 unsigned int rawCodes[RAWBUF]; // The durations if raw
41 int codeLen; // The length of the code
42 int toggle = 0; // The RC5/6 toggle state
43
44 // Stores the code for later playback
45 // Most of this code is just logging
46 void storeCode(decode_results *results) {
47   codeType = results->decode_type;
48   int count = results->rawlen;
49   if (codeType == UNKNOWN) {
50     Serial.println("Received unknown code, saving as raw");
51     codeLen = results->rawlen - 1;
52     // To store raw codes:
53     // Drop first value (gap)
54     // Convert from ticks to microseconds
55     // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
56     for (int i = 1; i <= codeLen; i++) {
57       if (i % 2) {
58         // Mark
59         rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
60         Serial.print(" m");
61       } 
62       else {
63         // Space
64         rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
65         Serial.print(" s");
66       }
67       Serial.print(rawCodes[i - 1], DEC);
68     }
69     Serial.println("");
70   }
71   else {
72     if (codeType == NEC) {
73       Serial.print("Received NEC: ");
74       if (results->value == REPEAT) {
75         // Don't record a NEC repeat value as that's useless.
76         Serial.println("repeat; ignoring.");
77         return;
78       }
79     } 
80     else if (codeType == SONY) {
81       Serial.print("Received SONY: ");
82     } 
83     else if (codeType == RC5) {
84       Serial.print("Received RC5: ");
85     } 
86     else if (codeType == RC6) {
87       Serial.print("Received RC6: ");
88     } 
89     else {
90       Serial.print("Unexpected codeType ");
91       Serial.print(codeType, DEC);
92       Serial.println("");
93     }
94     Serial.println(results->value, HEX);
95     codeValue = results->value;
96     codeLen = results->bits;
97   }
98 }
99
100 void sendCode(int repeat) {
101   if (codeType == NEC) {
102     if (repeat) {
103       irsend.sendNEC(REPEAT, codeLen);
104       Serial.println("Sent NEC repeat");
105     } 
106     else {
107       irsend.sendNEC(codeValue, codeLen);
108       Serial.print("Sent NEC ");
109       Serial.println(codeValue, HEX);
110     }
111   } 
112   else if (codeType == SONY) {
113     irsend.sendSony(codeValue, codeLen);
114     Serial.print("Sent Sony ");
115     Serial.println(codeValue, HEX);
116   } 
117   else if (codeType == RC5 || codeType == RC6) {
118     if (!repeat) {
119       // Flip the toggle bit for a new button press
120       toggle = 1 - toggle;
121     }
122     // Put the toggle bit into the code to send
123     codeValue = codeValue & ~(1 << (codeLen - 1));
124     codeValue = codeValue | (toggle << (codeLen - 1));
125     if (codeType == RC5) {
126       Serial.print("Sent RC5 ");
127       Serial.println(codeValue, HEX);
128       irsend.sendRC5(codeValue, codeLen);
129     } 
130     else {
131       irsend.sendRC6(codeValue, codeLen);
132       Serial.print("Sent RC6 ");
133       Serial.println(codeValue, HEX);
134     }
135   } 
136   else if (codeType == UNKNOWN /* i.e. raw */) {
137     // Assume 38 KHz
138     irsend.sendRaw(rawCodes, codeLen, 38);
139     Serial.println("Sent raw");
140   }
141 }
142
143 int lastButtonState;
144
145 void loop() {
146   // If button pressed, send the code.
147   int buttonState = digitalRead(BUTTON_PIN);
148   if (lastButtonState == HIGH && buttonState == LOW) {
149     Serial.println("Released");
150     irrecv.enableIRIn(); // Re-enable receiver
151   }
152
153   if (buttonState) {
154     Serial.println("Pressed, sending");
155     digitalWrite(STATUS_PIN, HIGH);
156     sendCode(lastButtonState == buttonState);
157     digitalWrite(STATUS_PIN, LOW);
158     delay(50); // Wait a bit between retransmissions
159   } 
160   else if (irrecv.decode(&results)) {
161     digitalWrite(STATUS_PIN, HIGH);
162     storeCode(&results);
163     irrecv.resume(); // resume receiver
164     digitalWrite(STATUS_PIN, LOW);
165   }
166   lastButtonState = buttonState;
167 }
168
169
170
171
172
173
174 \r