#include <FastLED.h>
// Define the data pin and the number of LEDs in your strip
#define LED_PIN 13 // Use D4 which corresponds to GPIO2 on NodeMCU
#define NUM_LEDS 10 // **IMPORTANT:** Change this to the number of LEDs you have
// Define the LED strip array
CRGB leds[NUM_LEDS];
// Global variables for LED strip animation timing
unsigned long previousMillis = 0;
int i = 0; // Index for the chase effects
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
delay(20);
// Initialize the LED strip
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
}
void allLedsOff() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
digitalWrite(LED_PIN, LOW);
}
void allLedsRed() {
fill_solid(leds, NUM_LEDS, CRGB::Red);
FastLED.show();
}
void rainbow() {
static uint8_t hue = 0;
fill_rainbow(leds, NUM_LEDS, hue, 7);
FastLED.show();
hue++;
// A small delay to slow down the animation without blocking ESP-NOW.
delay(2);
}
void CornertoCenter() {
const unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 100) {
leds[i] = CRGB::GreenYellow;
int a = NUM_LEDS - 1 - i;
leds[a] = CRGB::GreenYellow;
FastLED.show();
i++;
if (i > NUM_LEDS / 2) {
i = 0;
allLedsOff();
}
previousMillis = currentMillis;
}
}
void CentertoCorner() {
const unsigned long currentMillis = millis();
if (i == 0)fill_solid(leds, NUM_LEDS, CRGB::GreenYellow);
FastLED.show();
if (currentMillis - previousMillis >= 100) {
leds[i] = CRGB::Black;
int a = NUM_LEDS - 1 - i;
leds[a] = CRGB::Black;
FastLED.show();
i++;
if (i > NUM_LEDS / 2) {
i = 0;
}
previousMillis = currentMillis;
}
}
void police1() {
const unsigned long currentMillis = millis();
if (currentMillis - previousMillis < 1000) {
((millis() / 50) % 2) ? fill_solid(leds, NUM_LEDS, CRGB::Red) : fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
}
if (currentMillis - previousMillis >= 1000 && currentMillis - previousMillis < 2000) {
((millis() / 50) % 2) ? fill_solid(leds, NUM_LEDS, CRGB::Blue) : fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
}
if (currentMillis - previousMillis >= 2000)previousMillis = currentMillis;
}
void loop() {
police1();
}