const int leftButton = 8; // Button controlling the left LED
const int rightButton = 11; // Button controlling the right LED
const int leftTurnLED = A0; // LED for left turn signal
const int rightTurnLED = A1;// LED for right turn signal
unsigned long lastLeftBlink = 0; // Tracks last time left LED blinked
unsigned long lastRightBlink = 0; // Tracks last time right LED blinked
unsigned long lastBothBlink = 0; // Tracks last time both LEDs blinked together
unsigned long lastUpdate = 0; // Tracks time for state update
unsigned long lastBothCheck = 0; // Tracks time to check if both buttons are pressed
int blinkInterval = 300; // Blinking interval for LEDs
int leftButtonState = 0; // Tracks left button presses
int rightButtonState = 0; // Tracks right button presses
bool bothButtonsPressed = false; // Flag for both buttons pressed
int leftDoubleState = 0; // Double press state for left button
int rightDoubleState = 0; // Double press state for right button
int lastLeftButtonState = LOW; // Last state of the left button
int lastRightButtonState = LOW; // Last state of the right button
void setup() {
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(leftTurnLED, OUTPUT);
pinMode(rightTurnLED, OUTPUT);
}
void controlTurnSignals() {
int leftButtonValue = digitalRead(leftButton);
int rightButtonValue = digitalRead(rightButton);
// Handle left button press
if (leftButtonValue == HIGH && lastLeftButtonState == LOW) {
leftDoubleState++;
leftButtonState++;
rightButtonState = 0; // Reset right button state
}
// Handle right button press
if (rightButtonValue == HIGH && lastRightButtonState == LOW) {
rightButtonState++;
rightDoubleState++;
leftButtonState = 0; // Reset left button state
}
// Check if both buttons are pressed
if (millis() - lastBothCheck >= 200) {
if (leftDoubleState == 1 && rightDoubleState == 1) {
leftButtonState = 0;
rightButtonState = 0;
bothButtonsPressed = true;
// Blink both LEDs together
if (millis() - lastBothBlink >= blinkInterval) {
digitalWrite(rightTurnLED, !digitalRead(rightTurnLED));
digitalWrite(leftTurnLED, digitalRead(rightTurnLED));
lastBothBlink = millis();
}
} else {
leftDoubleState = 0;
rightDoubleState = 0;
bothButtonsPressed = false;
}
lastBothCheck = millis();
}
// Handle individual button control
if (millis() - lastUpdate >= 200 && !bothButtonsPressed) {
// Left button active
if (leftButtonState == 1 && millis() - lastLeftBlink >= blinkInterval) {
digitalWrite(leftTurnLED, !digitalRead(leftTurnLED));
digitalWrite(rightTurnLED, LOW);
lastLeftBlink = millis();
rightButtonState = 0;
} else if (leftButtonState == 0 && rightDoubleState == 0) {
digitalWrite(leftTurnLED, LOW);
}
// Right button active
if (rightButtonState == 1 && millis() - lastRightBlink >= blinkInterval) {
digitalWrite(rightTurnLED, !digitalRead(rightTurnLED));
digitalWrite(leftTurnLED, LOW);
lastRightBlink = millis();
} else if (rightButtonState == 0 && leftDoubleState == 0) {
digitalWrite(rightTurnLED, LOW);
}
lastUpdate = millis();
}
// Update the last button states
lastLeftButtonState = leftButtonValue;
lastRightButtonState = rightButtonValue;
// Reset button states if both are pressed multiple times
if (leftDoubleState > 1 && rightDoubleState > 1) {
leftDoubleState = 0;
rightDoubleState = 0;
leftButtonState = 0;
rightButtonState = 0;
}
// Ensure buttons don't remain at 1 unless pressed together
if (leftButtonState > 1) leftButtonState = 0;
if (rightButtonState > 1) rightButtonState = 0;
}
void loop() {
controlTurnSignals(); // Continuously control the turn signals
}