const int startStopButton = 4; // the pin number of the start/stop button
unsigned long startTime = 0; // the time the timer was started
unsigned long elapsedTime = 0; // the elapsed time of the timer
bool isRunning = false; // whether the timer is currently running
void setup() {
pinMode(startStopButton, INPUT_PULLUP); // set the start/stop button as input with pull-up resistor
Serial.begin(9600); // initialize serial communication
}
void loop() {
if (digitalRead(startStopButton) == LOW) { // if the start/stop button is pressed
if (isRunning) { // if the timer is running, stop it
elapsedTime = millis() - startTime;
isRunning = false;
} else { // if the timer is not running, start it
startTime = millis() - elapsedTime;
isRunning = true;
}
delay(200); // debounce the button
}
if (isRunning) { // if the timer is running, print the elapsed time
unsigned long currentTime = millis();
elapsedTime = currentTime - startTime;
Serial.print("Elapsed time: ");
Serial.print(elapsedTime / 1000);
Serial.println(" seconds");
}
}