#include <OneButton.h> // tps://github.com/mathertel/OneButton]
const byte buttonPin = 2; // OPTOCOUPLER SIGNAL TO START
const byte resetPin = 4; // OPTOCOUPLER SIGNAL TO RESET COUNTDOWN
const byte relayPin = 13; // 5V RELAY TO ENERGIZE COIL OF A 8 PIN RELAY TO DO THE HEAVY LIFTING
OneButton startButton(buttonPin);
OneButton resetButton(resetPin);
const unsigned long duration = 5000ul; // for testing purpose, only 5 seconds
// const unsigned long duration = 900000ul; // in ms => 15 minutes = 15*60 = 900 seconds
unsigned long chrono;
bool relayIsOn = false;
void relayOff() {
digitalWrite(relayPin, LOW);
relayIsOn = false;
}
void relayOn() {
digitalWrite(relayPin, HIGH);
chrono = millis();
relayIsOn = true;
}
void start() {
if (!relayIsOn) relayOn();
}
void stop() {
relayOff();
}
void setup() {
pinMode(relayPin, OUTPUT);
relayOff();
Serial.begin(115200);
startButton.attachClick(start);
resetButton.attachClick(stop);
}
void loop() {
startButton.tick();
resetButton.tick();
if (relayIsOn && (millis() - chrono >= duration)) relayOff();
}