// Serial Monitor Stopwatch
// Created by: Gengler, Alexander February 17th, 2024
// Three button stopwatch with buttons declared as, start, stop, and reset. Timer counts up from zero
// with place holders as shown, "minutes:Seconds:1/10 Seconds"
// Example: 12:34:1, 12 minutes, 34.1 seconds
const int bt_start = A0; // Declares Start button as Analog Pin 0
const int bt_stop = A1; // Declares Stop button as Analog Pin 1
const int bt_reset = A2; // Declares Reset button as Analog Pin 2
const int GLED = 9; // Declares GREEN LED output on Pin 9
const int RLED = 8; // Declares RED LED output on Pin 8
byte hr = 0; // hours
byte mm = 0; // minutes
byte ss = 0; // seconds
byte ms = 0; // 1/10 second
bool timerStart = false; //boulin to flip between true and false.
void setup()
{
Serial.begin(9600);
pinMode(bt_start, INPUT_PULLUP); // Declares bt_start as an input into serial
pinMode(bt_stop, INPUT_PULLUP); // Declares bt_stop as an input into serial
pinMode(bt_reset, INPUT_PULLUP); // Declares bt_reset as an input into serial
pinMode(RLED, OUTPUT); // Declares RLED as output
pinMode(GLED, OUTPUT); // Declares GLED as output
Serial.println("Stopwatch"); //Prints "Stopwatch", into serial monitor
}
void loop() { //Runs in a loop
if(digitalRead(bt_start) == 0){ // If statement runs if start button is pressed
timerStart = true; // Start stopwatch
digitalWrite(GLED, HIGH); // Turn LED on
digitalWrite(RLED, LOW); // Turn LED off
Serial.println("Timer Started"); // Prints "Timer Started", into serial monitor
delay(500); //Delays for half second to prevent button spaming
}
if(digitalRead(bt_stop) == 0){ // If statement runs if stop button is pressed
timerStart = false; // Stop stopwatch
Serial.println("Timer Stopped"); // Prints "Timer Stopped", into serial monitor
digitalWrite(GLED, LOW); // Turn LED off
digitalWrite(RLED, HIGH); // Turn LED on
delay(500); //Delays for half second to prevent button spaming
}
if(digitalRead(bt_reset) == 0 ){ // If statement runs if reset button is pressed
ms=0; //resets count back to zero
ss=0; //resets count back to zero
mm=0; //resets count back to zero
hr=0; //resets count back to zero
Serial.println("Timmer Reset"); // Prints "Timer Reset", into serial monitor
delay(500); //Delays for half second to prevent button spaming
}
if(timerStart==true){ // If statement runs if boulian is flipped to true
//Serial.print("Min:Sec:1/10Sec ");
Serial.print(mm);
Serial.print(":");
Serial.print(ss);
Serial.print(":");
Serial.println(ms); // Serial prints "mm:ss:ms", functions
}
if(timerStart == true){ // If statement runs if boulian is flipped to true
ms=ms+1;
delay(100);
if(ms>9)
{ ms=0;ss=ss+1;
if(ss>59)
{ss=0; mm=mm+1; // Timer counts time by rapidly adding 1/10 of a second every 1/10 of a second
}
}
}
}