// Define pins for each bi-color LED
const int led1Pin1 = 2; // Pin for one direction
const int led1Pin2 = 15; // Pin for the opposite direction
const int led2Pin1 = 4; // Pin for one direction
const int led2Pin2 = 5; // Pin for the opposite direction
// Variables to control fast/slow blink modes
bool fastBlinkMode = false; // Flag for fast blinking
unsigned long lastBlinkTime = 0; // To keep track of timing
unsigned long blinkDuration = 0; // Duration for the current blink phase
void setup() {
// Initialize LED pins as outputs
pinMode(led1Pin1, OUTPUT);
pinMode(led1Pin2, OUTPUT);
pinMode(led2Pin1, OUTPUT);
pinMode(led2Pin2, OUTPUT);
// Seed the random function with an unpredictable value
randomSeed(analogRead(A0)); // Assuming A0 is unused and can pick up noise
// Start with a random blink duration
blinkDuration = random(5000, 10000); // Random initial phase duration (5-10 seconds)
}
void loop() {
unsigned long currentTime = millis();
// Switch between fast and slow blink modes periodically
if (currentTime - lastBlinkTime > blinkDuration) {
fastBlinkMode = !fastBlinkMode; // Toggle between fast and slow modes
lastBlinkTime = currentTime;
if (fastBlinkMode) {
// Fast mode: shorter phase
blinkDuration = random(2000, 5000); // 2-5 seconds for fast blinking
} else {
// Slow mode: longer phase
blinkDuration = random(5000, 10000); // 5-10 seconds for slow blinking
}
}
// Randomize blinking based on the mode
if (fastBlinkMode) {
// Quick, short blinks for fast mode
randomBlink(led1Pin1, led1Pin2, 20, 150); // Blink between 20ms to 150ms
randomBlink(led2Pin1, led2Pin2, 20, 150);
} else {
// Slow, relaxed blinks for slow mode
randomBlink(led1Pin1, led1Pin2, 200, 800); // Blink between 200ms to 800ms
randomBlink(led2Pin1, led2Pin2, 200, 800);
}
// Short delay between blinks to make them more independent
delay(random(50, 200));
}
// Function to randomize color and blink pattern based on mode
void randomBlink(int pin1, int pin2, int minDuration, int maxDuration) {
int state = random(0, 4); // 4 possible states: Off, Red, Green, Yellow (alternating quickly)
switch (state) {
case 0:
// Off
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
break;
case 1:
// Red (pin1 HIGH, pin2 LOW)
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
break;
case 2:
// Green (pin1 LOW, pin2 HIGH)
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
break;
case 3:
// Yellow (quickly alternate between red and green)
for (int i = 0; i < random(5, 15); i++) { // Alternate 5-15 times for yellow effect
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
delay(10); // Very short on time for red
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
delay(10); // Very short on time for green
}
break;
}
// Random blink duration based on mode
delay(random(minDuration, maxDuration));
}