-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch(Min&Sec)
130 lines (123 loc) · 3.02 KB
/
Stopwatch(Min&Sec)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
unsigned long lastOut = 0;
LiquidCrystal lcd(12,11,5,4,8,7);
const int b1 = 2, b2 = 3;
int op = 0; //op=0 for 0 state, op=1 for running state, op=2 for paused state
int m=0,s=0;
int ct=0;
int buttonState;
int lastButton1State = LOW;
int lastButton2State = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup()
{
Serial.begin(9600);
pinMode(b1,INPUT);
pinMode(b2,INPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.print(" 0 sec");
}
void loop()
{
int reading1 = digitalRead(b1);
if (reading1 != lastButton1State)
{
lastDebounceTime = millis(); // Reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) // Check if state change has lasted for longer time than debounce delay
{
if (reading1 != buttonState)
{
buttonState = reading1;
if (buttonState == HIGH) // Only shift if button is pressed
{
if (op==1 || op==2)
{
op=0;
lcd.setCursor(0,1);
lcd.print(" Stop "); //Stop the timer
delay(1000);
}
else
if (op==0)
{
op=1;
lcd.setCursor(0,1);
lcd.print(" Start "); //Start the timer
delay(1000);
}
}
}
}
int reading2 = digitalRead(b2);
if (reading2 != lastButton2State)
{
lastDebounceTime = millis(); // Reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) // Check if state change has lasted for longer time than debounce delay
{
if (reading2 != buttonState)
{
buttonState = reading2;
if (buttonState == HIGH) // Only shift if button is pressed
{
if (op==1)
{
op=2;
lcd.setCursor(0,1);
lcd.print(" Paused "); //Pause the timer
delay(1000);
}
else
if (op==2)
op=1;
}
}
}
if (op==1)
{
lcd.clear();
s++;//count second
if (s==60)
{
m++;//count minute
s=0;//reset second
}
if (m<1)//print only sec
{
lcd.print(" ");
lcd.print(s);
lcd.print(" sec");
}
else
if (m>0)//print min and sec
{
lcd.print(" ");
lcd.print(m);
lcd.print(" min ");
lcd.print(s);
lcd.print(" sec");
}
//output to serial to confirm LCD output
Serial.print("S=");
Serial.print(s);
Serial.print(" M=");
Serial.println(m);
}
if (op==0)
{
//reset
s=0;
m=0;
lcd.clear();
lcd.print(" 0 sec ");
}
//reset lastButtonstates for debouncing
lastButton1State = reading1;
lastButton2State = reading2;
delay(1000);//delay 1 sec
}