(deleted)
You can’t do it like that. You need to use a state machine style of program, like the blink without delay.
You can not use for loops nor delay as that stops things wiring at the same time.
Can you draw how they need to fade? And the library in my signature can probably make it easy for you
(deleted)
With my library it's very easy to do. And you get gamma correction as a bonus
Think this should do it:
#include <FadeLed.h>
#include <Bounce2.h>
FadeLed ledRows[] = {9, 10, 11}; //FadeLed object for these pins
const byte NrLedRows = sizeof(ledRows) / sizeof(FadeLed); //Number of rows
const unsigned long FadeTime = 2000; //Time (in ms) a fade takes
const byte FadeThreshold = 20; //When to start the fade of the next row? in %
const byte ButtonPin = 5;
Bounce button;
void setup(){
for(byte i = 0; i < NrLedRows; i++){
ledRows[i].setTime(FadeTime);
ledRows[i].beginOn();
}
button.attach(ButtonPin, INPUT_PULLUP);
}
void loop(){
FadeLed::update();
button.update();
//Button was pressed, first row is on and not fading
if(button.fell() && ledRows[0].get() && ledRows[0].done()){
//start fading the first row to off
ledRows[0].off();
}
for(byte i = 1; i < NrLedRows; i++){
//if the previous row gets under the threshold, and next row isn't already fading or off,
//start fading the next
if((ledRows[i - 1].get() <= FadeThreshold) && ledRows[i].done() && !ledRows[i].get()){
ledRows[i].off();
}
}
//If all leds are off, a button press will trigger a turn on (no fade) of all leds
if(button.fell() && allOff()){
for(byte i = 0; i < NrLedRows; i++){
ledRows[i].beginOn();
}
}
}
bool allOff(){
bool rtn = false;
for(byte i = 0; i < NrLedRows; i++){
rtn |= ledRows[i].get();
}
return !rtn;
}
Does compile but untested because I don't have the hardware on hand.