#include "debouncer.h"
#include <EasyButton.h>
const byte BUTTON_PIN = A4;
EasyButton button(BUTTON_PIN);
void onPressed(){
// Your code here to INCREMENT a "state counter".
Serial.println("Button pressed");
}
void onPressedForDuration(){
// Your code here to SET the "state counter" to 0.
Serial.println("Button pressed for duration");
}
enum class States : byte
{
STATE_0,
STATE_1,
STATE_2,
STATE_3,
STATE_4
};
void setup()
{
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
// Initialize the button.
button.begin();
// Add the callback function to be called when the button is pressed.
button.onPressed(onPressed);
// Add the callback function to be called when the button is pressed
// for at least the given time.
button.onPressedFor(2000, onPressedForDuration);
}
void loop()
{
// Continuously read the status of the button.
//button.read();
uint16_t timestamp = millis();
//
// TASK 1: Read the button and update the state.
//
/**/
static States state = States::STATE_0;
static Debouncer button(BUTTON_PIN); // Defaults to a 100 ms repeat delay.
button.Update();
//Serial.println(button_output);
if (button.LowForCount(20)) // 20 repeats of 100 ms each gives 2000 ms;
{
// Reset condition.
state = States::STATE_0;
Serial.println(("Reset"));
Serial.println(byte(state));
}
else if (button.Rise())
{
Serial.println("Rise after fall.");
// Normal operation.
switch (state)
{
case States::STATE_0:
state = States::STATE_1;
Serial.println(byte(state));
break;
case States::STATE_1:
state = States::STATE_2;
Serial.println(byte(state));
break;
case States::STATE_2:
state = States::STATE_3;
Serial.println(byte(state));
break;
case States::STATE_3:
state = States::STATE_4;
Serial.println(byte(state));
break;
case States::STATE_4:
state = States::STATE_0;
Serial.println(byte(state));
break;
}
}
/**/
//
// TASK 2: Blink the inbuilt LED.
//
const uint16_t LED_BLINK_INTERVAL = 200;
static uint16_t led_blink_previous_timestamp = timestamp;
static bool led_state = false;
if (timestamp - led_blink_previous_timestamp >= LED_BLINK_INTERVAL)
{
led_state = !led_state;
digitalWrite(LED_BUILTIN, led_state);
led_blink_previous_timestamp += LED_BLINK_INTERVAL;
}
}