#include <Arduino.h>
#ifndef PI
#define PI 3.14159265358979323846
#endif
const uint8_t LED_PINS[5] = {3, 5, 6, 9, 10};
const uint8_t BUTTON_PIN = 2; // push button -> GND (INPUT_PULLUP)
// Button timing
const unsigned long DEBOUNCE_MS = 30;
const unsigned long LONGPRESS_MS = 1000;
// Breathing
const unsigned long BREATH_PERIOD_MS = 3000;
const uint8_t MAX_BRIGHTNESS = 255;
// State
uint8_t players = 0; // 0 = standby, anders 1..5
bool btnLast = HIGH;
unsigned long btnChangeAt = 0;
unsigned long btnPressedAt = 0;
void setAllOff() {
for (uint8_t i = 0; i < 5; i++) analogWrite(LED_PINS[i], 0);
}
// Spreidingspatroon volgens jouw mapping
// index 0..4 = LED1..LED5
bool patternOn(uint8_t nPlayers, uint8_t ledIndex) {
static const bool map[6][5] = {
{0,0,0,0,0}, // 0 spelers (standby)
{1,0,0,0,0}, // 1 -> LED 1
{0,0,1,1,0}, // 2 -> LED 3 & 4
{1,0,1,1,0}, // 3 -> LED 1,3,4
{0,1,1,1,1}, // 4 -> LED 2,3,4,5
{1,1,1,1,1} // 5 -> alle
};
if (nPlayers > 5) nPlayers = 5;
return map[nPlayers][ledIndex];
}
void setup() {
for (uint8_t i = 0; i < 5; i++) {
pinMode(LED_PINS[i], OUTPUT);
analogWrite(LED_PINS[i], 0);
}
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
bool btnNow = digitalRead(BUTTON_PIN);
unsigned long now = millis();
// Debounce + short/long press
if (btnNow != btnLast && (now - btnChangeAt) > DEBOUNCE_MS) {
btnChangeAt = now;
if (btnNow == LOW) {
btnPressedAt = now;
} else {
unsigned long held = now - btnPressedAt;
if (held >= LONGPRESS_MS) {
players = 0; // long press -> standby
} else {
// short press -> 0->1, 1..5 cyclisch
players = (players == 0) ? 1 : (players % 5) + 1;
}
}
btnLast = btnNow;
}
// Breathing voor de actieve leds in het patroon
if (players == 0) {
setAllOff();
} else {
float phase = (now % BREATH_PERIOD_MS) / (float)BREATH_PERIOD_MS; // 0..1
uint8_t breath = (uint8_t)((sin(phase * 2.0f * PI) * 0.5f + 0.5f) * MAX_BRIGHTNESS);
for (uint8_t i = 0; i < 5; i++) {
if (patternOn(players, i)) analogWrite(LED_PINS[i], breath);
else analogWrite(LED_PINS[i], 0);
}
}
delay(5);
}