unsigned long pressStartTime = 0; // Store time when button is pressed
bool buttonPressed = false; // Track button press state
void setup() {
pinMode(2, INPUT); // Button pin
pinMode(8, OUTPUT); // LED 1 pin
pinMode(9, OUTPUT); // LED 2 pin
pinMode(10, OUTPUT); // LED 3 pin (optional for further timing control)
digitalWrite(8, LOW); // Ensure LED 1 is off initially
digitalWrite(9, LOW); // Ensure LED 2 is off initially
digitalWrite(10, LOW); // Ensure LED 3 is off initially
}
void loop() {
// Check if button is pressed and not already pressed before
if (digitalRead(2) == HIGH && !buttonPressed) {
pressStartTime = millis(); // Record time of press
buttonPressed = true; // Set button press state to true
}
if (buttonPressed) {
// Turn on LED 1 after 1 second
if (millis() - pressStartTime > 1000) {
digitalWrite(8, HIGH);
}
// Turn on LED 2 after 3 seconds
if (millis() - pressStartTime > 3000) {
digitalWrite(9, HIGH);
}
// Turn on LED 3 after 5 seconds (optional)
if (millis() - pressStartTime > 5000) {
digitalWrite(10, HIGH);
}
// Reset if button is released
if (digitalRead(2) == LOW) {
buttonPressed = false; // Reset button state
digitalWrite(8, LOW); // Turn off LED 1
digitalWrite(9, LOW); // Turn off LED 2
digitalWrite(10, LOW); // Turn off LED 3 (optional)
}
}
}