const int buttonPin = 2; // Pin for the button
const int ledPin = 3; // Pin for the LED
bool buttonPressed = false; // Variable to track if the button is pressed
unsigned long buttonPressTime = 0; // Variable to store the time the button was pressed
unsigned long ledOnTime = 0; // Variable to store the time when LED turned on
int waittime = 5000; // Variable to store the Wating time after button pressed
int duty = 2000; // Effective duty time
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == HIGH && !buttonPressed) {
buttonPressed = true;
buttonPressTime = millis(); // Record the time when the button is pressed
}
// Check if 5 seconds have elapsed since button press
if (buttonPressed && millis() - buttonPressTime >= waittime ) {
digitalWrite(ledPin, HIGH); // Turn on the LED
ledOnTime = millis(); // Record the time when the LED turned on
buttonPressed = false; // Reset button state
}
// Check if 2 seconds have elapsed since LED turned on
if (digitalRead(ledPin) == HIGH && millis() - ledOnTime >= duty)
{
digitalWrite(ledPin, LOW); // Turn off the LED
}
}