#include <FastLED.h>  // Include the FasteLED library
 
// ––––––––– Change this according to your pins, number of LEDs and colors
 
#define LED_PIN 2   // Create constant variable that stores the digital pin the LEDs are connected to
#define NUM_LEDS 80  // Create constant variable that stores the number of LEDs
 
CRGB randColors[NUM_LEDS];
 
// ––––––––– No need to change anything here
 
CRGB leds[NUM_LEDS];  // Initialize the LEDs
 
unsigned long changeTimer = 0;  // Create timer variable for colors change
unsigned long fadeTimer = 0;    // Create timer variable for color fade
int colorIndex = 0;             // Create an index of the color currently faded to
float targetPrev[NUM_LEDS][3];  // Array in which to store the previous color for the smoothing calculation
 
// ––––––––– Setup section
 
void setup() {
  randomSeed(analogRead(A0));
  Serial.begin(115200);
 
  FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);  // Configure the LEDs
  FastLED.setBrightness(255);                          // Set all the LEDs' brightness in the range 0 - 255
  fill_solid(leds, NUM_LEDS, CRGB::Black);  // Change the LEDs' color to black (off) in order to clear them after restarting
  FastLED.show();                           // Show/apply the previously changed to color
}
 
// ––––––––– Loop section. This is where the main code runs continuously
 
void loop() {
  /*
  Call the function changeIndex() with the following argument:
 
  time in ms after which to advance to the next color in the colors[] array
  */
  changeIndex(4000);
 
  /*
  Call the function fadeLed() with the following arguments:
 
  The color to be faded to,
  The speed of the fade: 1 ~ 4000 with 1 being very slow (to the degree that you will be able to see the jumps on brightness in increments) and 4000 being instant to the human eye. This range is kind of arbitrary and non-linear
  */
  fadeLed(500);
}
 
void changeIndex(int delayTime) {
  if (millis() - changeTimer > delayTime) {  // The timer variable is used to keep track of how many milliseconds passed. In this example, the condition in the if function is true, when the milliseconds in the timer variable are less than the current count of milliseconds since starting the Arduino (millis()) by at least 4000ms
    changeTimer = millis();                  // Now that 4000ms have passed, the timer is set to the current time, to allow for the next comparison between it (the current time) and a future live time (millis())
 
    for (int i = 0; i < NUM_LEDS; i++) {
      randColors[i] = CRGB(random(256), random(256), random(256));
    }
 
    // Serial.print(randColors[0].r);
    // Serial.print(",");
    // Serial.print(randColors[0].g);
    // Serial.print(",");
    // Serial.print(randColors[0].b);
    // Serial.println();
    // Serial.print(randColors[1].r);
    // Serial.print(",");
    // Serial.print(randColors[1].g);
    // Serial.print(",");
    // Serial.print(randColors[1].b);
    // Serial.println();
  }
}
 
void fadeLed(float fadeSpeed) {
  fadeSpeed = constrain(fadeSpeed, 1, 4000);  // Values below 1 are set to 1 and values above 4000 are set to 4000.
  fadeSpeed /= 100000;                        // preparing the value for the following calculations
 
  if (millis() - fadeTimer > 3) {  // Part of creating a delay without actually using delay() since it would prevent other functions from working. In this case, the code in this if function is only executed every 3ms the most
    fadeTimer = millis();           // Part of creating a delay without actually using delay() since it would prevent other functions from working
 
    for (int ledIndex = 0; ledIndex < NUM_LEDS; ledIndex++) {                                                            // For every LED...
      float target[] = { float(randColors[ledIndex].r), float(randColors[ledIndex].g), float(randColors[ledIndex].b) };  // Extract the individual RGB values from the targetColor and put them in the array target[]
 
      for (int a = 0; a < 3; a++) {                                                         // For every color channel (r, b, g)...
        target[a] = (target[a] * fadeSpeed) + (targetPrev[ledIndex][a] * (1 - fadeSpeed));  // ...perform the smoothing calculation...
        targetPrev[ledIndex][a] = target[a];                                                // ...and save the result for use in the next calculation
 
        // Serial.print(-10);
        // Serial.print(",");
        // Serial.print(265);
        // Serial.print(",");
 
        // Serial.print(targetPrev[1][0]);
        // Serial.print(",");
        // Serial.print(targetPrev[1][1]);
        // Serial.print(",");
        // Serial.print(targetPrev[1][2]);
        // Serial.println();
      }
      leds[ledIndex] = CRGB(int(target[0]), int(target[1]), int(target[2]));  // Write the smoothed target colors to the LED
    }
    FastLED.show();  // Show the changed colors
  }
}
FPS: 0
Power: 0.00W