/*
Make distinction between long and short press
Based on example at bottom of:
https://forum.arduino.cc/t/long-short-button-press-issue/472257/10
*/
#include <Bounce2.h>
#define buttonPin 8
// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
// define times for short and long presses:
#define shortTime 500
#define longTime 1000
#define noEvent 0
#define shortPress 1
#define longPress 2
// Instantiate a Bounce object called button :
Bounce button = Bounce();
unsigned long buttonPressStartTimeStamp;
unsigned long buttonPressDuration;
boolean startTimeout = false;
boolean endTimeout = false;
byte event;
// variables for time management:
unsigned long previousMillis = 0; // will store last time timer was updated
const unsigned long interval = 1000; // 1 second
int state = 0; // blue (0), yellow (1) and red (2), finished (3)
void setup() {
Serial.begin(115200);
Serial.println("Start");
pinMode(buttonPin, INPUT_PULLUP);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
button.attach(buttonPin);
button.interval(20);//set debounce interval
reset();
}
void loop() {
event = checkButton();
switch (event) {
case shortPress:
Serial.println("Short press");
if ( state<3 ) {
Serial.print("Press state="); Serial.println(state);
state=2; // finish
// random color:
change_color(random(3));
// beep
}
break;
case longPress:
Serial.println("Long press");
reset();
break;
case noEvent:
// nothing...
break;
}
// blinking task (every second):
unsigned long currentMillis = millis();
// task 1 - blink with 3 colors:
if ((currentMillis - previousMillis > interval) && state<3 ) { // interval passed?
previousMillis = currentMillis; // save the last time
//state: blue (0), yellow (1) and red (2), finished (3)
change_color(state);
// next state:
state++;
if (state==3) state=0;
}
}
byte checkButton() {
byte event = noEvent;
// Update the Bounce instance, does digitalRead of button
button.update();
// Button press transition from HIGH to LOW)
if (button.fell())
{
buttonPressStartTimeStamp = millis();
startTimeout = true;
}
// Button release transition from LOW to HIGH) :
if (button.rose())
{
buttonPressDuration = (millis() - buttonPressStartTimeStamp);
startTimeout = false;
}
if (buttonPressDuration > 0 && buttonPressDuration <= shortTime)
{
event = shortPress;
buttonPressDuration = 0;
}
if (startTimeout == true && (millis() - buttonPressStartTimeStamp) > longTime)
{
event = longPress;
startTimeout = false;
buttonPressDuration = 0;
}
return event;
}
void change_color(int color_number) {
if (color_number==-1) { // off
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
} else if (color_number==0) { // blue
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
} else if (color_number==1) { // yellow
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
} else if (color_number==2) { // red
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
}
void reset() {
Serial.println("reset()");
change_color(-1);
delay(1000);
while (checkButton()!=shortPress) ; // wait until button is pressed...
Serial.println("ON");
// red for 2 seconds:
change_color(2);
delay(2000);
// off for 1 second:
change_color(-1);
delay(1000);
state = 0;
}