/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <FastLED.h>
const byte stripPin = 2;
const byte numLeds = 100;
const byte numChosenLeds = 30;
const CRGB amber = CRGB(255, 191, 0);
CRGB leds[numLeds];
byte indexArray[numLeds];
enum {SHUFFLE, LIGHTUP, WAIT, FADE} state = SHUFFLE;
const unsigned long waitTime = 2000ul; // make it 20000ul for 20s, limiting at 2s for demo
const unsigned long fadeStepTime = 10ul;
unsigned long startTime;
void fisherYatesShuffle() {
for (size_t i = numLeds - 1; i > 0; --i) {
size_t r = random(0, i + 1); // generate rand numbers from 0 to i
size_t tmpSwap = indexArray[i];
indexArray[i] = indexArray[r];
indexArray[r] = tmpSwap;
}
}
void animateStrip() {
switch (state) {
case SHUFFLE:
fisherYatesShuffle();
state = LIGHTUP;
break;
case LIGHTUP:
fill_solid(leds, numLeds, CRGB::Black); // turn them all off
for (byte i = 0; i < numChosenLeds; i++)leds[indexArray[i]] = amber; // turn the selected ones amber
FastLED.show();
startTime = millis();
state = WAIT;
break;
case WAIT:
if (millis() - startTime >= waitTime) {
startTime = millis();
state = FADE;
}
break;
case FADE:
if (millis() - startTime >= fadeStepTime) {
fadeToBlackBy(leds, numLeds, 1);
FastLED.show();
if (leds[indexArray[0]] == CRGB::Black) { // we totally faded out
state = SHUFFLE;
} else {
startTime = millis();
}
}
break;
}
}
void setup() {
randomSeed(analogRead(A0));
Serial.begin(115200);
for (byte i = 0; i < numLeds; i++) indexArray[i] = i;
FastLED.addLeds<WS2812B, stripPin, GRB>(leds, numLeds);
}
void loop() {
animateStrip();
// you can do other stuff here as long as it does not take too long
// ....
}