#include <FastLED.h>
#define LED_PIN 3 // Pin pour le bandeau LED WS2812B
#define NUM_LEDS 5 // Nombre de LEDs dans le bandeau
#define LED_STATUS 12
#define BTN1 7
#define BTN2 6
// Initialiser le bandeau LED
CRGB leds[NUM_LEDS];
CRGB colors[] = {CRGB::White, CRGB::Red, CRGB::Yellow, CRGB::Green, CRGB::Blue};
// Définir le tableau contenant les indices des LED
unsigned char BTN_LEDS[4] = {11, 10, 9, 8};
// LED status type
enum LedStatus : unsigned char { lsOff = 0, lsOn = 1, lsFlashing = 2 };
// Status we want to share with the buttons
LedStatus ledStatus[4] = {lsOff, lsOff, lsOff, lsOff};
bool buttonEnabled[4] = {false, false, false, false};
bool buttonConnected[4] = {false, false, false, false};
bool hasAnswered[4] = {false, false, false, false};
unsigned long lastContact[4] = {0, 0, 0, 0};
unsigned long lastLoopTime = 0;
bool isReady = false;
void setup() {
Serial.begin(115200);
while (!Serial) {};
// Initialiser le bandeau LED FastLED
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(LED_STATUS, OUTPUT);
pinMode(BTN1, INPUT_PULLUP);
pinMode(BTN2, INPUT_PULLUP);
for (char channel = 0; channel < 4; channel++) {
pinMode(BTN_LEDS[channel], OUTPUT);
digitalWrite(BTN_LEDS[channel], LOW);
}
}
void loop() {
lastLoopTime = millis();
if (digitalRead(BTN2) == LOW) {
for (unsigned char button = 0; button < 4; button++) {
ledStatus[button] = lsOff;
buttonEnabled[button] = false;
hasAnswered[button] = false;
}
isReady = false;
digitalWrite(LED_STATUS, LOW);
leds[0] = CRGB::Black;
FastLED.show();
} else if (digitalRead(BTN1) == LOW) {
for (unsigned char button = 0; button < 4; button++) {
buttonEnabled[button] = !hasAnswered[button];
ledStatus[button] = hasAnswered[button] ? lsOff : lsFlashing;
}
isReady = true;
digitalWrite(LED_STATUS, HIGH);
leds[0] = colors[0];
FastLED.show();
}
// Update our LEDs and monitor for ones that are out of contact
for (unsigned char button = 0; button < 4; button++) {
// If the button is connected
if (buttonConnected[button]) {
// If its been 1 second since we heard from it
if (lastLoopTime - lastContact[button] > 1000) {
// Disconnect it
buttonConnected[button] = false;
digitalWrite(BTN_LEDS[button], LOW);
} else {
// Set the LED to match the state we have it in
digitalWrite(BTN_LEDS[button],(ledStatus[button] == lsOn) || ((ledStatus[button] == lsFlashing) && (lastLoopTime & 255) > 128));
if (ledStatus[button] == lsOn) {
for (int i = 1; i < 5; i++) {
leds[i] = colors[i];
}
FastLED.show();
} else if ((ledStatus[button] == lsFlashing) && (lastLoopTime & 255) > 128) {
for (int i = 1; i < 5; i++) {
leds[i] = (lastLoopTime & 255) > 128 ? colors[i] : CRGB::Black;
}
FastLED.show();
} else {
for (int i = 1; i < 5; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}
}
} else {
// For disconnected ones we just give a short 'blip' once per few second
digitalWrite(BTN_LEDS[button], (lastLoopTime & 2047) > 2000);
for (int i = 1; i < 5; i++) {
leds[i] = (lastLoopTime & 2047) > 2000 ? colors[i] : CRGB::Black;
}
FastLED.show();
}
}
}