# 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);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
# define WINDOW 1000
static unsigned long counter;
void loop() {
static unsigned char pst;
static bool waiting;
static unsigned long start;
unsigned long now = millis();
unsigned char was = pst;
pst |= oneButtonK() ? 0x1 : 0;
pst |= twoButtonK() ? 0x2 : 0;
if (!was && pst) {
start = now;
digitalWrite(9, HIGH);
}
if (pst == 0x3) {
Serial.println("both edges seen in time");
// set act flag and reset mechanism
pst = 0;
digitalWrite(9, LOW);
}
else if (pst) {
if (now - start > WINDOW) {
Serial.print(" "); Serial.println(pst);
Serial.println("t o o s l o w");
pst = 0;
digitalWrite(9, LOW);
}
}
delay(20);
}
void loopTHE() {
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(10);
}
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!