well i got this code from someone on here. i asked for help creating a reliable timing systems and this is what they gave me. i changed it slightly to make things easier for me and so far its been working perfect. the only thing is when i start the timers it starts at pin 4 runs for a specific duration then moves to pin 5 then 6 then back to pin 4 and just repeats the timers. the problem im having is when i start my arduino the timers always start back at t.timer[0] which mean it always starts at the top of the list. well there are occasions i would like to skip timer 1 and 2 and start the timers on pin 6 then continue normally. i can handle the button side of things but i dont know what to tell the code to do when i push the button to skip the current timer that its on a proceed to the next?
when i want to stop the timers i can call this,
if (somevar == 0) {
for (auto& t : timer) {
t.stop();
digitalWrite(4, HIGH); //off relay 1
digitalWrite(5, HIGH); //off relay 2
digitalWrite(6, HIGH); //off relay 3
}
}
to re enable them i can call this,
if (somevar == 1) {
for (auto& t : timer) {
t.init();
}
but how can i skip the current timer its on and proceed directly to the next?
sorry im everywhere all over the place but right now i have the whole project offline when its being used in the field and i have no idea what im looking at. this code is a level of sophistication im not yet on that level. and i got sidetracked working with python to create a gui application to control this then when i went back to the arduino code to implement the feature to goto the next timer i barley new what i was looking at anymore.
this is the original code that is a little cleaner,
class PumpTimer {
using functPtr = void(*)(void);
public:
PumpTimer(int pumpPin, uint32_t runTime, uint32_t pauseTime, functPtr stoppedAction) : pin(pumpPin), cycleMillis(runTime), offMillis(pauseTime), callback(stoppedAction) {}
void setTime(uint32_t runTime) {
cycleMillis = runTime;
}
void init(void) {
pinMode(pin, OUTPUT);
}
void start(uint32_t duration) {
cycleMillis = duration;
start();
}
void start(void) {
startMillis = millis();
state = ACTIVE_RUNNING;
digitalWrite(pin, HIGH);
}
void process(void) {
switch (state) {
case STOPPED:
break;
case ACTIVE_RUNNING:
if (millis() - startMillis > cycleMillis) {
digitalWrite(pin, LOW);
state = ACTIVE_PAUSED;
startMillis = millis();
}
break;
case ACTIVE_PAUSED:
if (millis() - offMillis > cycleMillis) {
state = STOPPED;
callback();
}
break;
}
}
void stop(void) {
digitalWrite(pin, LOW);
state = STOPPED;
callback();
}
private:
enum {
STOPPED,
ACTIVE_RUNNING,
ACTIVE_PAUSED,
}state = STOPPED;
uint8_t pin;
uint32_t cycleMillis;
uint32_t offMillis;
uint32_t startMillis;
functPtr callback;
};
PumpTimer timer[] = {
/*Pin number, On Time, Pause Time and pointer to callback function */
{ 4, 8*60*1000UL, 12*60*1000UL, [](){timer[1].start();} }, // 8*60*1000UL, 12*60*1000UL,
{ 5, 8*60*1000UL, 12*60*1000UL, [](){timer[2].start();} },
{ 6, 8*60*1000UL, 12*60*1000UL, [](){timer[0].start();} },
};
void setup() {
for (auto& t : timer) {
t.init();
}
timer[0].start();
}
void loop() {
for (auto& t : timer) {
t.process();
}
// do other non-blocking things here...
}