#define LED_PIN 2
#define NUM_LEDS 3
// Include the FastLED library
#include <FastLED.h>
CRGB leds[NUM_LEDS];
void setup() {
// Initialize the LED strip
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
FastLED.clear();
}
void loop() {
// Chase red
chaseColor(CRGB::Red);
delay(2000);
// Chase green
chaseColor(CRGB::Green);
delay(2000);
// Chase blue
chaseColor(CRGB::Blue);
delay(2000);
// Blink each LED with yellow color twice
blinkEachColorTwice(CRGB::Yellow);
// Blink each LED with cyan color twice
blinkEachColorTwice(CRGB::Cyan);
// Blink each LED with magenta color twice
blinkEachColorTwice(CRGB::Magenta);
// Blink all LEDs simultaneously 7 times with rainbow colors
blinkRainbowSimultaneously(7);
}
void chaseColor(CRGB color) {
// Move forward
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
FastLED.show();
delay(500);
leds[i] = CRGB::Black; // Turn off the LED
}
// Move backward
for(int i = NUM_LEDS - 1; i >= 0; i--) {
leds[i] = color;
FastLED.show();
delay(500);
leds[i] = CRGB::Black; // Turn off the LED
}
}
void blinkEachColorTwice(CRGB color) {
for (int i = 0; i < NUM_LEDS; i++) {
// Blink twice
for (int j = 0; j < 2; j++) {
leds[i] = color;
FastLED.show();
delay(250);
leds[i] = CRGB::Black;
FastLED.show();
delay(250);
}
}
}
void blinkRainbowSimultaneously(int times) {
for (int i = 0; i < times; i++) {
// Display rainbow colors
for (int j = 0; j < NUM_LEDS; j++) {
leds[j] = CHSV(j * (255 / NUM_LEDS), 255, 255);
}
FastLED.show();
delay(250);
// Turn off all LEDs
FastLED.clear();
FastLED.show();
delay(250);
}
}