struct Rocker {
const byte pin;
void (*callback)(const byte activePin);
byte state;
};
// forward declaration of the callbacks, some could be shared and use the pin to differentiate action
void r2(const byte);
void r3(const byte);
void r4(const byte);
void r5(const byte);
Rocker rockers[] = {{2, r2}, {3, r3}, {4, r4}, {5, r5}};
void r2(const byte pin) {Serial.println(pin);}
void r3(const byte pin) {Serial.println(pin);}
void r4(const byte pin) {Serial.println(pin);}
void r5(const byte pin) {Serial.println(pin);}
void check() {
bool antiBounceNeeded = false;
for (auto && r : rockers) {
byte s = digitalRead(r.pin);
if (s != r.state) {
antiBounceNeeded = true;
r.state = s;
r.callback(r.pin);
}
}
if (antiBounceNeeded) delay(15); // anti bounce of the poor
}
void setup() {
for (auto && r : rockers) {
pinMode(r.pin, INPUT_PULLUP);
r.state = digitalRead(r.pin);;
}
Serial.begin(115200);
}
void loop() {
check();
}