#include <FastLED.h>
#include <TimeLib.h>
// Number of LEDs in the strip
#define NUM_LEDS 22
#define DATA_PIN 3
#define BUTTON_PIN 2 // Pin number for the button
int lighting_chance = 100; // Higher chance for the initial selection (100%)
int loopdelay = 100; // Delay between loop iterations
unsigned long onDuration = 600000; // 10 minutes in milliseconds
unsigned long offDuration = 600000; // 10 minutes in milliseconds
// Define the array of LEDs
CRGB leds[NUM_LEDS];
// Track the state and timers for each LED
bool ledOn[NUM_LEDS] = {false}; // Whether the LED is currently on
unsigned long ledOnTime[NUM_LEDS] = {0}; // When the LED was turned on
unsigned long ledOffTime[NUM_LEDS] = {0}; // When the LED was turned off
// Day and night brightness levels
const uint8_t dayBrightness = 255; // Full brightness during the day
const uint8_t nightBrightness = 50; // Dimmed brightness during the night
// Button state tracking
int buttonState = HIGH; // Current state of the button
int lastButtonState = HIGH; // Previous state of the button
unsigned long lastDebounceTime = 0; // Debounce time to prevent multiple triggers
unsigned long debounceDelay = 50; // Debounce delay in milliseconds
// Operating mode: 0 = normal operation, 1 = all lights on, 2 = all lights off
int mode = 0;
// Variables to track button press and release
bool buttonPressed = false;
void setup() {
Serial.begin(9600);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set the button pin as an input with internal pull-up
// Initialize time (set this according to your needs or use a real-time clock)
setTime(18, 0, 0, 1, 1, 2024); // Example: setting the time to 6:00 PM
// Clear LEDs initially
FastLED.clear();
FastLED.show();
Serial.println("Setup complete, starting in mode 0");
}
void loop() {
// Handle button press with debounce
int reading = digitalRead(BUTTON_PIN);
// If the button state changes, reset the debounce timer
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// If the debounce delay has passed and the button state has changed, process it
if ((millis() - lastDebounceTime) > debounceDelay) {
// Check if the button is pressed (LOW)
if (reading == LOW && buttonState == HIGH) {
mode = (mode + 1) % 3; // Cycle between 0, 1, and 2
Serial.print("Mode changed to: ");
Serial.println(mode);
}
buttonState = reading; // Update the button state
}
lastButtonState = reading; // Save the current button state for next comparison
// Adjust brightness based on the time of day
if (hour() >= 18 || hour() < 6) {
FastLED.setBrightness(nightBrightness); // Dim the LEDs at night
} else {
FastLED.setBrightness(dayBrightness); // Full brightness during the day
}
// Operating modes:
if (mode == 0) {
// Mode 0: Normal operation
normalOperation();
} else if (mode == 1) {
// Mode 1: All lights on
allLightsOn();
} else if (mode == 2) {
// Mode 2: All lights off
allLightsOff();
}
FastLED.show();
delay(loopdelay); // Delay between loops
}
// Function for normal operation
void normalOperation() {
// Set LEDs 1, 5, 8, and 12 (indices 0, 4, 7, 11) to solid red (always on)
leds[0] = CRGB::Red; // LED 1
leds[4] = CRGB::Red; // LED 5
leds[7] = CRGB::Red; // LED 8
leds[11] = CRGB::Red; // LED 12
// Set LED 14 (index 13) to green between 6 PM and 6 AM
if (hour() >= 18 || hour() < 6) {
leds[13] = CRGB::Green; // LED 14
} else {
leds[13] = CRGB::Black; // Turn off outside the time range
}
// Count the number of LEDs currently on (excluding fixed LEDs)
int activeCount = 0;
for (int i = 0; i < NUM_LEDS; i++) {
if (i != 0 && i != 4 && i != 7 && i != 11 && i != 13) { // Skip fixed LEDs
if (ledOn[i]) {
// If the LED is on, check if it's been on for 10 minutes
if (millis() - ledOnTime[i] >= onDuration) {
leds[i] = CRGB::Black; // Turn off after 10 minutes
ledOn[i] = false; // Update the state
ledOffTime[i] = millis(); // Record when it was turned off
}
activeCount++;
} else {
// If the LED is off, check if it has been off for 10 minutes
if (millis() - ledOffTime[i] >= offDuration) {
if (activeCount < 3 && random(0, 1000) < lighting_chance) { // Turn on with a chance
leds[i] = CRGB(255, 255, 0); // Turn on as yellow
ledOn[i] = true; // Update the state
ledOnTime[i] = millis(); // Record the time it was turned on
activeCount++;
}
}
}
}
}
}
// Function to turn all lights on (based on time of day brightness)
void allLightsOn() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White; // Turn all LEDs white (full or dimmed)
}
}
// Function to turn all lights off
void allLightsOff() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black; // Turn off all LEDs
}
}