# define button1 2
# define button2 3
# define b1State 4
# define b2State 5
void setup() {
Serial.begin(115200);
Serial.println("\TEST\n");
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(8, OUTPUT);
}
static unsigned long counter;
void loop() {
static bool one = false;
bool two = false;
counter++;
one |= oneButtonK();
if (one) {
digitalWrite(8, HIGH);
two = twoButtonK();
while (!two)
two = twoButtonK();
}
digitalWrite(8, LOW);
if (two) {
Serial.println(" temporal congruence ");
one = false;
}
delay(777);
}
bool oneButtonK()
{
static bool lastButton;
bool isPressed = false;
digitalWrite(b1State, lastButton ? HIGH : LOW);
bool thisButton = digitalRead(button1) == LOW; // true = pressed
if (lastButton != thisButton) {
lastButton = thisButton;
if (thisButton) isPressed = true;
}
return isPressed;
}
bool twoButtonK()
{
static bool lastButton;
bool isPressed = false;
digitalWrite(b2State, lastButton ? HIGH : LOW);
bool thisButton = digitalRead(button2) == LOW; // true = pressed
if (lastButton != thisButton) {
lastButton = thisButton;
if (thisButton) isPressed = true;
}
return isPressed;
}
bool twoButtonK_() // delay the second button press by N loop cycles
{
static bool buffer[8];
static byte readI;
static byte writeI;
static bool lastButton;
bool isPressed = false;
bool thisButton = digitalRead(button2) == LOW; // true = pressed
if (lastButton != thisButton) {
lastButton = thisButton;
if (thisButton) isPressed = true;
}
buffer[writeI++] = isPressed;
writeI &= 0x7;
isPressed = buffer[readI++];
readI &= 0x7;
return isPressed;
}
// test fragments and functions
// test button handling
void loopTEST() {
counter++;
bool one = oneButtonK();
bool two = twoButtonK();
if (one) {
Serial.print(counter);
Serial.println(" [] one got pressed?");
}
if (two) {
Serial.print(counter);
Serial.println(" two got pressed?");
}
delay(20);
}
void loop0() {
bool one = digitalRead(2) == LOW;
bool two = digitalRead(3) == LOW;
if (one != two) Serial.println("mismatch");
}
NO BOUNCE!