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
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
sw1:1
sw1:2
sw1:3
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
rgb2:R
rgb2:COM
rgb2:G
rgb2:B
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
led1:A
led1:C
led2:A
led2:C