/* Timer with 4 LEDs and 3 Buttons
https://forum.arduino.cc/t/delay-issue-millis-usage/1264070
2024-05-25 by noiasca
to be deleted 2024-09
https://wokwi.com/projects/398844668015443969
*/
class Button { // a simple class for buttons based on the "Debounce" example
const uint8_t buttonPin; // the GPIO / pin for the button
static constexpr byte debounceDelay = 30; // the debounce time; Static because we only need one value for all buttons
const bool active; // is the pin active HIGH or active LOW (will also activate the pullups!)
bool lastButtonState = HIGH; // the previous reading from the input pin
uint32_t lastDebounceTime = 0; // the last time the output pin was toggled
public:
/**
\brief constructor for a button
The constructor takes the GPIO as parameter.
If you omit the second parameter, the class will activate the internal pullup resistor
and the button should connect to GND.
If you set the second parameter to HIGH, the button is active HIGH.
The button should connect to VCC.
The internal pullups will not be used but you will need an external pulldown resistor.
\param buttonPin the GPIO for the button
\param active LOW (default) - if button connects to GND, HIGH if button connects to VCC
*/
Button(uint8_t buttonPin, bool active = LOW) : buttonPin(buttonPin), active(active) {}
/**
\brief set the pin to the proper state
Call this function in your setup().
The pinMode will be set according to your constructor.
*/
void begin() {
if (active == LOW)
pinMode(buttonPin, INPUT_PULLUP);
else
pinMode(buttonPin, INPUT);
}
/**
\brief indicate if button was pressed since last call
@return HIGH if button was pressed since last call - debounce
*/
bool wasPressed() {
bool buttonState = LOW; // the current reading from the input pin
byte reading = LOW; // "translated" state of button LOW = released, HIGH = pressed, despite the electrical state of the input pint
if (digitalRead(buttonPin) == active) reading = HIGH; // if we are using INPUT_PULLUP we are checking invers to LOW Pin
if ((millis() - lastDebounceTime) > debounceDelay) { // If the switch changed, AFTER any pressing or noise
if (reading != lastButtonState && lastButtonState == LOW) { // If there was a change and and last state was LOW (= released)
buttonState = HIGH;
}
lastDebounceTime = millis();
lastButtonState = reading;
}
return buttonState;
}
};
const uint8_t ledPin[4] = {8, 9, 10, 11}; // indicator LEDs
const uint8_t interval[4] = {5, 10, 15, 20}; // interval for each LED
const uint32_t factor = 1000; // 60 * 1000UL for minutes
const uint8_t relayPin = 7; // GPIO for the output
const uint8_t buttonPin1 = 12; // Min Select
const uint8_t buttonPin2 = 13; // Start
const uint8_t buttonPin3 = 6; // Stop Test
enum State {IDLE, RUNNING} state;
int8_t presses = 0; // counts how often the select button was pressed
uint32_t previousMillis; // time management
Button selectBtn{buttonPin1};
Button startBtn{buttonPin2};
Button stopBtn{buttonPin3};
// switches some indicator LEDs on:
void indicate(byte count = state) {
for (size_t i = 0; i < sizeof(ledPin) / sizeof(ledPin[0]); i++ ) {
if (count >= i)
digitalWrite(ledPin[i], HIGH);
else
digitalWrite(ledPin[i], LOW);
}
}
void runFSM() {
switch (state) {
case IDLE :
if (selectBtn.wasPressed()) {
presses = ++presses % (sizeof(ledPin) / sizeof(ledPin[0]));
indicate(presses);
Serial.println(presses);
}
if (startBtn.wasPressed()) {
Serial.print("start for "); Serial.print(interval[presses] * factor); Serial.println(" ms");
state = RUNNING;
previousMillis = millis();
digitalWrite(relayPin, HIGH);
}
break;
case RUNNING :
if ((millis() - previousMillis > (interval[presses] * factor)) || stopBtn.wasPressed() ) {
Serial.println("stop");
state = IDLE;
digitalWrite(relayPin, LOW);
presses = 0;
indicate(state);
}
// optional update indicator
break;
}
}
void setup() {
Serial.begin(115200);
selectBtn.begin();
startBtn.begin();
stopBtn.begin();
pinMode(relayPin, OUTPUT);
indicate(state);
}
void loop() {
runFSM();
}