Devices & Components
Arduino Nano
WS2812 strip leds
32x8 Ws2812 LED matrix
Slider potentiometer
Software & Tools
Arduino IDE
Project description
Code
Setting all leds in a strip to red using FastLED library
cpp
Shows how the FastLED library works
1#include <FastLED.h> 2#define NUM_LEDS 7 3#define DATA_PIN 13 4 5CRGB leds[NUM_LEDS]; 6 7void setup() { 8 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); 9} 10 11void loop() { 12 for (int i = 0; i < NUM_LEDS; i++) { 13 leds[i] = CRGB(255, 0, 0); // Red 14 } 15 FastLED.show(); 16}
Setting all leds in a strip to Green using Neopixel library
cpp
Shows how neopixel library works
1#include <Adafruit_NeoPixel.h> 2#define NUM_LEDS 7 3#define DATA_PIN 13 4 5Adafruit_NeoPixel strip(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800); 6 7void setup() { 8 strip.begin(); 9 strip.show(); // Initialize all pixels to 'off' 10} 11 12void loop() { 13 for (int i = 0; i < NUM_LEDS; i++) { 14 strip.setPixelColor(i, strip.Color(0, 255, 0)); // Green 15 } 16 strip.show(); 17}
Creating a Flowing Color Animation Using HSV Color spectrum
cpp
LED strip flows continuously with changing color
1#include <FastLED.h> 2 3#define LED_PIN 13 4#define NUM_LEDS 65 5#define BRIGHTNESS 200 6#define LED_TYPE WS2812B 7#define COLOR_ORDER GRB 8 9CRGB leds[NUM_LEDS]; 10 11uint8_t startColor = 0; 12const uint8_t colorStep = 3; 13unsigned long lastUpdate = 0; 14const unsigned long timeStep = 50; 15 16void setup() { 17 FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); 18 FastLED.setBrightness(BRIGHTNESS); 19 fill_solid(leds, NUM_LEDS, CRGB::Black); 20} 21 22void loop() { 23 unsigned long currentTime = millis(); 24 if (currentTime - lastUpdate >= timeStep) { 25 lastUpdate = currentTime; 26 27 // Shift all LEDs one step to the right 28 for (int i = NUM_LEDS - 1; i > 0; i--) { 29 leds[i] = leds[i - 1]; 30 } 31 32 // Insert new hue at the front 33 leds[0] = CHSV(startColor, 255, 255); 34 35 // Advance to the next hue 36 startColor += colorStep; 37 38 FastLED.show(); 39 } 40}
Releasing Commets:)
cpp
ColorLED travels through strip,leaves fading trail
1#include <FastLED.h> 2 3#define NUM_LEDS 65 4#define DATA_PIN 13 5#define COLOR_COUNT 5 6#define FADE_STEP 0.5 // 50% of previous brightness 7 8CRGB leds[NUM_LEDS]; 9 10CRGB colors[COLOR_COUNT] = { 11 CRGB::Red, 12 CRGB::Green, 13 CRGB::Blue, 14 CRGB::Magenta, 15 CRGB::Cyan 16}; 17 18unsigned long lastStepTime = 0; 19unsigned long lastCometStart = 0; 20const int stepDelay = 60; // ms between comet moves 21const int cometInterval = 2000; // ms between new comets 22int cometCount = 0; 23 24struct Comet { 25 int position; 26 int colorIndex; 27 bool active; 28}; 29 30#define MAX_COMETS 10 31Comet comets[MAX_COMETS]; 32 33void setup() { 34 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); 35 FastLED.clear(); 36} 37 38void loop() { 39 unsigned long currentTime = millis(); 40 41 // Launch new comet 42 if (currentTime - lastCometStart >= cometInterval) { 43 // Start a new comet 44 for (int i = 0; i < MAX_COMETS; i++) { 45 if (!comets[i].active) { 46 comets[i] = {0, cometCount % COLOR_COUNT, true}; 47 cometCount++; 48 lastCometStart = currentTime; 49 break; 50 } 51 } 52 } 53 54 // Step forward 55 if (currentTime - lastStepTime >= stepDelay) { 56 lastStepTime = currentTime; 57 58 // Fade all LEDs slightly 59 for (int i = 0; i < NUM_LEDS; i++) { 60 leds[i].nscale8(255 * FADE_STEP); // dim by 10% 61 } 62 63 // Update comets 64 for (int i = 0; i < MAX_COMETS; i++) { 65 if (comets[i].active) { 66 if (comets[i].position < NUM_LEDS) { 67 leds[comets[i].position] = colors[comets[i].colorIndex]; 68 comets[i].position++; 69 } else { 70 comets[i].active = false; 71 } 72 } 73 } 74 75 FastLED.show(); 76 } 77}
Comments
Only logged in users can leave comments