#include <FastLED.h>
#define DATA_PIN 2 // Pin connected to the NeoPixels
#define NUM_LEDS 6 // Number of NeoPixels
#define TRAIL_LENGTH 2 // Length of the fading trail
#define FADE_DELAY 50 // Delay between brightness steps in milliseconds
#define MAX_BRIGHTNESS 255 // Maximum brightness value
CRGB leds[NUM_LEDS]; // Define the array of LED colors
//********************** RED LED *******
const int ledPin = 3;
// Variables to track time
unsigned long previousMillis = 0;
const long interval = 500; // Interval in milliseconds (half second)
//********************** BLUE LED
const int ledPinBlue = 4;
// Variables to track time
unsigned long previousMillisBlue = 0;
const long intervalBlue = 250;
//********************* LED state
int ledState = LOW;
int ledStateBlue = LOW;
//*************** RGB LED
// Define the pins for RGB LED
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 7;
// Define the time interval for strobing
const unsigned long strobeInterval = 250; // 100 milliseconds
// Variables to store the current time
unsigned long previousMillisStrobe = 0;
// Variable to store the state of strobe
boolean strobeState = false;
// Variables for HSV color space
float hue = 0.0; // Hue value (0 to 360)
const float saturation = 1.0; // Saturation value (0 to 1)
const float value = 1.0; // Value/Brightness value (0 to 1)
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // Initialize the NeoPixel strip
pinMode(ledPin, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Incrementally change the hue of the color
static uint8_t hue = 0;
hue++;
// Main chaser sequence loop
for (int i = 0; i < NUM_LEDS; i++) {
// Fade the previous and next pixel
for (int j = 1; j <= TRAIL_LENGTH; j++) {
int prevIndex = (i - j + NUM_LEDS) % NUM_LEDS;
int nextIndex = (i + j) % NUM_LEDS;
// Fade the previous pixel
leds[prevIndex].fadeToBlackBy(128);
// Fade the next pixel
leds[nextIndex].fadeToBlackBy(128);
}
// Set the color of the current pixel
leds[i] = CHSV(hue, 255, MAX_BRIGHTNESS);
FastLED.show(); // Update the NeoPixel strip
delay(FADE_DELAY); // Delay for the fade effect
}
//************************ BUTTONS
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to blink the LED
if (currentMillis - previousMillis >= interval) {
// Save the last time the LED blinked
previousMillis = currentMillis;
// Toggle the LED state
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// Apply the new LED state
digitalWrite(ledPin, ledState);
}
unsigned long currentMillisBlue = millis(); // Get the current time
// Check if it's time to blink the LED
if (currentMillisBlue - previousMillisBlue >= intervalBlue) {
// Save the last time the LED blinked
previousMillisBlue = currentMillisBlue;
// Toggle the LED state
if (ledStateBlue == LOW) {
ledStateBlue = HIGH;
} else {
ledStateBlue = LOW;
}
// Apply the new LED state
digitalWrite(ledPinBlue, ledStateBlue);
}
//***********************RGB LEDS
// Get the current time
unsigned long currentMillisStrobe = millis();
// Check if it's time to toggle the strobe
if (currentMillisStrobe - previousMillisStrobe >= strobeInterval) {
// Save the current time
previousMillisStrobe = currentMillisStrobe;
// Toggle the strobe state
strobeState = !strobeState;
// If strobe is ON, set RGB to the next hue color
if (strobeState) {
// Convert HSV to RGB
int rgbValues[3];
hsvToRgb(hue, saturation, value, rgbValues);
// Set the RGB LED to the calculated color
analogWrite(redPin, rgbValues[0]);
analogWrite(greenPin, rgbValues[1]);
analogWrite(bluePin, rgbValues[2]);
// Increment the hue for the next flash
hue += 10.0; // Increment hue by 10 degrees
if (hue >= 360.0) {
hue = 0.0; // Reset hue to 0 if it exceeds 360
}
}
// If strobe is OFF, turn off RGB
else {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
}
}
// Function to convert HSV to RGB
void hsvToRgb(float h, float s, float v, int rgb[]) {
int i;
float f, p, q, t;
if (s == 0) {
// Achromatic (grey)
rgb[0] = rgb[1] = rgb[2] = round(v * 255);
return;
}
h /= 60; // sector 0 to 5
i = floor(h);
f = h - i; // factorial part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch (i) {
case 0:
rgb[0] = round(v * 255);
rgb[1] = round(t * 255);
rgb[2] = round(p * 255);
break;
case 1:
rgb[0] = round(q * 255);
rgb[1] = round(v * 255);
rgb[2] = round(p * 255);
break;
case 2:
rgb[0] = round(p * 255);
rgb[1] = round(v * 255);
rgb[2] = round(t * 255);
break;
case 3:
rgb[0] = round(p * 255);
rgb[1] = round(q * 255);
rgb[2] = round(v * 255);
break;
case 4:
rgb[0] = round(t * 255);
rgb[1] = round(p * 255);
rgb[2] = round(v * 255);
break;
default: // case 5:
rgb[0] = round(v * 255);
rgb[1] = round(p * 255);
rgb[2] = round(q * 255);
break;
}
}