/*
LEDs toggled with buttons
https://forum.arduino.cc/t/einstieg-arduino/1333397/2
2025-09-29 https://forum.arduino.cc/t/midi-controller-part-3/1407693/
code fehlt in thread
V2: Channel beinhalted LEDs und Buttons
V1: Channel für LED+Button, kombiniert in Gruppe
*/
#include "button.h"
template <size_t noOfChannels>
class Group {
Button button[noOfChannels]; // one group consists of several Buttons
const uint8_t ledPin[noOfChannels]; // several LED GPIOs
boolean isActive[noOfChannels]; // is the LED on
uint8_t mode = 'A'; // 'A' only a single channel is active, 'B' several channels can be active
public:
Group (const uint8_t ledA, const uint8_t buttonA, const uint8_t ledB, const uint8_t buttonB, const uint8_t ledC, const uint8_t buttonC, const uint8_t ledD, const uint8_t buttonD) :
ledPin {ledA, ledB, ledC, ledD}, button {buttonA, buttonB, buttonC, buttonD} {}
void begin() {
for (size_t i = 0; i < noOfChannels; i++) {
pinMode(ledPin[i], OUTPUT);
button[i].begin();
}
}
void setModeA() {
mode = 'A';
}
void setModeB() {
mode = 'B';
}
void update() {
for (size_t i = 0; i < noOfChannels; i++) {
if (button[i].wasPressed()) {
if (mode == 'A' && isActive[i] == false) { // only react on a new button Press in mode A (no switch off with same button press)
digitalWrite(ledPin[i], HIGH);
isActive[i] = true;
for (size_t j = 0; j < noOfChannels; j++) {
if (j != i) {
digitalWrite(ledPin[j], LOW); // switch off all other channels. todo: only in mode A
isActive[j] = false;
}
}
}
else if (mode == 'B') {
if (isActive[i]) {
digitalWrite(ledPin[i], LOW);
isActive[i] = false;
}
else {
digitalWrite(ledPin[i], HIGH);
isActive[i] = true;
}
}
}
}
//@todo: react on button[0].longPressed() and
//@todo: react on button[1].longPressed()
}
};
Group<4> groupA { 8, A0, 9, A1, 10, A2, 11, A3};
void setup() {
Serial.begin(115200);
Serial.println(F("Startup"));
groupA.begin();
//groupA.setModeB(); // reactivate for mode B
}
void loop() {
groupA.update();
}
//a Group of several LEDs+Buttons