#include <Arduino.h>
#define LED_PIN_1 5 // Replace with the GPIO pin connected to the first LED
#define BUTTON_PIN_1 4 // Replace with the GPIO pin connected to the first button
#define LED_PIN_2 18 // Replace with the GPIO pin connected to the second LED
#define BUTTON_PIN_2 19 // Replace with the GPIO pin connected to the second button
unsigned long flashDuration = 6000; // 6 seconds duration for flashing
unsigned long pulseDuration = 80; // 0.05 seconds duration for each pulse
unsigned long interPulseDelay = 80; // 0.05 seconds inter-pulse delay
unsigned long pauseDuration = 500; // 0.5 second pause between pulse patterns
enum LedState
{
OFF,
FLASHING,
};
LedState ledState1 = OFF;
LedState ledState2 = OFF;
void setup()
{
pinMode(LED_PIN_1, OUTPUT);
pinMode(BUTTON_PIN_1, INPUT_PULLUP); // Assuming the button is normally open and pulled up externally
pinMode(LED_PIN_2, OUTPUT);
pinMode(BUTTON_PIN_2, INPUT_PULLUP); // Assuming the button is normally open and pulled up externally
}
void loop()
{
// Check if button 1 is pressed
if (digitalRead(BUTTON_PIN_1) == LOW)
{
ledState1 = FLASHING;
flashLED(LED_PIN_1);
}
// Check if button 2 is pressed
if (digitalRead(BUTTON_PIN_2) == LOW)
{
ledState2 = FLASHING;
flashLED(LED_PIN_2);
}
}
void flashLED(int ledPin)
{
unsigned long flashStartTime = millis();
// Keep flashing until the elapsed time exceeds the flash duration
while (millis() - flashStartTime < flashDuration)
{
// Check if the other button is pressed
if (digitalRead(LED_PIN_1 == ledPin ? BUTTON_PIN_2 : BUTTON_PIN_1) == LOW)
{
// If the other button is pressed, exit the function
ledState1 = OFF;
ledState2 = OFF;
return;
}
// Pulse pattern: 8 pulses in 1 second
for (int pulse = 0; pulse < 8; ++pulse)
{
digitalWrite(ledPin, HIGH);
delay(pulseDuration);
digitalWrite(ledPin, LOW);
delay(interPulseDelay);
}
// Pause between pulse patterns
delay(pauseDuration);
}
// Turn off the LED after flashing duration
digitalWrite(ledPin, LOW);
ledState1 = OFF;
ledState2 = OFF;
}