const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
// Variables
int buttonState = 0; // variable for reading the pushbutton status
int countdown = 0; // initial countdown time in seconds
bool countdownStarted = false; // flag to track if countdown has started
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
Serial.begin(9600); // initialize serial communication
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed and countdown hasn't started
if (buttonState == HIGH && !countdownStarted) {
countdownStarted = true; // mark countdown as started
countdown = 10; // set initial countdown time
}
// if countdown has started, decrement countdown time
if (countdownStarted) {
// print the countdown time
Serial.println(countdown);
// check if countdown reaches zero
if (countdown == 0) {
// turn on the LED
digitalWrite(ledPin, HIGH);
countdownStarted = false; // stop the countdown
} else {
// decrement countdown time
countdown--;
// wait for a second
delay(1000);
}
}
}