const int firstSwitchPin = 6; // Pin connected to first switch
const int secondSwitchPin = 4; // Pin connected to second switch
const int ledPin = 3; // Pin connected to LED
bool firstSwitchPressed = false; // Flag to keep track of first switch press
bool secondSwitchPressed = false; // Flag to keep track of second switch press
unsigned long firstSwitchTime = 0; // Time of first switch press
void setup() {
pinMode(firstSwitchPin, INPUT_PULLUP);
pinMode(secondSwitchPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Check if first switch has been pressed
if (digitalRead(firstSwitchPin) == LOW && !firstSwitchPressed) {//0
firstSwitchPressed = true;
secondSwitchPressed = false;
firstSwitchTime = millis();
if (digitalRead(firstSwitchPin) == LOW)
Serial.println(firstSwitchPressed);
}
// Check if second switch has been pressed within 10 seconds of first switch press
if (digitalRead(secondSwitchPin) == LOW && firstSwitchPressed && !secondSwitchPressed) {
if (millis() - firstSwitchTime < 5000) {
//secondSwitchPressed = true;
digitalWrite(ledPin, LOW); // LED off
firstSwitchPressed = false;
secondSwitchPressed = true;
}
}
// Check if time limit has passed without second switch press
if (millis() - firstSwitchTime >= 5000 && firstSwitchPressed && !secondSwitchPressed) {
digitalWrite(ledPin, HIGH); // LED on
if (digitalRead(secondSwitchPin) == LOW && !secondSwitchPressed)
{
digitalWrite(ledPin, LOW);
secondSwitchPressed = false;
firstSwitchPressed = false;
}
}
}