hi im using this code to run some timers. the problem im having is i cant figure out how to skip one time and go to the next when i press a button. i can figure out the button code i just dont know how to call back to the class and switch to the next timer. hopefully you understand what i mean.
class PumpTimer {
using functPtr = void(*)(void);
public:
PumpTimer(int pumpPin, uint32_t runTime, uint32_t pauseTime, functPtr stoppedAction = nullptr) : pin(pumpPin), cycleMillis(runTime), offMillis(pauseTime), callback(stoppedAction) {}
void setTime(uint32_t runTime) {
cycleMillis = runTime;
}
void setTime(uint32_t runTime, uint32_t pauseTime) {
cycleMillis = runTime;
offMillis = pauseTime;
}
uint8_t getPin() {
return pin;
}
void setPauseTime(uint32_t pauseTime) {
offMillis = pauseTime;
}
void init(void) {
pinMode(pin, OUTPUT);
}
void start(uint32_t duration) {
cycleMillis = duration;
start();
}
void start(void) {
startMillis = millis();
state = ACTIVE_RUNNING;
digitalWrite(pin, LOW);
pumponstate = 0;
digitalWrite(BLUE_PIN, HIGH);
}
void process(void) {
switch (state) {
case STOPPED:
break;
case ACTIVE_RUNNING:
if (millis() - startMillis > cycleMillis) {
digitalWrite(pin, HIGH);
state = ACTIVE_PAUSED;
startMillis = millis();
pumponstate = 1;
digitalWrite(BLUE_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
}
break;
case ACTIVE_PAUSED:
if (millis() - startMillis > offMillis) {
state = STOPPED;
pumponstate = 1;
digitalWrite(BLUE_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
if (callback) {
callback();
}
}
break;
}
}
void stop(void) {
digitalWrite(pin, HIGH);
state = STOPPED;
callback();
}
public:
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, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 1 pin 4 /////
timer[1].start(); /////
} /////
}, /////
{ 5, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 2 pin 5 /////
timer[0].start(); /////
// } /////
// }, /////
// { 6, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 3 pin 6 /////
// timer[2].start(); /////
} /////
}, /////
};
for example this part of the code,
{ 4, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 1 pin 4
timer[1].start();
}
},
{ 5, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 2 pin 5
timer[0].start();
}
},
{ 6, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 3 pin 6
timer[2].start();
}
},
};
i would like to skip
{ 4, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 1 pin 4
timer[1].start();
and move directly to
{ 5, Pump_Timer(ontime1), Pump_Timer(offtime1), []() { //socket 2 pin 5
timer[0].start();
}
when i press a button. i just cant figure out how to call for such a thing?
i tried chopping it up and using somthing like this but with no luck..,
for (auto& t : timer) {
t.startMillis = (millis() + t.cycleMillis);
delay(100);
t.startMillis = (millis() + t.startMillis);
t.stop();
t.start();
}