|
| 1 | + |
| 2 | +/* Raw IR decoder sketch! |
| 3 | + |
| 4 | + This sketch/program uses the Arduno and a PNA4602 to |
| 5 | + decode IR received. This can be used to make a IR receiver |
| 6 | + (by looking for a particular code) |
| 7 | + or transmitter (by pulsing an IR LED at ~38KHz for the |
| 8 | + durations detected |
| 9 | + |
| 10 | + Code is public domain, check out www.ladyada.net and adafruit.com |
| 11 | + for more tutorials! |
| 12 | + */ |
| 13 | + |
| 14 | +// We need to use the 'raw' pin reading methods |
| 15 | +// because timing is very important here and the digitalRead() |
| 16 | +// procedure is slower! |
| 17 | +//uint8_t IRpin = 2; |
| 18 | +// Digital pin #2 is the same as Pin D2 see |
| 19 | +// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping |
| 20 | +#define IRpin_PIN PIND |
| 21 | +#define IRpin 2 |
| 22 | + |
| 23 | +// the maximum pulse we'll listen for - 65 milliseconds is a long time |
| 24 | +#define MAXPULSE 65000 |
| 25 | + |
| 26 | +// what our timing resolution should be, larger is better |
| 27 | +// as its more 'precise' - but too large and you wont get |
| 28 | +// accurate timing |
| 29 | +#define RESOLUTION 20 |
| 30 | + |
| 31 | +// we will store up to 100 pulse pairs (this is -a lot-) |
| 32 | +uint16_t pulses[100][2]; // pair is high and low pulse |
| 33 | +uint8_t currentpulse = 0; // index for pulses we're storing |
| 34 | + |
| 35 | +void setup(void) { |
| 36 | + Serial.begin(9600); |
| 37 | + Serial.println("Ready to decode IR!"); |
| 38 | +} |
| 39 | + |
| 40 | +void loop(void) { |
| 41 | + uint16_t highpulse, lowpulse; // temporary storage timing |
| 42 | + highpulse = lowpulse = 0; // start out with no pulse length |
| 43 | + |
| 44 | + |
| 45 | +// while (digitalRead(IRpin)) { // this is too slow! |
| 46 | + while (IRpin_PIN & (1 << IRpin)) { |
| 47 | + // pin is still HIGH |
| 48 | + |
| 49 | + // count off another few microseconds |
| 50 | + highpulse++; |
| 51 | + delayMicroseconds(RESOLUTION); |
| 52 | + |
| 53 | + // If the pulse is too long, we 'timed out' - either nothing |
| 54 | + // was received or the code is finished, so print what |
| 55 | + // we've grabbed so far, and then reset |
| 56 | + if ((highpulse >= MAXPULSE) && (currentpulse != 0)) { |
| 57 | + printpulses(); |
| 58 | + currentpulse=0; |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + // we didn't time out so lets stash the reading |
| 63 | + pulses[currentpulse][0] = highpulse; |
| 64 | + |
| 65 | + // same as above |
| 66 | + while (! (IRpin_PIN & _BV(IRpin))) { |
| 67 | + // pin is still LOW |
| 68 | + lowpulse++; |
| 69 | + delayMicroseconds(RESOLUTION); |
| 70 | + if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) { |
| 71 | + printpulses(); |
| 72 | + currentpulse=0; |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + pulses[currentpulse][1] = lowpulse; |
| 77 | + |
| 78 | + // we read one high-low pulse successfully, continue! |
| 79 | + currentpulse++; |
| 80 | +} |
| 81 | + |
| 82 | +void printpulses(void) { |
| 83 | + Serial.println("\n\r\n\rReceived: \n\rOFF \tON"); |
| 84 | + for (uint8_t i = 0; i < currentpulse; i++) { |
| 85 | + Serial.print(pulses[i][0] * RESOLUTION, DEC); |
| 86 | + Serial.print(" usec, "); |
| 87 | + Serial.print(pulses[i][1] * RESOLUTION, DEC); |
| 88 | + Serial.println(" usec"); |
| 89 | + } |
| 90 | + |
| 91 | + // print it in a 'array' format |
| 92 | + Serial.println("int IRsignal[] = {"); |
| 93 | + Serial.println("// ON, OFF "); |
| 94 | + for (uint8_t i = 0; i < currentpulse-1; i++) { |
| 95 | + //Serial.print("\t"); // tab |
| 96 | + Serial.print("pulseIR("); |
| 97 | + Serial.print(pulses[i][1] * RESOLUTION , DEC); |
| 98 | + Serial.print(");"); |
| 99 | + Serial.println(""); |
| 100 | + //Serial.print("\t"); |
| 101 | + Serial.print("delayMicroseconds("); |
| 102 | + Serial.print(pulses[i+1][0] * RESOLUTION , DEC); |
| 103 | + Serial.println(");"); |
| 104 | + |
| 105 | + } |
| 106 | + //Serial.print("\t"); // tab |
| 107 | + Serial.print("pulseIR("); |
| 108 | + Serial.print(pulses[currentpulse-1][1] * RESOLUTION, DEC); |
| 109 | + Serial.print(");"); |
| 110 | + |
| 111 | +} |
0 commit comments