-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch with RESET function
48 lines (42 loc) · 1.34 KB
/
Stopwatch with RESET function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//This code is an tutorial of how to request the time via the keyboard input "T" in the Serial Monitor and 'R' to reset stopwatch
int elapsedTimeSeconds = 0; // Variable to store the elapsed time in seconds
int elapsedTimeMinutes = 0; // Variable to store the elapsed time in minutes
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Check if serial data is available
if (Serial.available() > 0) {
// Read the incoming byte
char keyboardInput = Serial.read();
// Check if the incoming byte is 'T'
if (keyboardInput == 'T') {
// Display the elapsed time
Serial.print("TIME = ");
Serial.print(elapsedTimeMinutes);
Serial.print("m ");
Serial.print(elapsedTimeSeconds);
Serial.println("s");
}
if (keyboardInput == 'R') {
// Reset time
int elapsedTimeSeconds = 0;
int elapsedTimeMinutes = 0;
Serial.print("TIME = ");
Serial.print(elapsedTimeMinutes);
Serial.print("m ");
Serial.print(elapsedTimeSeconds);
Serial.println("s");
}
}
// Delay for 1 second
delay(1000);
// Increment the elapsed time in seconds
elapsedTimeSeconds++;
// Check if 1 minute has passed
if (elapsedTimeSeconds == 60) {
// Reset seconds and increment minutes
elapsedTimeSeconds = 0;
elapsedTimeMinutes++;
}
}