/* Raw IR commander */ #define IRpin_PIN PIND #define IRpin 2 #define MAXPULSE 65000 #define RESOLUTION 20 #define FUZZINESS 20 uint16_t pulses[100][2]; // pair is high and low pulse uint8_t currentpulse = 0; // index for pulses we're storing // Up int off1[] = { 892, 436, 62, 156, 64, 158, 64, 46, 62, 48, 64, 46, 64, 158, 62, 46, 66, 46, 62, 48, 62, 48, 64, 156, 64, 156, 64, 156, 66, 46, 62, 158, 64, 158, 62, 158, 62, 158, 66, 44, 62, 50, 62, 46, 66, 46, 62, 48, 64, 46, 64, 46, 62, 48, 64, 156, 64, 156, 66, 156, 64, 156, 64, 156, 66, 156, 64, 3916, 892, 212, 64, 2886, 894, 210, 66, 2888, 892, 212, 64, 0}; // Down int dtop[] = { // ON, OFF (in 10's of microseconds) 892, 434, 62, 158, 62, 158, 64, 46, 62, 48, 64, 46, 64, 158, 62, 46, 66, 46, 62, 48, 64, 44, 66, 158, 62, 156, 64, 158, 64, 46, 62, 158, 66, 156, 64, 44, 66, 46, 62, 158, 64, 46, 62, 50, 62, 46, 66, 46, 62, 48, 64, 156, 62, 158, 64, 46, 62, 160, 64, 158, 62, 156, 64, 156, 66, 156, 64, 3916, 892, 212, 64, 2888, 892, 212, 64, 0}; // Lamp void setup(void) { Serial.begin(9600); Serial.println("Ready to decode IR!"); randomSeed(analogRead(0)); } void loop(void) { int numberpulses; numberpulses = listenForIR(); if (IRcompare(numberpulses, off1)) { Serial.println("Lamp"); analogWrite(11, 1); analogWrite(10, 1); analogWrite(9, 1); } if (IRcompare(numberpulses, dtop)) { // Serial.println("Up"); analogWrite(11, random(255)); analogWrite(10, random(255)); analogWrite(9, random(255)); } } boolean IRcompare(int numpulses, int Signal[]) { for (int i=0; i< numpulses-1; i++) { int oncode = pulses[i][1] * RESOLUTION / 10; int offcode = pulses[i+1][0] * RESOLUTION / 10; if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) { } else { return false; } if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) { } else { return false; } } 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 (IRpin_PIN & (1 << IRpin)) { highpulse++; delayMicroseconds(RESOLUTION); if ((highpulse >= MAXPULSE) && (currentpulse != 0)) { return currentpulse; } } pulses[currentpulse][0] = highpulse; while (! (IRpin_PIN & _BV(IRpin))) { lowpulse++; delayMicroseconds(RESOLUTION); if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) { return currentpulse; } } pulses[currentpulse][1] = lowpulse; 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};"); }