#include <Arduino.h>
// Enum to define LED states
enum LedState {
LED_OFF,
LED_STROBE,
LED_RAMP,
LED_FLICKER
};
// Define LED pins
const int rgbLedPins[][3] = {{5, 6, 7}, {6, 7, 8}, {7, 8, 9}, {8, 9, 5}}; // RGB LEDs on pins 5-8
const int ledPin2 = 2; // rx2
const int ledPin3 = 3; // rx3
const int ledPin4 = 4; // b
// Define button pin
const int buttonPin = 13;
// Set delays (in milliseconds)
const int strobeDelay = 40;
const int interLEDelay = 75;
const int rampStepDelay = 20;
const int burstDuration = 300;
// Variables to track the button state and LED state
int buttonState = LOW;
int lastButtonState = LOW;
// Variable to track the current LED state
LedState currentLedState = LED_OFF;
unsigned long flickerStartTime[4] = {0};
int flickerDuration[4] = {0};
// Debouncing variables
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 100;
// Variable to track whether the button was just pressed
bool buttonPressed = false;
// Variable to track whether the animation has been triggered in the current loop cycle
bool animationTriggered = false;
// Define constant LED pin
const int constantLedPin = 12; // Change 12 to the actual pin number you want to use
// ... rest of the code ...
void setup() {
// Set RGB LED pins as outputs
for (int i = 0; i < sizeof(rgbLedPins) / sizeof(rgbLedPins[0]); ++i) {
for (int j = 0; j < 3; ++j) {
pinMode(rgbLedPins[i][j], OUTPUT);
}
}
// Set constant LED pin as output
pinMode(constantLedPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// Set button pin as input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Check if external power is present
if (!digitalRead(7)) {
// Turn on RGB LEDs with asynchronous flicker
for (int i = 0; i < sizeof(rgbLedPins) / sizeof(rgbLedPins[0]); ++i) {
analogWrite(rgbLedPins[i][0], random(256)); // Set random intensity for Red
analogWrite(rgbLedPins[i][1], random(256)); // Set random intensity for Green
analogWrite(rgbLedPins[i][2], random(256)); // Set random intensity for Blue
}
// Asynchronously flicker RGB LEDs
flickerRGBLEDs();
} else {
// Turn off all LEDs when external power is not present
for (int i = 0; i < sizeof(rgbLedPins) / sizeof(rgbLedPins[0]); ++i) {
for (int j = 0; j < 3; ++j) {
analogWrite(rgbLedPins[i][j], 0); // Turn off RGB LEDs
}
}
// Check if the button is pressed
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH && !buttonPressed) {
// Button is pressed, set the flag to trigger the LED cycle immediately
buttonPressed = true;
cycleLEDs();
}
}
}
lastButtonState = reading;
// If the button was just pressed and the animation hasn't been triggered in the current cycle
if (buttonPressed && currentLedState == LED_OFF) {
buttonPressed = false;
}
}
// Asynchronously flicker RGB LEDs continuously
flickerRGBLEDs();
}
void cycleLEDs() {
strobeLED(ledPin2);
delay(interLEDelay);
strobeLED(ledPin3);
delay(interLEDelay);
rampLED(ledPin4);
delay(interLEDelay);
}
void strobeLED(int pin) {
// Flash the LED with a 75-millisecond strobe
digitalWrite(pin, HIGH);
delay(strobeDelay);
digitalWrite(pin, LOW);
delay(strobeDelay);
}
void rampLED(int pin) {
// Ramp up LED brightness
for (int brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(pin, brightness);
delay(rampStepDelay);
}
// Burst at maximum brightness for 250 milliseconds
delay(burstDuration);
// Ramp down LED brightness
for (int brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(pin, brightness);
delay(rampStepDelay);
}
}
void flickerRGBLEDs() {
// Asynchronous flicker for RGB LEDs
for (int i = 0; i < sizeof(rgbLedPins) / sizeof(rgbLedPins[0]); ++i) {
// Check if it's time to start a new flicker for this RGB LED
if (millis() - flickerStartTime[i] >= flickerDuration[i]) {
flickerStartTime[i] = millis();
flickerDuration[i] = random(250, 700); // Random flicker duration
for (int j = 0; j < 3; ++j) {
analogWrite(rgbLedPins[i][j], random(256)); // Set random intensity for each color channel
}
}
}
}