1
2
3
4
5 #include<LiquidCrystal.h>
6 LiquidCrystal lcd(12,11,7,6,5,4);
7
8 int ss1 = 0, ss2 = 0;
9 int pss1 = 0;
10 int ms=0,sec =0, minute=0;
11long interval = 98;
12unsigned long last=0,dur,pre=0,last2=0,dur2;
13 int num_rise=0;
14bool reset=0,row=0;
15
16void timeNprint(){
17 unsigned long curr = millis();
18 if (curr-pre>interval){
19 pre=curr;
20 ms++;
21 if (ms==10) {sec++;ms=0;}
22 if (sec==60) {sec=0;minute++;}
23 lcd.setCursor(0,0);
24 if (minute<10) lcd.print("0");
25 lcd.print(minute);
26 lcd.print(":");
27 if (sec<10) lcd.print("0");
28 lcd.print(sec);
29 lcd.print(":");
30 lcd.print(ms);
31 }
32}
33void printTime(){
34 lcd.setCursor(9,row);
35 row=!row;
36 if (minute<10) lcd.print("0");
37 lcd.print(minute);
38 lcd.print(":");
39 if (sec<10) lcd.print("0");
40 lcd.print(sec);
41 lcd.print(":");
42 lcd.print(ms);
43 }
44void resetNwelcome(){
45 ms=0;minute=0;sec=0;
46 lcd.setCursor(0,1);
47 lcd.print("XW's Stopwatch");
48 lcd.setCursor(0,0);
49 lcd.print("Welcome to use");
50}
51
52void setup() {
53lcd.begin(16,2);
54pinMode(2,INPUT);
55pinMode(3,INPUT);
56}
57
58void loop() {
59 num_rise=0;
60 resetNwelcome();
61 ss1=digitalRead(2);
62if (ss1 == HIGH)
63{
64 num_rise++;
65 last=millis();
66 lcd.clear();
67 while(1)
68 {
69 switch(num_rise%2)
70 {
71 case 0:
72 ss2=digitalRead(3);
73 ss1=digitalRead(2);
74 dur=millis()-last;
75 if (ss2==HIGH) {reset=1;break;}
76 else if(ss1==HIGH && dur>300) {
77 num_rise++;
78 last=millis();}
79 break;
80 case 1:
81 timeNprint();
82 ss1=digitalRead(2);dur=millis()-last;
83 if (ss1==HIGH &&dur>300) {
84 num_rise++;
85 last=millis();
86 }
87 break;
88
89 }
90 ss2=digitalRead(3);
91 dur2=millis()-last2;
92 if(ss2==1 && dur2>300)
93 {
94 last2=millis();
95 printTime();
96 }
97 if(reset==1)
98 {reset=0;lcd.clear();row=0;break;}
99 }
100}
101}
Anonymous user
2 years ago
using Mega 2560, did not get same results