/* Raw IR commander This sketch was modified by Mazzmn for the Instructable on Build your own Bobble-head Bobbler http://www.instructables.com/id/EC7ANH7HPJ0592E/ This sketch/program uses the Arduno and an PNA4602 to decode IR received. It then attempts to match it to a previously recorded IR signal. Based on the button pressed we'll shake a Bobble-head by driving a Servo The previously recorded IR signals were determined with the tutorial supplied script called irdecode and then stored in the arrays below (like One_IRSignal) http://learn.adafruit.com/ir-sensor/reading-ir-commands These arrays worked for a GE Universal Remote (programmed for some TV model, who knows which) Code is public domain, check out www.ladyada.net and adafruit.com for more tutorials! */ // We need to use the 'raw' pin reading methods // because timing is very important here and the digitalRead() // procedure is slower! //uint8_t IRpin = 2; // Digital pin #2 is the same as Pin D2 see // http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping #define IRpin_PIN PIND #define IRpin 2 // the maximum pulse we'll listen for - 65 milliseconds is a long time #define MAXPULSE 65000 #define NUMPULSES 50 // what our timing resolution should be, larger is better // as its more 'precise' - but too large and you wont get // accurate timing #define RESOLUTION 20 // What percent we will allow in variation to match the same code #define FUZZINESS 20 // we will store up to 100 pulse pairs (this is -a lot-) uint16_t pulses[NUMPULSES][2]; // pair is high and low pulse uint8_t currentpulse = 0; // index for pulses we're storing #include int servoPin = 14; // servo as analog 0 int minAngle = 544; //minimum degree angle on servo int maxAngle = 180; //max degree angle on servo Servo servo1; int VolumeUpSignal[] = { 404, 382, 58, 188, 60, 186, 60, 186, 60, 188, 58, 90, 60, 88, 58, 188, 60, 90, 58, 188, 60, 186, 60, 188, 58, 188, 58, 90, 58, 90, 60, 88, 58, 90, 60, 188, 58, 188, 58, 90, 58, 188, 60, 88, 58, 90, 60, 90, 58, 90, 58, 776, 402, 384, 58, 188, 58, 188, 60, 188, 58, 188, 58, 88, 60, 90, 60, 186, 60, 88, 60, 186, 60, 188, 58, 188, 58, 188, 58, 90, 60, 90, 58, 88, 60, 90, 58, 188, 58, 188, 58, 92, 58, 188, 58, 90, 58, 90, 60, 88, 58, 90, 60, 776, 402, 382, 60, 188, 58, 188, 60, 186, 60, 186, 60, 88, 60, 90, 58, 188, 58, 90, 58, 188, 56, 190, 60, 186, 60, 186, 60, 90, 60, 88, 58, 90, 58, 90, 60, 186, 60, 188, 58, 90, 60, 186, 56, 92, 60, 90, 58, 90, 56, 92, 56, 0}; int VolumeDownSignal[] = { 396, 388, 54, 192, 54, 194, 52, 194, 54, 192, 54, 94, 54, 96, 52, 194, 54, 94, 52, 194, 54, 192, 54, 194, 52, 96, 54, 94, 52, 96, 54, 94, 54, 96, 52, 194, 54, 192, 54, 94, 52, 194, 54, 94, 54, 94, 54, 96, 54, 192, 54, 782, 394, 390, 54, 192, 54, 192, 54, 192, 54, 194, 52, 96, 54, 94, 54, 192, 54, 96, 52, 194, 54, 192, 54, 192, 54, 94, 54, 94, 54, 96, 54, 94, 52, 96, 52, 194, 54, 192, 54, 96, 54, 192, 54, 94, 52, 94, 54, 96, 52, 194, 54, 0}; int Two_IRSignal[] = { // ON, OFF (in 10's of microseconds) 398, 386, 56, 190, 56, 192, 52, 194, 56, 190, 54, 94, 56, 94, 54, 192, 54, 192, 54, 94, 56, 94, 56, 190, 52, 94, 58, 92, 56, 92, 56, 92, 56, 92, 54, 192, 56, 192, 56, 92, 56, 92, 56, 190, 56, 192, 56, 92, 56, 190, 56, 780, 402, 382, 56, 190, 56, 190, 56, 190, 58, 190, 56, 92, 56, 92, 56, 190, 56, 190, 56, 94, 56, 92, 56, 190, 56, 92, 56, 94, 56, 90, 56, 94, 56, 92, 56, 190, 58, 190, 56, 90, 56, 94, 56, 190, 56, 190, 58, 90, 56, 192, 56, 778, 404, 380, 56, 192, 54, 192, 56, 190, 56, 190, 56, 94, 56, 90, 58, 190, 56, 190, 56, 92, 56, 92, 56, 190, 56, 94, 58, 90, 56, 92, 56, 92, 58, 92, 56, 190, 56, 190, 56, 92, 56, 94, 54, 192, 56, 190, 58, 90, 56, 190, 56, 0}; int One_IRSignal[] = { // ON, OFF (in 10's of microseconds) 404, 380, 60, 186, 56, 190, 60, 188, 58, 188, 60, 88, 60, 88, 60, 186, 60, 188, 58, 90, 60, 90, 58, 88, 60, 186, 60, 90, 60, 88, 58, 90, 60, 88, 60, 186, 60, 188, 58, 90, 60, 88, 60, 186, 60, 188, 58, 188, 60, 88, 58, 778, 402, 382, 58, 188, 58, 188, 60, 186, 60, 186, 60, 90, 60, 88, 60, 186, 60, 186, 60, 90, 58, 88, 60, 90, 60, 186, 60, 88, 60, 88, 60, 90, 58, 90, 60, 186, 60, 186, 60, 88, 60, 90, 58, 188, 58, 188, 60, 186, 60, 90, 58, 774, 404, 382, 60, 186, 60, 188, 58, 188, 58, 188, 60, 88, 60, 90, 58, 188, 60, 186, 60, 88, 60, 88, 60, 88, 60, 186, 60, 90, 60, 88, 58, 90, 60, 88, 60, 186, 60, 186, 60, 90, 60, 88, 60, 186, 60, 186, 60, 188, 58, 90, 58, 776, 404, 382, 60, 186, 60, 186, 60, 188, 58, 188, 60, 88, 58, 90, 58, 188, 60, 186, 60, 90, 58, 90, 58, 90, 60, 186, 60, 88, 60, 90, 60, 188, 58, 188, 60, 90, 58, 0}; void setup(void) { Serial.begin(9600); pinMode(servoPin, OUTPUT); servo1.attach(servoPin); // pin to activate servo Serial.println("Bobble-head Bobbler V1, Start!"); Serial.println("Ready to decode IR!"); } void loop(void) { int numberpulses; int posOne = 130; // initial position of servo wing int posTwo = 90; int posThree = 70; numberpulses = listenForIR(); Serial.print("Heard "); Serial.print(numberpulses); Serial.println("-pulse long IR signal"); if (IRcompare(numberpulses, One_IRSignal,sizeof(One_IRSignal)/4)) { Serial.println("One Button"); Serial.println("Shake your moneymaker"); for (int i=0; i<=10; i++) { servo1.write(posOne); delay(110); servo1.write(posTwo); delay(90); //delay 90 ms to allow servo to reach angle servo1.write(posThree); delay(90); //delay 90 ms to allow servo to reach angle } servo1.write(posOne); // set back to ready position delay(90); } if (IRcompare(numberpulses, Two_IRSignal,sizeof(Two_IRSignal)/4)) { Serial.println("Two Button"); } if (IRcompare(numberpulses, VolumeDownSignal,sizeof(VolumeDownSignal)/4)) { Serial.println("Volume Down \n"); } if (IRcompare(numberpulses, VolumeUpSignal,sizeof(VolumeUpSignal)/4)) { Serial.println("Volume Up \n"); } delay(500); } //KGO: added size of compare sample. Only compare the minimum of the two boolean IRcompare(int numpulses, int Signal[], int refsize) { int count = min(numpulses,refsize); Serial.print("\n count set to: "); Serial.println(count); if (count <= 1) { Serial.print("bye\n"); return false; } for (int i=0; i< count-1; i++) { int oncode = pulses[i][1] * RESOLUTION / 10; int offcode = pulses[i+1][0] * RESOLUTION / 10; #ifdef DEBUG Serial.print(oncode); // the ON signal we heard Serial.print("\n - \n"); Serial.print(Signal[i*2 + 0]); // the ON signal we want #endif // check to make sure the error is less than FUZZINESS percent if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) { #ifdef DEBUG Serial.print(" (ok)"); #endif } else { #ifdef DEBUG Serial.print(" (x) on signal bad"); #endif // we didn't match perfectly, return a false match return false; } #ifdef DEBUG Serial.print(" \t"); // tab Serial.print(offcode); // the OFF signal we heard Serial.print(" - "); Serial.print(Signal[i*2 + 1]); // the OFF signal we want #endif if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) { #ifdef DEBUG Serial.print(" (ok)"); #endif } else { #ifdef DEBUG Serial.print(" (x) Off signal bad"); #endif // we didn't match perfectly, return a false match return false; } #ifdef DEBUG Serial.println(); #endif } // Everything matched! return true; } int listenForIR(void) { currentpulse = 0; while (1) { uint16_t highpulse, lowpulse; // temporary storage timing highpulse = lowpulse = 0; // start out with no pulse length // while (digitalRead(IRpin)) { // this is too slow! while (IRpin_PIN & (1 << IRpin)) { // pin is still HIGH // count off another few microseconds highpulse++; delayMicroseconds(RESOLUTION); // If the pulse is too long, we 'timed out' - either nothing // was received or the code is finished, so print what // we've grabbed so far, and then reset // KGO: Added check for end of receive buffer if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) { return currentpulse; } } // we didn't time out so lets stash the reading pulses[currentpulse][0] = highpulse; // same as above while (! (IRpin_PIN & _BV(IRpin))) { // pin is still LOW lowpulse++; delayMicroseconds(RESOLUTION); // KGO: Added check for end of receive buffer if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) { return currentpulse; } } pulses[currentpulse][1] = lowpulse; // we read one high-low pulse successfully, continue! currentpulse++; } } void printpulses(void) { Serial.println("\n\r\n\rReceived: \n\rOFF \tON"); for (uint8_t i = 0; i < currentpulse; i++) { Serial.print(pulses[i][0] * RESOLUTION, DEC); Serial.print(" usec, "); Serial.print(pulses[i][1] * RESOLUTION, DEC); Serial.println(" usec"); } // print it in a 'array' format Serial.println("int IRsignal[] = {"); Serial.println("// ON, OFF (in 10's of microseconds)"); for (uint8_t i = 0; i < currentpulse-1; i++) { Serial.print("\t"); // tab Serial.print(pulses[i][1] * RESOLUTION / 10, DEC); Serial.print(", "); Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC); Serial.println(","); } Serial.print("\t"); // tab Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC); Serial.print(", 0};"); }