#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 10
#define BUTTON1_PIN 2
#define BUTTON2_PIN 3
#define BUTTON3_PIN 4
CRGB leds[NUM_LEDS];
int button1State = 0;
int button2State = 0;
int button3State = 0;
bool discoActive = false; // Flag to indicate disco effect is active
bool button2Active = false; // Flag to indicate button 2 is active
unsigned long fadeStartTime = 0; // To track fade timing
bool fadingToRed = true; // To track direction of fade
void setup() {
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
}
void loop() {
// Read button states
button1State = digitalRead(BUTTON1_PIN);
button2State = digitalRead(BUTTON2_PIN);
button3State = digitalRead(BUTTON3_PIN);
// Button 1 functionality: Reset and override other effects
if (button1State == LOW) {
delay(50); // Debounce delay
if (digitalRead(BUTTON1_PIN) == LOW) {
button2Active = false; // Stop fade effect
discoActive = false; // Stop disco effect
fillLEDs(CRGB::White); // Reset to white
FastLED.show();
while (digitalRead(BUTTON1_PIN) == LOW); // Wait for button release
}
}
// Button 2 functionality: Fade from white to blood red and back
if (button2State == LOW && !button2Active && !discoActive) {
button2Active = true;
fadeStartTime = millis(); // Start timing for fade
fadingToRed = true; // Start fading to red
while (digitalRead(BUTTON2_PIN) == LOW); // Wait for button release
}
if (button2Active) {
fadeEffect(); // Run the fade effect
}
// Button 3 functionality: Flashing disco effect
if (button3State == LOW && !discoActive && !button2Active) {
discoActive = true; // Set disco effect active
while (digitalRead(BUTTON3_PIN) == LOW); // Wait for button release
}
// Run disco effect if active
if (discoActive) {
discoLights();
}
FastLED.show();
}
void fadeEffect() {
unsigned long elapsedTime = millis() - fadeStartTime;
int fadeDuration = 30000; // 30 seconds for each fade (adjust as needed)
// Calculate the progress of the fade
float progress = (float)(elapsedTime % fadeDuration) / fadeDuration;
if (fadingToRed) {
// Fade from white to blood red
int redValue = 255; // Blood red stays constant
int greenValue = 255 - (255 * progress); // Decrease green
int blueValue = 255 - (255 * progress); // Decrease blue
fillLEDs(CRGB(redValue, greenValue, blueValue)); // Set color
} else {
// Fade from blood red back to white
int redValue = 255; // Blood red stays constant
int greenValue = (255 * progress); // Increase green
int blueValue = (255 * progress); // Increase blue
fillLEDs(CRGB(redValue, greenValue, blueValue)); // Set color
}
// Check if the fade duration has completed
if (elapsedTime >= fadeDuration) {
fadingToRed = !fadingToRed; // Toggle direction
fadeStartTime = millis(); // Reset timer for the next fade
}
}
void fillLEDs(CRGB color) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
}
}
void discoLights() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(random(255), random(255), random(255));
}
FastLED.show();
delay(100); // Adjust speed of disco effect
}