/*
Wokwi: https://wokwi.com/projects/359009955305213953
Demo sketch with two buttons (but4 and but5) connected to Arduino MEGA board that
1. starts counting up (slowly) when solely button but4 is pressed
2. starts counting down (slowly) when solely button but5 is pressed
3. starts counting up (quickly) when but5 is pressed while but4 is hold down
4. starts counting up (quickly) when but4 is pressed while but5 is hold down
To achieve slow counting as in no 1. and 2. the respective buttons have to be pressed. After
2 seconds the counting starts.
If the second button is pressed within these 2 seconds, quick counting as in no 3. or no. 4 starts.
The further behaviour is like this:
* In case of no. 1 or no. 2
the counting stops if the respective button has been released.
the counting starts again if the respective button is pressed.
* In case of no. 3 or no. 4
the counting stops when the "second" button has been released
the counting starts again if the "second" button is pressed
the counting stops when the first button (but4 in no. 3, but5 in no. 4) is released, but
if the "second" button is not released within 2 seconds the slow(!) counting starts depending
on the still pressed button
Version: V0.1 - 2023-03-12
ec2021
*/
#include "ezButton.h"
const byte IN4 = 4;
const byte IN5 = 5;
ezButton but4(IN4);
ezButton but5(IN5);
signed long count = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start");
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
}
void Refresh() {
but4.loop();
but5.loop();
}
unsigned long but4PressTime;
boolean but4Used;
unsigned long but5PressTime;
boolean but5Used;
void loop() {
Refresh();
if (but4.getState() != 1) {
but4PressTime = millis();
but5Used = false;
while (but4.getState() != 1)
{
Refresh();
if (millis() - but4PressTime > 2000 && !but5Used) {
count++;
Serial.print("Slow count up\t");
Serial.println(count);
delay(300);
}
else
{
if (but5.getState() != 1) {
but5Used = true;
count++;
Serial.print("Quick count up\t");
Serial.println(count);
delay(100);
}
}
}
}
if (but5.getState() != 1) {
but5PressTime = millis();
but4Used = false;
while (but5.getState() != 1)
{
Refresh();
if (millis() - but5PressTime > 2000 && !but4Used) {
count--;
Serial.print("Slow count down\t");
Serial.println(count);
delay(300);
}
else
{
if (but4.getState() != 1) {
but4Used = true;
count--;
Serial.print("Quick count down\t");
Serial.println(count);
delay(100);
}
}
}
}
}