Decoding Code: (for some odd reason this read now fails, even though it worked earlier.)
/* Raw IR decoder sketch!
This sketch/program uses the Arduno and a PNA4602 to
decode IR received. This can be used to make a IR receiver
(by looking for a particular code)
or transmitter (by pulsing an IR LED at ~38KHz for the
durations detected
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
// for MEGA use these!
//#define IRpin_PIN PINE
//#define IRpin 4
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000
// 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
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2]; // pair is high and low pulse
uint8_t currentpulse = 0; // index for pulses we're storing
void setup(void) {
Serial.begin(9600);
Serial.println("Ready to decode IR!");
}
void loop(void) {
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
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
currentpulse=0;
return;
}
}
// 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);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
currentpulse=0;
return;
}
}
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};");
}
IR play code. I changed the trigger to a button.
// This program allows an Arduino to power on/off a Duraflame heater using infrared
//
// Usage: hook up an IR led with Annode on pin 13. Then send serial data "POWER"
// to the arduino. It should flash the LED code.
//
// By Rick Osgood with code borrowed from adafruit.com
//
#define IRledPin 13
#define NumIRsignals 68
const int Button = 6;
int buttonstate = 0;
// This is the code I determined works for my Duraflame heater
int IRsignal[] = {
// ON, OFF (in 10's of microseconds)
498, 432,
90, 140,
88, 144,
88, 142,
90, 24,
82, 36,
66, 50,
64, 50,
66, 50,
64, 168,
62, 168,
64, 166,
62, 54,
62, 54,
60, 56,
58, 58,
56, 58,
58, 58,
56, 174,
58, 58,
56, 58,
58, 58,
58, 58,
58, 56,
58, 58,
56, 174,
58, 58,
56, 174,
58, 172,
58, 174,
58, 172,
58, 172,
58, 174,
56, 4826,
466, 464,
58, 172,
58, 172,
58, 174,
56, 58,
58, 58,
56, 60,
56, 58,
58, 56,
58, 174,
58, 172,
58, 174,
56, 58,
58, 58,
56, 58,
58, 58,
58, 56,
58, 58,
58, 172,
58, 58,
56, 60,
56, 58,
58, 58,
56, 60,
56, 58,
58, 172,
58, 58,
58, 172,
58, 174,
56, 174,
56, 174,
58, 172,
58, 174,
56, 0};
void setup(void) {
pinMode(Button, INPUT);
digitalWrite(IRledPin, LOW); //Make sure LED starts "off"
Serial.begin(9600); //Initialize Serial port
}
void loop() {
char data[6];
int index = 0;
buttonstate = digitalRead(Button);
// delay(1000); //Serial input seems to need some kind of short delay or the data gets screwed up.
// while (Serial.available() > 0) { //Loop if there data on the serial line
// if (index < 5) { //Make sure we don't overflow
// data[index] = Serial.read(); //Load a character into the string
// index++; //Increment the index to get the next character
// }
// }
// data[5]='\0'; //Null terminate the string
if (buttonstate == HIGH){ //If the Arduino receives the POWER signal...
Serial.println("SENDING SIGNAL!");
for (int i = 0; i < NumIRsignals; i+=2) { //Loop through all of the IR timings
pulseIR(IRsignal[i]*10); //Flash IR LED at 38khz for the right amount of time
delayMicroseconds(IRsignal[i+1]*10); //Then turn it off for the right amount of time
}
} //Otherwise do nothing!
}
// This function allows us to PWM the IR LED at about 38khz for the sensor
// Borrowed from Adafruit!
void pulseIR(long microsecs) {
// we'll count down from the number of microseconds we are told to wait
cli(); // this turns off any background interrupts
while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
delayMicroseconds(10); // hang out for 10 microseconds, you can also change this to 9 if its not working
digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
delayMicroseconds(10); // hang out for 10 microseconds, you can also change this to 9 if its not working
// so 26 microseconds altogether
microsecs -= 26;
}
sei(); // this turns them back on
}