Hello everyone.
I have the YUN and have looked at several different examples of time and timer functions and libraries.
One that I thought might work is from the Simon Monk updated libary here: GitHub - JChristensen/Timer: A fork of Simon Monk's Arduino Timer library
Here is this simple example from the download:
#include "Timer.h"
Timer t;
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
t.pulse(pin, 10 * 1000, HIGH); // 10 seconds
// t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes
}
void loop()
{
t.update();
}
For my own purpose I encapsulated the code into a set of functions:
void onTimer (void)
{
pinMode(pin, OUTPUT); //pin 13 output- pin already defined earlier so I can turn on the LED
digitalWrite(pin,LOW); //force to off
t.pulse(pin, 2 * 60 * 1000, HIGH); // on for 2 minutes
Console.println("Timer turned on");
Console.println();
}
void offTimer (void)
{
t.pulse(pin, 3 * 60 * 1000, LOW); // off for 3 minutes
Console.println("Timer turned off");
Console.println();
//essentially after this t.pulse() has finished, everything s/b reset and ready to be responded to in the main loop again.
}
I call the "onTimer" function from inside "void loop(void)" from an event driven "if" statement pending the meeting of certain criteria. When I run my sketch, I can successfully get to the "println" statement in the "onTimer" function just fine and my LED is lit up.
My problem is, after the time has elapsed, nothing changes. I mean the "onTimer" function, specifically the:
t.pulse(pin, 2 * 60 * 1000, HIGH);
does not automatically reset the pin to LOW.
Not mentioned on our site here:
http://playground.arduino.cc/code/timer
but present in the Timer.cpp file is this wording and I wonder if this may work;
/**
* This method will generate a pulse of startingValue, starting immediately and of
* length period. The pin will be left in the !startingValue state
*/
int8_t Timer::pulseImmediate(uint8_t pin, unsigned long period, uint8_t pulseValue)
{
int8_t id(oscillate(pin, period, pulseValue, 1));
// now fix the repeat count
if (id >= 0 && id < MAX_NUMBER_OF_EVENTS) {
_events[id].repeatCount = 1;
}
return id;
}
The key here as per the library is "The pin will be left in the !startingValue state". But actually, this code does not seem to reset the pin to its !startingValue state--if it did that would be great!!! I could code for that event and have it trigger something else. Maybe I did something wrong. always possible.
As it is though, I am wanting to get a total of 5 minutes per cycle using a software timer. Two minutes on and 3 minutes off. Also, it does not appear that any two statements can be combined into a single function thru this library. Each has to be alone and called separately.
Repeating essentially as above:
Another sticking point is actually responding to when a timer is finished (if indeed the pin is not set to the !startingValue state) and then doing the next code, function "void offTimer (void)" in this case. If this were merely a hardcoded external event control like it is for starting the first timer, it would be easier since some external event would trigger the "void offTimer (void)," but it isn't .
I didn't see any particular function in the *.h or *.cpp files giving me that ability, although there is a "stop()" function that can stop a timer based on knowing its id. The problem still remains that the stop needs to be triggered by either the event finishing and notifying me, or some other timer/counter telling me it's done which must be based on my timer input length parameter (2 min, 3 min, etc), or a button click, etc;. Not only that, I wouldn't necessarily want to stop my timer until it was finished unless, I needed a panic button, but it could be useful.
So I suspect there must be something not really clear to me here. If anyone has a theory or actually knows of code that works and which would go on working perpetually weeks or months at a time after loading my sketch, then I would love to see this. Please don't suggest delay()-its not practical in this situation.
Thanks for everyone's help here in our forums.
houdinihar