Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Eyes and Random Blinking LEDs test
- 9/12/2025
- */
- #include "FastLED.h"
- // How many leds in your strip?
- #define NUM_LEDS 36
- #define DATA_PIN 14
- #define LED_TYPE NEOPIXEL
- #define BRIGHTNESS 50 // 0 - 255, 8-bit
- CRGBArray<NUM_LEDS> leds;
- CRGBSet eyeLEDS(leds(6, 7)); // Eyes are last two in strand
- CRGBSet randomLeds(leds(0, 5));
- //CRGBSet allLEDs(leds(0, NUM_LEDS - 1));
- // For random blinking effect
- #define NUM_RANDOM_LEDS 6
- // Define base timing values for random blinking
- #define CHECK_INTERVAL 50 // How often we check for updates
- #define MIN_BLINK_TIME 300 // Minimum time in milliseconds
- #define MAX_BLINK_TIME 2000 // Maximum time in milliseconds
- // Arrays to track state for each random blinking LED
- bool blinkState[NUM_RANDOM_LEDS];
- CRGB colors[NUM_RANDOM_LEDS];
- uint16_t nextChangeTime[NUM_RANDOM_LEDS]; // How many milliseconds until this LED changes state
- // FadeUpDown stuff
- uint8_t hue = 0;
- uint8_t sat = 255; // red
- uint8_t val = 0;
- boolean fadeDirection = 1; // [1=fade up, 0=fade down]
- void setup() {
- Serial.begin(115200);
- FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS); // 0 - 255, 8-bit
- // Initialize random seed
- random16_add_entropy(analogRead(35));
- // Initialize random blink timings, states, and colors
- for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
- // Randomize initial states with variation to avoid clusters
- blinkState[i] = (random8(10) > 5 + i % 3) ? true : false;
- // Assign an initial delay before the first change
- nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
- // Generate a random color for each LED
- colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
- }
- }
- void loop() {
- // Fade eyes up/down
- fadeUpDown();
- // Randomly blink the first set of LEDs
- EVERY_N_MILLISECONDS(CHECK_INTERVAL) {
- for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
- // Decrease the time until next change
- if (nextChangeTime[i] > CHECK_INTERVAL) {
- nextChangeTime[i] -= CHECK_INTERVAL;
- } else {
- // Time to change this LED's state
- blinkState[i] = !blinkState[i];
- if (blinkState[i]) {
- // Generate a new random color when turning on
- colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
- // Assign new time for how long to stay ON
- nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
- } else {
- // Assign new time for how long to stay OFF (slightly different range)
- nextChangeTime[i] = random16(MIN_BLINK_TIME + 100, MAX_BLINK_TIME - 200);
- }
- }
- // Set LED to either its color or black based on current state
- randomLeds[i] = blinkState[i] ? colors[i] : CRGB::Black;
- }
- // Update the LED strip
- EVERY_N_MILLISECONDS(20) {
- FastLED.show();
- }
- }
- }
- void fadeUpDown() {
- if (fadeDirection == 1) { //fade up
- EVERY_N_MILLISECONDS(3) {
- fill_solid(eyeLEDS, 2, CHSV(hue, sat, val));
- val = val + 1;
- if (val == 255) {
- fadeDirection = !fadeDirection; //reverse direction
- Serial.print("---fade up done---");
- Serial.print(" fadeDirection: ");
- Serial.println(fadeDirection);
- }
- }
- }
- if (fadeDirection == 0) { //fade down
- EVERY_N_MILLISECONDS(9) {
- fill_solid(eyeLEDS, 2, CHSV(hue, sat, val));
- val = val - 1;
- if (val == 0) {
- fadeDirection = !fadeDirection; //reverse direction
- Serial.print("--fade down done--");
- Serial.print(" fadeDirection: ");
- Serial.println(fadeDirection);
- }
- }
- }
- FastLED.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment