Hi All,
Arduino type: MEGA 1280.
This is what I am trying to do:
I have a cyclic event occurring which is interrupting the processor via INT0. This event is derived from the mains on the secondary side of the power transformer which provides a pulse every 20mS, ie half wave rectified 50Hz mains filtered and voltage limited to drive Pin 2. This is working just fine. I see pulses on this pin at the expected frequency and the interrupt is just fine and I can count these pulses.
Now I am interested in detecting dropped cycles where the mains has "glitched". My power supply has sufficient storage capacity to withstand a considerable mains outage (about 5 seconds) yet maintain an adequate supply to the MEGA regulator.
What I had intended was to use a timer (eg Timer1) with a period of 21 millseconds (just over 1 cycle of 50Hz) where I had attached an interrupt routine executed when the Timer ran out. The plan was to use restart (eg Timer1.restart()) when the mains interrupt occurred. If the mains is running normally the timer is always restarted before it completes and the timer interrupt routine never executes. If the mains glitches due to a failed cycle and the time reaches 21 mS the Timer routine executes and I know that I have a missed cycle. The code fragment looks like this:
#include <TimerOne.h>
const int what_is_happening = 8; // I have a LED on pin 8, normally off
void mainsinterrupt()
{
Timer1.restart(); // timer reset every mains pulse
}
void Mains_Cycle_Timeout()
{
digitalWrite(what_is_happening, HIGH); // turn the LED on when a cycle is skipped
Timer1.restart(); // and set up again for another missed cycle unless the mains returns
}
void setup()
{
attachInterrupt(0, mainsinterrupt, FALLING);
Timer1.initialize(21000); // set the timer up for just over one cycle
Timer1.attachInterrupt(Mains_Cycle_Timeout);
pinMode(what_is_happening, OUTPUT); // configure the test output
digitalWrite(what_is_happening, LOW); // and make it low
}
void loop()
{
// loop is redundant here - all the action in interrupts
}
However when I flick the mains off/on the LED remains stubbornly off. What am I missing here?
Thanks, Fred