-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheisser_draht.ino
245 lines (191 loc) · 6.7 KB
/
heisser_draht.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
// software for http://www.kreativekiste.de/elektro/dem-heissen-draht-auf-der-spur
// developed by Timo Denk (Nov 2014)
// modified by Sebastian Sproesser (2015)
// libraries
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SevSeg.h>
#include "heisser_draht.h"
SevSeg sevseg;
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13; // pin 13 will control the backlight
const byte startPin = 22, stopPin = 24, mistakePin = 26, buzzerPin = 28, penaltyTimePin = A0;
const byte minPenaltyTime = 1, maxPenaltyTime = 15; // [t] = 1 s
byte actualStartPin, actualStopPin;
const String penaltyTimeString = "Fehler", finalTimeString = "Endzeit", timeString = "Zeit", commercialString = "FABLAB Neuenstadt", totalTime = "Gesamtzeit";
unsigned long millisStart, millisEnd;
int mistakes, penaltyTime, lastPenaltyTime;
unsigned long lastStatusUpdate = 0;
unsigned long lastMistakes = 0;
void setup() {
// Initialise our game pins
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
pinMode(startPin, INPUT);
pinMode(stopPin, INPUT);
pinMode(mistakePin, INPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(startPin, HIGH);
digitalWrite(stopPin, HIGH);
digitalWrite(mistakePin, HIGH);
// Initialise seven segment display
byte numDigits = 4;
byte digitPins[] = {41, 42, 43, 44};
byte segmentPins[] = {45, 46, 47, 48, 49, 50, 51, 52};
sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
sevseg.setBrightness(100);
// Initialise LCD
lcd.begin(20,4);
Serial.begin(9600);
clearScreen();
}
void loop() {
printStringCenter(1, "Heisser Draht");
// Waiting for start or stop pin
while (digitalRead(startPin) == HIGH &&
digitalRead(stopPin) == HIGH) {
printStringCenter(2, "Bitte auf Start!");
if (lastStatusUpdate) {
printStringCenter(3, "F: " + String(mistakes) + " Zeit: " + sec2MinSecString(lastStatusUpdate + (mistakes * penaltyTime)));
}
}
// Recognize which Pin serves as the starting point for this round.
// The other one will be the stop pin.
if (digitalRead(startPin) == LOW) {
actualStartPin = startPin;
actualStopPin = stopPin;
} else {
actualStartPin = stopPin;
actualStopPin = startPin;
}
strafzeit:
printStringCenter(2, "Strafzeit einstellen");
lastPenaltyTime = 0, penaltyTime = 0;
// waiting for game to start
while (digitalRead(actualStartPin) == LOW) {
penaltyTime = (byte)round(map(analogRead(penaltyTimePin), 0, 1023, minPenaltyTime, maxPenaltyTime));
if (penaltyTime != lastPenaltyTime) {
clearRow(3);
printStringCenter(3, String(penaltyTime) + " s");
lastPenaltyTime = penaltyTime;
}
}
char buf[21];
sprintf(buf,"%02d:%02d.000;000;Start",(int)(penaltyTime/60), (int)(penaltyTime%60));
Serial.println(buf);
// game running
millisStart = millis();
clearScreen();
mistakes = 0;
unsigned long lastDisplayUpdate = 0;
unsigned long lastError = 0;
bool buzzerOn = 0;
while (digitalRead(actualStopPin) == HIGH) {
// Check for reset (by again touching actualStartPin)
// Yes, goto is ugly. I know. But it must be done like this if we don't completely rewrite the code
if (digitalRead(actualStartPin) == LOW) {
if (buzzerOn) {
digitalWrite(buzzerPin, LOW);
buzzerOn = 0;
}
Serial.println("00:00.000;000;Reset");
goto strafzeit;
}
unsigned long now = millis();
sevseg.refreshDisplay();
if (lastDisplayUpdate + 10 < now) {
printStatusOnScreen();
printSevSeg();
lastDisplayUpdate = now;
}
if (!buzzerOn) {
// Check for mistakes
if (digitalRead(mistakePin) == LOW) {
mistakes++;
unsigned long errorMillis = millis();
unsigned long CurrentTime = round((errorMillis - millisStart)/1000) + (mistakes * penaltyTime);
char buf[21];
sprintf(buf,"%02d:%02d.%03d;%03d;Mistake",(int)(CurrentTime/60), (int)(CurrentTime%60), (int)((errorMillis-millisStart)%1000), (int)(mistakes));
Serial.println(buf);
digitalWrite(buzzerPin, HIGH);
buzzerOn = 1;
lastError = now;
}
}
if (lastError + 1000 < now) {
// Reset buzzer
if (buzzerOn) {
digitalWrite(buzzerPin, LOW);
buzzerOn = 0;
}
lastError = now;
}
}
// Game finished
millisEnd = millis();
unsigned long CurrentTime = round((millisEnd - millisStart)/1000) + (mistakes * penaltyTime);
sprintf(buf,"%02i:%02i.%03i;%03i;Stop",(int)(CurrentTime/60), (int)(CurrentTime%60), (int)((millisEnd-millisStart)%1000), (int)(mistakes));
Serial.println(buf);
clearRow(0);
printStringCenter(0, "Spiel beendet!");
printStatusOnScreen();
// Show the result for 5 seconds
while (millisEnd + 5000 > millis()) {
sevseg.refreshDisplay();
}
clearScreen();
}
unsigned long lastPlayingTime = 0;
void printSevSeg() {
unsigned long currentMillis = millis();
unsigned long playingTime = round((currentMillis - millisStart)/1000) + (mistakes * penaltyTime);
if (playingTime != lastPlayingTime) {
int seconds = playingTime % 60;
int minutes = playingTime / 60;
char buf[21];
sprintf(buf,"%02i:%02i.%03i;%03i;Time",(int)(playingTime/60), (int)(playingTime%60), (int)((currentMillis-millisStart)%1000), (int)(mistakes));
Serial.println(buf);
int displayTime = minutes * 100 + seconds;
sevseg.setNumber(displayTime,2);
lastPlayingTime = playingTime;
}
}
void printStatusOnScreen() {
unsigned long playingTime = round((millis() - millisStart)/1000), penaltyTimeSum = mistakes * penaltyTime;
if ((lastStatusUpdate != playingTime) ||
(lastMistakes != mistakes) ) {
printStringCenter(1, "Spielzeit: " + sec2MinSecString(playingTime));
printStringCenter(2, "Fehler: " + String(mistakes) + " x " + String(penaltyTime) + "s");
printStringCenter(3, "Gesamt: " + sec2MinSecString(playingTime + penaltyTimeSum));
lastStatusUpdate = playingTime;
lastMistakes = mistakes;
}
}
void showCommercial() {
clearRow(0);
printStringCenter(0, commercialString);
}
void clearScreen() {
for (byte i = 0; i < 4; i++)
clearRow(i);
showCommercial();
}
void printStringCenter(byte row, String string) {
lcd.setCursor((int)((20 - string.length()) / 2), row);
lcd.print(string);
}
void clearRow(byte row) {
lcd.setCursor(0, row);
lcd.print(" ");
}
String sec2MinSecString(int input) { // converts seconds to a string like "minutes:seconds"
char buf[6];
int minutes = (int)(input / 60);
int seconds = (int)(input % 60);
if (minutes > 99) {
minutes = 99;
}
sprintf(buf, "%d:%02d ", minutes, seconds);
return String(buf);
}