-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRundenzähler.ino
265 lines (231 loc) · 5.02 KB
/
Rundenzähler.ino
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <Wire.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h>
unsigned long starta, startb, ttimea, ttimeb;
unsigned limit = 600;
unsigned timea, timeb, besta, bestb;
unsigned rundea, rundeb;
unsigned statea, olda, stateb, oldb;
int i, disp;
char buffer[18];
// lightbeams
char pina = 6;
char pinb = 7;
// pushbutton
char pushb = 4; //Reset
char pushc = 3; //Start
#define I2C_ADDRESS 0x3C
// noname I2C Display
SSD1306AsciiWire oled;
void setup() {
Wire.begin();
pinMode (pina, INPUT);
pinMode (pinb, INPUT);
pinMode (pushb, INPUT);
pinMode (pushc, INPUT);
oled.begin(&SH1106_128x64, I2C_ADDRESS);
oled.setFont(Adafruit5x7);
oled.clear();
oled.set2X();
oled.setCursor (0, 1);
oled.print("Herrmann");
delay (500);
oled.setCursor (0, 3);
oled.print(" Track");
delay (1000);
oled.set1X();
oled.setCursor (0, 6);
oled.print(" Kupferzell");
delay (3000);
oled.clear();
oled.set2X();
oled.setCursor (0, 1);
oled.print(" Welcome");
delay (1500);
// oled.setCursor (0, 3);
// oled.print(" Track");
// delay (1000);
oled.set1X();
oled.setCursor (0, 6);
oled.print(" !! Have fun !!");
delay (3000);
// small animation <img draggable="false" role="img" class="emoji" alt="🙂" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f642.svg">
// oled.clear();
// oled.set2X();
// for (i = 10; i > -1; i--) {
// sprintf (buffer, " %d", i);
// oled.setCursor (0, 3);
// oled.print(buffer);
// delay (1000);
// }
cowntdown();
// main display now
oled.clear();
oled.set1X();
starta = millis();
startb = millis();
update_loops();
}
void loop() {
if (digitalRead (pushb) == HIGH) {
delay (20);
// wait for release
while (!digitalRead (pushb)) {};
cowntdown();
starta = millis();
startb = millis();
rundea = 0;
rundeb = 0;
besta = 0;
bestb = 0;
oled.clear();
update_loops();
}
statea = digitalRead (pina);
if (statea != olda) {
// start counting when line goes up
if (statea) {
// debouncing: time between changes must be > 1s
if ((millis() - starta) > 1000) {
// display last time
timea = (unsigned) (millis() - starta);
if (!besta && rundea) {
besta = timea; // first round
}
else if (timea < besta) {
besta = timea; // best time
}
rundea++;
}
starta = millis(); //start new counter
update_loops();
update_times();
}
}
olda = statea;
// the same procedure for track b
stateb = digitalRead (pinb);
if (stateb != oldb) {
//start counting when line goes up
if (stateb) {
// debouncing: time between changes must be > 1s
if ((millis() - startb) > 1000) {
// display last time
timeb = (unsigned) (millis() - startb);
if (!bestb && rundeb) {
bestb = timeb; // first round
}
else if (timeb < bestb) {
bestb = timeb; // best time
}
rundeb++;
}
startb = millis(); //start new counter
update_loops();
update_times();
}
}
oldb = stateb;
delay (10);
}
void convert(unsigned t) {
unsigned s, ms;
s = t / 1000;
ms = (t % 1000) / 10; // only 2 digits
sprintf (buffer, "%2d.%02d s", s, ms);
}
void update_loops() {
char c;
oled.set2X();
if (statea) {
c= '!'; // Auto gemessen
} else {
c= ' '; // kein Echo
}
sprintf (buffer, "%4d%c", rundea,c);
oled.setCursor (0, 1); oled.print (buffer);
if (stateb) {
c= '!'; // Auto gemessen
} else {
c= ' '; // kein Echo
}
sprintf (buffer, "%4d%c", rundeb,c);
oled.setCursor (64, 1); oled.print (buffer);
oled.set1X();
}
void update_times() {
oled.setCursor (0, 4);
convert (timea); oled.print (buffer);
oled.setCursor (70, 4);
convert (timeb); oled.print (buffer);
oled.setCursor (0, 6);
convert (besta); oled.print (buffer);
oled.setCursor (70, 6);
convert (bestb); oled.print (buffer);
}
void cowntdown() {
oled.clear();
oled.set2X();
oled.setCursor (0, 2);
oled.print(" Ready ?");
delay (1000);
oled.set1X();
oled.setCursor (0, 7);
oled.print(" Push Button!");
delay (4000);
while (!digitalRead (pushc)== HIGH) {};
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 10");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 9");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 8");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 7");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 6");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 5");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 4");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 3");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 2");
delay (1000);
oled.clear();
oled.set2X();
oled.setCursor (0, 3);
oled.print(" 1");
delay (1000);
oled.clear();
oled.setCursor (0, 3);
oled.print(" Go!!!");
delay (100);
}