#include <Toggle.h>
const int RELAY_OFF = LOW;
const int RELAY_ON = HIGH;
const byte pumpRelayPin = 9;
const byte boilerRelayPin = 8;
const byte startButtonPin = 3;
const byte stopButtonPin = 2;
const unsigned long waitTime = 5000ul; // 5s in ms
unsigned long startTime;
Toggle startButton, stopButton;
enum {OFF, BOTH, WAIT_A_BIT} state = OFF;
void setup() {
pinMode(pumpRelayPin, OUTPUT); digitalWrite(pumpRelayPin, RELAY_OFF); // pump off
pinMode(boilerRelayPin, OUTPUT); digitalWrite(boilerRelayPin, RELAY_OFF); // boiler off
startButton.begin(startButtonPin);
stopButton.begin(stopButtonPin);
Serial.begin(115200);
}
void loop() {
switch (state) {
case OFF:
startButton.poll();
if (startButton.onPress()) {
digitalWrite(pumpRelayPin, RELAY_ON); // pump ON
digitalWrite(boilerRelayPin, RELAY_ON); // boiler ON
state = BOTH;
}
break;
case BOTH:
stopButton.poll();
if (stopButton.onPress()) {
digitalWrite(boilerRelayPin, RELAY_OFF); // boiler OFF
startTime = millis();
state = WAIT_A_BIT;
}
break;
case WAIT_A_BIT:
stopButton.poll();
if (stopButton.onPress() || (millis() - startTime >= waitTime)) {
digitalWrite(pumpRelayPin, RELAY_OFF); // pump OFF
state = OFF;
}
break;
}
}
PUMP
BOILER
START
STOP