Im glad I have found out the first part by myself. But to be honest, I don´t know why it works.
Well, in the original code we only clear all LEDs and set the selected LED color, only IF a button was pressed (or we went beyond the 5 minute marker).
The selected LED is stored in CurrentLED, which is -1 if nothing was selected.
By the way the more correct method would be:
FastLED.clear(); // set all LEDs to black
if (CurrentLED>-1) { // Set LED color of selected LED
leds[CurrentLED] = myColors[CurrentLED];
}
FastLED.show(); // Show LEDs
This to avoid that we are going to use a negative index for the "leds[]" and "myColors[]" arrays (which theoretically could cause errors, since we'd be addressing an invalid memory location, even though the Arduino doesn't seem to be bothered by it).
I'm not sure if I understood your explanation of what happens though.
As for pressing multiple buttons at the same time: This would mean we'd need to redo some of the coding.
Right now we save one button press only in CurrentLED, which would not work as expected when pressing multiple buttons.
In the (untested) code below, I'm still using CurrentLED, so I can see if a button was pressed and/or to set the LEDs after the 5 minute marker.
For this to work I needed to move the FastLED.clear more to the front, and test each button and set it's LED if applicable.
I hope this works and makes sense 😉
#define FASTLED_INTERNAL // just used to mute the Pragma messages when compiling
#include <FastLED.h>
#define NUM_LEDS 4
#define LED_PIN 5
#define buttonPin2 10
#define buttonPin3 9
#define buttonPin4 8
#define buttonPin5 7
// time slots when nothing was pressed: 5 minutes = 300 seconds = 300,000 milliseconds
#define NotPressed1 300000
#define NotPressed2 330000
#define NotPressed3 360000
#define NotPressed4 390000
#define NotPressed5 420000
unsigned long LastPressed = 0; // 0 = ignore lastPressed
CRGB leds[NUM_LEDS];
CRGB myColors[4] = {
CRGB(255, 0, 0),
CRGB(0, 255, 0),
CRGB(0, 0, 255),
CRGB(255, 0, 255) };
void setup() {
//FastLED.addLeds<WS2812B, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds < SK6812, LED_PIN, RGB > (leds, NUM_LEDS);
FastLED.setBrightness(20);
FastLED.clear();
FastLED.show();
pinMode(buttonPin2, INPUT_PULLUP); // switch connected to buttonPin2 and GND
pinMode(buttonPin3, INPUT_PULLUP); // switch connected to buttonPin3 and GND
pinMode(buttonPin4, INPUT_PULLUP); // switch connected to buttonPin4 and GND
pinMode(buttonPin5, INPUT_PULLUP); // switch connected to buttonPin5 and GND
LastPressed = millis();
}
void loop() {
FastLED.clear(); // set all LEDs to black
// Read buttons until we find one that is pressed (this can be done more elegant)
if (digitalRead(buttonPin2) == LOW) { // try LOW instead of HIGH
leds[0] = myColors[0];
CurrentLED = 0;
}
if (digitalRead(buttonPin3) == LOW) {
leds[1] = myColors[1];
CurrentLED = 1;
}
if (digitalRead(buttonPin4) == LOW) {
leds[2] = myColors[2];
CurrentLED = 2;
}
if (digitalRead(buttonPin5) == LOW) {
leds[3] = myColors[3];
CurrentLED = 3;
}
// if a button was pressed set LastPressed
if (CurrentLED > -1) {
LastPressed = millis();
} else { // else see if auto loop should run
SinceLastPressed = millis() - LastPressed; // milliseconds since last time button was pressed
if (SinceLastPressed > NotPressed5) { // restart auto loop after NorPressed5 milliseconds
LastPressed = millis() - NotPressed1;
}
SinceLastPressed = millis() - LastPressed; // milliseconds since last time button was pressed
if (SinceLastPressed > NotPressed4) {
CurrentLED = 3;
} else if (SinceLastPressed > NotPressed3) {
CurrentLED = 2;
} else if (SinceLastPressed > NotPressed2) {
CurrentLED = 1;
} else if (SinceLastPressed > NotPressed1) {
CurrentLED = 0;
}
if (CurrentLED > -1) {
leds[CurrentLED] = myColors[CurrentLED];
}
}
FastLED.show(); // Show LEDs
}