-
Notifications
You must be signed in to change notification settings - Fork 0
/
mega_move counter_testfile
330 lines (263 loc) · 8.98 KB
/
mega_move counter_testfile
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// sketch_aug01a_blink, for arduino Mega und modified DGT clock
// - just a test
// DGT mit REED:
// braun = dig_out (braun) an D7
// rot (+) Vcc
// orange (-)
// two blinking Leds show who is to move
// Serial monitor shows move No and time left
/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
This example detects the pressed and released events of a button without debounce.
*/
//MULTITASKING WITH ARDUINO
//https://roboticsbackend.com/how-to-do-multitasking-with-arduino/
#include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
String a = "one";
String expression1 = "";
String expression2 = "";
String expression1a = "";
String expression2a = "";
int countUp = 0;
unsigned long time0;
unsigned long timew;
unsigned long timewTotal;
unsigned long timeb;
unsigned long timebTotal;
long timew_left = 6000;
long timeb_left = 6000;
int w_hrs, w_mins, w_secs = 0;
int b_hrs, b_mins, b_secs = 0;
unsigned long timeFormove_w;
unsigned long timeFormove_b;
unsigned long time4game = 2; //time for game (minutes);
unsigned long increment = 1; //increment per move (seconds);
unsigned long previousTimeLed4 = millis();
unsigned long previousTimeLed5 = millis();
unsigned long previousTimeLed9 = millis();
unsigned long previousTimeTimecontrol = millis();
long timeIntervalLed4 = 250;
//LED STATES to keep track
int ledState4 = LOW;
int ledState5 = LOW;
int ledState6 = LOW;
int ledState8 = LOW;
int ledState9 = LOW;
long timeIntervalTime_left = 2000;
//long timeIntervalSerialPrint = 2000;
int n = 1;
void setup() {
button.setDebounceTime(50);
button.setCountMode(COUNT_BOTH);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
pinMode(9, OUTPUT);
digitalWrite(9, LOW); //yellow2 LED off
pinMode(8, OUTPUT);
digitalWrite(8, LOW); //yellow LED off
pinMode(6, OUTPUT);
digitalWrite(6, LOW); //red LED off
pinMode(5, OUTPUT);
digitalWrite(5, LOW); //red LED off
pinMode(4, OUTPUT);
digitalWrite(4, HIGH); //green LED on
// Switch on LEDS for 200 msecs
digitalWrite(9, HIGH); //yellow2 LED
digitalWrite(8, HIGH); //yellow LED
digitalWrite(6, HIGH); //red LED on
digitalWrite(5, HIGH); //red LED on
digitalWrite(4, HIGH); //green LED on
delay(500);
digitalWrite(9, LOW); //yellow2 LED off
digitalWrite(8, LOW); //yellow LED off
digitalWrite(5, LOW); //red LED on
digitalWrite(6, LOW); //red LED off
digitalWrite(4, LOW); //green LED off
delay(500);
digitalWrite(9, HIGH); //yellow2 LED
digitalWrite(8, HIGH); //yellow LED
digitalWrite(6, HIGH); //red LED on
digitalWrite(5, HIGH); //red LED on
digitalWrite(4, HIGH); //green LED on
delay(500);
digitalWrite(9, LOW); //yellow2 LED off
digitalWrite(8, LOW); //yellow LED off
digitalWrite(6, LOW); //red LED off
digitalWrite(5, LOW); //red LED off
digitalWrite(4, LOW); //green LED off
Serial.println("LedStates set");
Serial.print("Timew = ");
Serial.print(timew);
Serial.println(" secs");
Serial.print("Timeb = ");
Serial.print(timeb);
Serial.println(" secs");
Serial.print("Movecounter = ");
Serial.println (countUp);
Serial.print("Buttonpressed Counter = ");
Serial.println (button.getCount());
Serial.println("ready! ... ");
Serial.println("Black Player now starts the game by pressing the Lever ");
Serial.println("");
Serial.println("Move" "\t" "Player1[w/b]" "\t" "secs used" "\t" "secs total" "\t" "time left""\t" "\t" "Move " "\t" "Player2[w/b]" "\t" "secs used" "\t" "secs total""\t" "time left");
}
void loop() {
button.loop(); // MUST call the loop() function first
unsigned long count = button.getCount();
int btnState = button.getState();
unsigned long currentTime = millis();
unsigned long currentTime2 = millis();
//TASK 1 Blink LED4
if (btnState = 1) {
if (currentTime - previousTimeLed4 > timeIntervalLed4) {
previousTimeLed4 = currentTime;
ledState4 = !ledState4;
//nächste Zeile auskommentieren um BlinkLed auszuschalten
//digitalWrite(4, ledState4); //blink
}
//Serial.println(ledState8); //Status der gelben LED
//Serial.println(ledState6); //Status der roten LED
}
//TASK0 Zeitkontrolle, intervall 0,5 sec
/*
//TASK 2
if (Serial.available()) {
a = Serial.parseInt();
if (userInput >= 0 && userInput < 256) {
analogWrite(4, userInput);
Serial.println(
}
}
*/
//TASK 2a Serial read Try
/*
while(Serial.available()) {
a= Serial.readString();// read the incoming data as string
Serial.println(a);}
*/
//TASK 3 Schalten von LED9:
if (ledState8 == HIGH and ledState4 == HIGH)
{
ledState9 = HIGH;
previousTimeLed9 = currentTime;
digitalWrite(9, ledState9);
if (currentTime - previousTimeLed9 < timeIntervalLed4) {
ledState9 = !ledState9;
digitalWrite(9, ledState9); //signal
}
}
//Schalten von LED5:
if (ledState6 == HIGH and ledState4 == HIGH)
{
ledState5 = HIGH;
previousTimeLed5 = currentTime;
digitalWrite(5, ledState5);
if (currentTime - previousTimeLed5 < timeIntervalLed4) {
ledState5 = !ledState5;
digitalWrite(5, ledState5); //signal
}
}
if (button.isReleased()) { //black pressed button down to start game, white time is running
digitalWrite(8, HIGH); //yellow LED on
ledState8 = HIGH;
digitalWrite(6, LOW); //red LED off
ledState6 = LOW;
//Serial.print("Button pressed No ");
//Serial.println(button.getCount());
if (button.getCount() == 1) { //Black starts the game bei pressing the Lever as if he has done move 0, time is taken and a starting tone is played
timeb = millis() / 1000;
tone(3, 1000, 300); // after Start by Black => 300 msec Tone 333 Hz
}
if (button.getCount() > 1) {
tone(3, 1000, 30);
timeb = millis() / 1000;
timeFormove_b = -timew + timeb;
timebTotal = timeFormove_b + timebTotal;
timeb_left = time4game * 60 - timebTotal ; //time left in seconds for printintout
timeb_left = timeb_left + button.getCount() / 2 * increment; //mit increment
//Serial.println (timeb);
//Serial.println (timew);
expression2 = String(button.getCount() / 2) + "\t" "BLACK moved" "\t" + String(timeFormove_b) + "\t""\t" + timebTotal + "\t" + "\t";
Serial.print(expression2);
b_secs = timeb_left % 60;
b_mins = (timeb_left % 3600) / 60;
b_hrs = timeb_left / 3600;
if (b_secs < 10) {
expression2a = String(b_hrs) + String(":") + String(b_mins + String(":0")) + String(b_secs);
}
else {
expression2a = String(b_hrs) + String(":") + String(b_mins + String(":")) + String(b_secs);
}
expression2a = "0" + String(b_hrs);
if (b_mins < 10) {
expression2a = expression2a + ":0" + String(b_mins);
}
else {
expression2a = expression2a + ":" + String(b_mins);
}
if (b_secs < 10) {
expression2a = expression2a + ":0" + String(b_secs);
}
else {
expression2a = expression2a + ":" + String(b_secs);
}
if (timeb_left < 0) {
expression2a = "LOSS ON TIME";
tone(3, 1400, 1000);
}
Serial.println(expression2a);
}
}
if (button.isPressed()) { //white time is stopped, Blacks time is running
tone(3, 2000, 30);
digitalWrite(8, LOW); //yellow LED off
ledState8 = LOW;
// LED 6 ist abgeschaltet
digitalWrite(6, HIGH); //red LED on
ledState6 = HIGH;
//Serial.print("Button pressed No ");
//Serial.println(button.getCount());
//Serial.println (timeb);
//Serial.println (timew);
timew = millis() / 1000;
timeFormove_w = timew - timeb;
timewTotal = timeFormove_w + timewTotal;
timew_left = time4game * 60 - timewTotal ;
timew_left = timew_left + button.getCount() / 2 * increment; //mit increment
expression1 = String(button.getCount() / 2) + "\t" + "WHITE moved" "\t" + String(timeFormove_w) + "\t""\t" + timewTotal + "\t" + "\t" ;//timew_left + "\t" + "\t" "\t";
Serial.print(expression1);
w_secs = timew_left % 60;
w_mins = (timew_left % 3600) / 60;
w_hrs = timew_left / 3600;
if (w_secs < 10) {
expression1a = String(w_hrs) + String(":") + String(w_mins + String(":0")) + String(w_secs);
}
else {
expression1a = String(w_hrs) + String(":") + String(w_mins + String(":")) + String(w_secs);
}
expression1a = "0" + String(w_hrs);
if (w_mins < 10) {
expression1a = expression1a + ":0" + String(w_mins);
}
else {
expression1a = expression1a + ":" + String(w_mins);
}
if (w_secs < 10) {
expression1a = expression1a + ":0" + String(w_secs);
}
else {
expression1a = expression1a + ":" + String(w_secs);
}
if (timew_left < 0) {
expression1a = "LOSS ON TIME";
tone(3, 1400, 1000);
}
Serial.print(expression1a + "\t" + "\t");
timeb = millis() / 1000;
}
}