-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chess_clock.ino
124 lines (115 loc) · 2.44 KB
/
Chess_clock.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
#include <LiquidCrystal.h>
#define btnBLACK 22
#define btnWHITE 23
#define sensorPin 0
const int interval = 1000;
unsigned long previousMillis = 0;
int whiteTimer = 0;
int blackTimer = 0;
int sensorValue = 0;
short playerMode = 0;
bool start = false;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(26, 27, 50, 51, 52, 53);
void DisplayTimer(int timeLeft, int col = 0)
{
lcd.setCursor(col, 1);
lcd.print(timeLeft / 60);
lcd.print(":");
if (timeLeft % 60 > 9)
{
lcd.print(timeLeft % 60);
}
else
{
lcd.print("0");
lcd.print(timeLeft % 60);
}
}
void DisplayPlayer(short player)
{
switch (player)
{
case 1:
{
lcd.setCursor(0, 0);
lcd.print(" BLACK >WHITE<");
DisplayTimer(whiteTimer, 10);
break;
}
case 2:
{
lcd.setCursor(0, 0);
lcd.print(">BLACK< WHITE ");
DisplayTimer(blackTimer, 1);
break;
}
}
}
void SetTimer()
{
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, 0, 1023, 3, 1);
switch (sensorValue)
{
case 1:
whiteTimer = 60;
blackTimer = 60;
break;
case 2:
whiteTimer = 180;
blackTimer = 180;
break;
case 3:
whiteTimer = 300;
blackTimer = 300;
break;
}
DisplayTimer(whiteTimer, 10);
DisplayTimer(blackTimer, 1);
}
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" BLACK WHITE ");
pinMode(btnWHITE, INPUT);
pinMode(btnBLACK, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (start == true)
{
if ((unsigned long)(currentMillis - previousMillis) >= interval && whiteTimer > 0 && blackTimer > 0)
{
previousMillis = currentMillis;
(playerMode == 1) ? whiteTimer-- : blackTimer--;
}
(playerMode == 1) ? DisplayPlayer(1) : DisplayPlayer(2);
if (whiteTimer == 0 || blackTimer == 0)
{
start = false;
if (whiteTimer == 0)
{
lcd.setCursor(0, 0);
lcd.print(">WINNER<>LOOSER<");
}
else if (blackTimer == 0)
{
lcd.setCursor(0, 0);
lcd.print(">LOOSER<>WINNER<");
}
}
if (digitalRead(btnWHITE) == HIGH)
playerMode = 2;
else if (digitalRead(btnBLACK) == HIGH)
playerMode = 1;
}
else
{
SetTimer();
if (digitalRead(btnWHITE) == HIGH || digitalRead(btnBLACK) == HIGH)
start = true;
playerMode = 1;
delay(200);
}
}