|
| 1 | +/* Raw IR commander |
| 2 | + |
| 3 | + This sketch/program uses the Arduno and a PNA4602 to |
| 4 | + decode IR received. It then attempts to match it to a previously |
| 5 | + recorded IR signal. Limor Fried, Adafruit Industries |
| 6 | + |
| 7 | + MIT License, please attribute |
| 8 | + check out learn.adafruit.com for more tutorials! |
| 9 | + */ |
| 10 | + |
| 11 | +// We need to use the 'raw' pin reading methods |
| 12 | +// because timing is very important here and the digitalRead() |
| 13 | +// procedure is slower! |
| 14 | +//uint8_t IRpin = 2; |
| 15 | +// Digital pin #2 is the same as Pin D2 see |
| 16 | +// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping |
| 17 | +#define IRpin_PIN PIND |
| 18 | +#define IRpin 2 |
| 19 | + |
| 20 | +// the maximum pulse we'll listen for - 65 milliseconds is a long time |
| 21 | +#define MAXPULSE 65000 |
| 22 | +#define NUMPULSES 50 |
| 23 | + |
| 24 | +// what our timing resolution should be, larger is better |
| 25 | +// as its more 'precise' - but too large and you wont get |
| 26 | +// accurate timing |
| 27 | +#define RESOLUTION 20 |
| 28 | + |
| 29 | +// What percent we will allow in variation to match the same code |
| 30 | +#define FUZZINESS 20 |
| 31 | + |
| 32 | +// we will store up to 100 pulse pairs (this is -a lot-) |
| 33 | +uint16_t pulses[NUMPULSES][2]; // pair is high and low pulse |
| 34 | +uint8_t currentpulse = 0; // index for pulses we're storing |
| 35 | + |
| 36 | +#include "ircommander.h" |
| 37 | + |
| 38 | +void setup(void) { |
| 39 | + Serial.begin(9600); |
| 40 | + Serial.println("Ready to decode IR!"); |
| 41 | +} |
| 42 | + |
| 43 | +void loop(void) { |
| 44 | + int numberpulses; |
| 45 | + |
| 46 | + numberpulses = listenForIR(); |
| 47 | + |
| 48 | + Serial.print("Heard "); |
| 49 | + Serial.print(numberpulses); |
| 50 | + Serial.println("-pulse long IR signal"); |
| 51 | + if (IRcompare(numberpulses, ApplePlaySignal,sizeof(ApplePlaySignal)/4)) { |
| 52 | + Serial.println("PLAY"); |
| 53 | + } |
| 54 | + if (IRcompare(numberpulses, AppleRewindSignal,sizeof(AppleRewindSignal)/4)) { |
| 55 | + Serial.println("REWIND"); |
| 56 | + } |
| 57 | + if (IRcompare(numberpulses, AppleForwardSignal,sizeof(AppleForwardSignal)/4)) { |
| 58 | + Serial.println("FORWARD"); |
| 59 | + } |
| 60 | + delay(500); |
| 61 | +} |
| 62 | + |
| 63 | +//KGO: added size of compare sample. Only compare the minimum of the two |
| 64 | +boolean IRcompare(int numpulses, int Signal[], int refsize) { |
| 65 | + int count = min(numpulses,refsize); |
| 66 | + Serial.print("count set to: "); |
| 67 | + Serial.println(count); |
| 68 | + for (int i=0; i< count-1; i++) { |
| 69 | + int oncode = pulses[i][1] * RESOLUTION / 10; |
| 70 | + int offcode = pulses[i+1][0] * RESOLUTION / 10; |
| 71 | + |
| 72 | +#ifdef DEBUG |
| 73 | + Serial.print(oncode); // the ON signal we heard |
| 74 | + Serial.print(" - "); |
| 75 | + Serial.print(Signal[i*2 + 0]); // the ON signal we want |
| 76 | +#endif |
| 77 | + |
| 78 | + // check to make sure the error is less than FUZZINESS percent |
| 79 | + if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) { |
| 80 | +#ifdef DEBUG |
| 81 | + Serial.print(" (ok)"); |
| 82 | +#endif |
| 83 | + } else { |
| 84 | +#ifdef DEBUG |
| 85 | + Serial.print(" (x)"); |
| 86 | +#endif |
| 87 | + // we didn't match perfectly, return a false match |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +#ifdef DEBUG |
| 93 | + Serial.print(" \t"); // tab |
| 94 | + Serial.print(offcode); // the OFF signal we heard |
| 95 | + Serial.print(" - "); |
| 96 | + Serial.print(Signal[i*2 + 1]); // the OFF signal we want |
| 97 | +#endif |
| 98 | + |
| 99 | + if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) { |
| 100 | +#ifdef DEBUG |
| 101 | + Serial.print(" (ok)"); |
| 102 | +#endif |
| 103 | + } else { |
| 104 | +#ifdef DEBUG |
| 105 | + Serial.print(" (x)"); |
| 106 | +#endif |
| 107 | + // we didn't match perfectly, return a false match |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | +#ifdef DEBUG |
| 112 | + Serial.println(); |
| 113 | +#endif |
| 114 | + } |
| 115 | + // Everything matched! |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | +int listenForIR(void) { |
| 120 | + currentpulse = 0; |
| 121 | + |
| 122 | + while (1) { |
| 123 | + uint16_t highpulse, lowpulse; // temporary storage timing |
| 124 | + highpulse = lowpulse = 0; // start out with no pulse length |
| 125 | + |
| 126 | +// while (digitalRead(IRpin)) { // this is too slow! |
| 127 | + while (IRpin_PIN & (1 << IRpin)) { |
| 128 | + // pin is still HIGH |
| 129 | + |
| 130 | + // count off another few microseconds |
| 131 | + highpulse++; |
| 132 | + delayMicroseconds(RESOLUTION); |
| 133 | + |
| 134 | + // If the pulse is too long, we 'timed out' - either nothing |
| 135 | + // was received or the code is finished, so print what |
| 136 | + // we've grabbed so far, and then reset |
| 137 | + |
| 138 | + // KGO: Added check for end of receive buffer |
| 139 | + if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) { |
| 140 | + return currentpulse; |
| 141 | + } |
| 142 | + } |
| 143 | + // we didn't time out so lets stash the reading |
| 144 | + pulses[currentpulse][0] = highpulse; |
| 145 | + |
| 146 | + // same as above |
| 147 | + while (! (IRpin_PIN & _BV(IRpin))) { |
| 148 | + // pin is still LOW |
| 149 | + lowpulse++; |
| 150 | + delayMicroseconds(RESOLUTION); |
| 151 | + // KGO: Added check for end of receive buffer |
| 152 | + if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) { |
| 153 | + return currentpulse; |
| 154 | + } |
| 155 | + } |
| 156 | + pulses[currentpulse][1] = lowpulse; |
| 157 | + |
| 158 | + // we read one high-low pulse successfully, continue! |
| 159 | + currentpulse++; |
| 160 | + } |
| 161 | +} |
| 162 | +void printpulses(void) { |
| 163 | + Serial.println("\n\r\n\rReceived: \n\rOFF \tON"); |
| 164 | + for (uint8_t i = 0; i < currentpulse; i++) { |
| 165 | + Serial.print(pulses[i][0] * RESOLUTION, DEC); |
| 166 | + Serial.print(" usec, "); |
| 167 | + Serial.print(pulses[i][1] * RESOLUTION, DEC); |
| 168 | + Serial.println(" usec"); |
| 169 | + } |
| 170 | + |
| 171 | + // print it in a 'array' format |
| 172 | + Serial.println("int IRsignal[] = {"); |
| 173 | + Serial.println("// ON, OFF (in 10's of microseconds)"); |
| 174 | + for (uint8_t i = 0; i < currentpulse-1; i++) { |
| 175 | + Serial.print("\t"); // tab |
| 176 | + Serial.print(pulses[i][1] * RESOLUTION / 10, DEC); |
| 177 | + Serial.print(", "); |
| 178 | + Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC); |
| 179 | + Serial.println(","); |
| 180 | + } |
| 181 | + Serial.print("\t"); // tab |
| 182 | + Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC); |
| 183 | + Serial.print(", 0};"); |
| 184 | +} |
| 185 | + |
0 commit comments