I'm now trying to integrate the code with some code i have had working to send a IR signal.
Sometimes the led stays high, I guess it could be due to the button press being to long? The orignal code used a serial command so only would run one, I tried adding a delay of 200 after signal is sent but didn't help.
Not sure if the new code is messing with the interrupts as it seems to disable then re-enable them?
Thanks
#include <avr/sleep.h>
//const byte LEDA = 13;
const byte LEDB = 12;
#define IRledPin 13
#define NumIRsignals 76
// This is the code I determined works for my Duraflame heater
int IRsignal[] = {
// ON, OFF (in 10's of microseconds)
884, 436,
58, 52,
58, 162,
58, 50,
58, 162,
58, 162,
56, 162,
58, 162,
58, 52,
56, 162,
58, 52,
58, 160,
58, 52,
58, 52,
58, 50,
58, 52,
58, 162,
58, 160,
58, 162,
58, 162,
56, 162,
58, 162,
58, 50,
60, 50,
58, 52,
58, 52,
58, 50,
58, 52,
58, 52,
56, 52,
58, 162,
58, 160,
58, 162,
58, 3900,
882, 216,
58, 2844,
882, 216,
58, 0};
volatile byte wakeUpPin;
void wake_A ()
{
wakeUpPin = 2;
// must do this as the pin will probably stay low for a while
detachInterrupt (0);
// cancel sleep as a precaution
sleep_disable();
} // end of wake_A
void wake_B ()
{
wakeUpPin = 3;
// must do this as the pin will probably stay low for a while
detachInterrupt (1);
// cancel sleep as a precaution
sleep_disable();
} // end of wake_B
void setup ()
{
pinMode(IRledPin, OUTPUT);
digitalWrite(IRledPin, LOW); //Make sure LED starts "off"
Serial.begin(9600); //Initialize Serial port
digitalWrite (2, HIGH); // enable pull-up
digitalWrite (3, HIGH); // enable pull-up
} // end of setup
void loop ()
{
if (wakeUpPin == 2)
{
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
}
}
else if (wakeUpPin == 3)
{
pinMode (LEDB, OUTPUT);
digitalWrite (LEDB, HIGH);
delay (100);
digitalWrite (LEDB, LOW);
delay (100);
pinMode (LEDB, INPUT);
}
// disable ADC
ADCSRA = 0;
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Do not interrupt before we go to sleep, or the
// ISR will detach interrupts and we won't wake.
noInterrupts ();
// will be called when pin D2 goes low
attachInterrupt (0, wake_A, LOW);
// will be called when pin D3 goes low
attachInterrupt (1, wake_B, LOW);
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR = bit (BODS);
// We are guaranteed that the sleep_cpu call will be done
// as the processor executes the next instruction after
// interrupts are turned on.
interrupts (); // one cycle
sleep_cpu (); // one cycle
} // end of loop
// This function allows us to PWM the IR LED at about 38khz for the sensor
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
}