Hi everyone! Have an interesting problem here... I am using the MSTimer2 and EventFuse libraries to enable different intervals for on and off times of a light. In the following code I have rem'd out all outputs except one for testing reasons. The program counts "ticks" instead of counting millis thus providing an easy way to control multiple interrupts(&outputs) for several different events at the same time, without the delay function which stops all events until delay is complete.
The issue here is this: any settings below 120 ticks(seconds) works fine. If I set it to run the interval for 300 ticks(5 minutes worth) it goes low (off) at 44 ticks(seconds). If I set it to go on for 480 ticks (8 minutes) it runs for 3 minutes, 44 seconds! Like I said before though... it is VERY accurate at anything below 120 seconds. Here's the code:
/*
*
* Description:
* EventFuse example demonstrating control of
* multiple independent switched outputs. Each
* output can be configured with independent
* on and off durations with a minimum of 1 second
* and a maximum of about 1100 hours (2^32 mS).
*/
#include <EventFuse.h>
#include <MsTimer2.h>
#define OutputCount 4
// These would be better handled as enums,
// but that requires a seperate .h file.
#define OffTime 0
#define OnTime 1
#define OutputPin 2
// The outputs array defines how long each output will
// be turned off, on, and what pin to use for that output.
// The off and on values are in units of 'ticks'. The length
// of a tick is controlled by the setup of MsTimer2.
// 3600 = 1 HOUR. 64800 = 16 HOURS. 21600 = 6 HOURS 1 = 1 SECONDS
// off,on,pin
//HERE IS THE CODE TO DETERMINE THE AMT OF TICKS FOR ON-OFF-PIN:
//I REMARKED OUT THE OTHER 3 OUTPUTS FOR TESTING.
byte outputs[OutputCount][3] ={{3,960,7}}; // Light
//{30,4,6}, // Mister
//{50,3,5}, // Water Pump
//{20, 2,4}}; // FANS
void OutputHandler(FuseID fuseID, int outputID){
// look up the pin associated with this output
byte pin = outputs[outputID][OutputPin];
// get and invert the current pin state and write
// it back to the port to invert the current pin state.
int state = 1&~digitalRead(pin);
digitalWrite( pin, state );
// Reset the fuse length with a new interval. The current state
// of the pin is used to determine which interval should be used.
eventFuse[fuseID].fuseLen = outputs[outputID][state];
}
void timerTick(){
eventFuse.burn(1);
}
void setup() {
// Set up and init all outputs to off
for(byte i = 0; i<OutputCount; i++){
pinMode( outputs[i][OutputPin], OUTPUT);
digitalWrite( outputs[i][OutputPin], LOW );
// Set up an event fuse for this output.
eventFuse.newFuse( i, outputs[i][OffTime], INF_REPEAT, OutputHandler );
}
// Set MsTimer2 for one second per tick.
MsTimer2::set(1000, timerTick );
MsTimer2::start();
}
void loop(){
}
The libraries are untouched downloaded straight from the author's site. MsTimer2 and FlexiTimer2 Arduino Libraries, Run a Function At Regular Intervals
Anyone else have this issue with MSTimer2/EventFuse?!? Or possibly see an error in the code?
Any help would be much appreciated...
Thanks! S2H