/*
LEDs toggled with buttons
https://forum.arduino.cc/t/einstieg-arduino/1333397/2
2024-12-18 by noiasca
2024-12-21 reused in https://forum.arduino.cc/t/pressing-two-buttons-at-once/1334416/7
code in thread
*/
#include "button.h"
class Led {
private:
const uint8_t pin; // the GPIO
boolean isActive = false; // is the output on or off
public:
Led (uint8_t pin) : pin(pin) {}
void begin() { // call once in setup
pinMode(pin, OUTPUT);
}
void toggle() { // call to change the state of the output
if (isActive) {
digitalWrite(pin, LOW);
}
else {
digitalWrite(pin, HIGH);
}
isActive = !isActive;
}
};
Led leds[] {8, 9, 10, 11};
constexpr size_t noOfLeds = sizeof(leds) / sizeof(leds[0]);
Button buttons[noOfLeds] {A0, A1, A2, A3};
void setup() {
Serial.begin(115200);
Serial.println("Startup");
for (size_t i = 0; i < noOfLeds; i++) {
leds[i].begin();
buttons[i].begin();
}
}
void loop() {
for (size_t i = 0; i < noOfLeds; i++) {
if (buttons[i].wasPressed()) leds[i].toggle();
}
}
//
Micro Controller
conventional