-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.ino
284 lines (224 loc) · 5.33 KB
/
sketch.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <FastLED.h>
#include <LinkedList.h>
const int PIN_LED = 4;
const int PIN_BUTTON_P1 = 2;
const int PIN_BUTTON_P2 = 3;
const int LED_COUNT = 30;
const int LIFES = 5;
const int START_SPEED = 50;
CRGB leds[LED_COUNT] = {};
int pos = 15;
int direction = 1;
int speed = START_SPEED;
int speeds[10] = { 10, 30, 50, 100, 200, 200, 200, 200, 200, 200 };
int lastPressed = -1;
bool game_started = false;
LinkedList<int> barriers_p1 = LinkedList<int>();
LinkedList<int> barriers_p2 = LinkedList<int>();
void setup() {
Serial.begin(9600);
FastLED.clear();
FastLED.show();
FastLED.setBrightness(150);
for(int i = LED_COUNT - LIFES; i < LED_COUNT; i++) {
barriers_p2.add(i);
}
for(int i = 0; i < LIFES; i++) {
barriers_p1.add(i);
}
pinMode(PIN_BUTTON_P1, INPUT);
pinMode(PIN_BUTTON_P2, INPUT);
FastLED.addLeds<WS2812, PIN_LED, GRB>(leds, LED_COUNT);
}
void clear_strip() {
FastLED.clear();
FastLED.show();
}
void fill_strip(CRGB color) {
for (int i = 0; i < LED_COUNT; i++) {
leds[i] = color;
}
FastLED.show();
}
void light_up(CRGB color) {
clear_strip();
fill_strip(color);
delay(300);
}
void reset_game() {
pos = 15;
game_started = false;
lastPressed = -1;
speed = START_SPEED;
}
void p1_win_stip() {
for(int i = 0; i <= 10; i++) {
clear_strip();
delay(500);
for (int i = 0; i < LIFES; i++) {
leds[i] = CRGB::Blue;
}
for (int i = LED_COUNT - LIFES; i < LED_COUNT; i++) {
leds[i] = CRGB::Red;
}
FastLED.show();
delay(500);
}
}
void p2_win_stip() {
for(int i = 0; i <= 10; i++) {
clear_strip();
delay(500);
for (int i = 0; i < LIFES; i++) {
leds[i] = CRGB::Red;
}
for (int i = LED_COUNT - LIFES; i < LED_COUNT; i++) {
leds[i] = CRGB::Blue;
}
FastLED.show();
delay(500);
}
}
void move() {
if (direction == 2) {
pos++;
} else {
pos--;
}
if (pos > 29) {
direction = 1;
}
if (pos < 0) {
direction = 2;
}
bool isPlaygroundExceededAtP1 = pos < 0;
bool isPlaygroundExceededAtP2 = pos > LED_COUNT - 1;
if(isPlaygroundExceededAtP1) {
barriers_p1.add(barriers_p1.size());
barriers_p2.remove(0);
lastPressed = -1;
speed = START_SPEED;
light_up(CRGB::Red);
}
if(isPlaygroundExceededAtP2) {
barriers_p2.unshift(LED_COUNT - barriers_p2.size() - 1);
barriers_p1.pop();
lastPressed = -1;
speed = START_SPEED;
light_up(CRGB::Green);
}
if(barriers_p1.size() == 0) {
p1_win_stip();
reset_game();
}
if(barriers_p2.size() == 0) {
p2_win_stip();
reset_game();
}
}
unsigned long previousMillisStart = 0;
void start_blink() {
FastLED.clear();
unsigned long currentMillis = millis();
unsigned long diff = currentMillis - previousMillisStart;
if (diff >= 1000) {
leds[15] = CRGB::Red;
FastLED.show();
}
if(diff >= 2000) {
leds[15] = CRGB::Black;
FastLED.show();
previousMillisStart = currentMillis;
}
}
unsigned long previousMillisLeds = 0;
bool has_update() {
unsigned long currentMillis = millis();
unsigned long diff = currentMillis - previousMillisLeds;
if(diff >= speed) {
previousMillisLeds = currentMillis;
return true;
} else {
return false;
}
}
void update_leds() {
FastLED.clear();
int bp1_size = barriers_p1.size();
int bp2_size = barriers_p2.size();
for (int bp1 = 0; bp1 < bp1_size; bp1++) {
leds[barriers_p1.get(bp1)] = CRGB::Green;
}
for (int bp2 = 0; bp2 < bp2_size; bp2++) {
leds[barriers_p2.get(bp2)] = CRGB::Red;
}
for (int i = 0; i < LED_COUNT; i++) {
if (i == pos) {
leds[i] = CRGB::Blue;
}
}
FastLED.show();
}
bool pressedP1() {
return digitalRead(PIN_BUTTON_P1) == HIGH;
}
bool pressedP2() {
return digitalRead(PIN_BUTTON_P2) == HIGH;
}
int getAccelerationP1() {
int bp1_size = barriers_p1.size();
if(pos > bp1_size) {
return -1;
}
return pos;
}
int getAccelerationP2() {
int bp2_size = barriers_p2.size();
if(pos < LED_COUNT - bp2_size - 1) {
return -1;
}
return LED_COUNT - 1 - pos;
}
void loop() {
if(pressedP1() && !game_started) {
game_started = true;
direction = 2;
}
if(pressedP2() && !game_started) {
game_started = true;
direction = 1;
}
if(!game_started) {
start_blink();
return;
}
int bp1_size = barriers_p1.size();
int bp2_size = barriers_p2.size();
if(has_update()) {
move();
update_leds();
}
bool isInBarrierP1 = pos < bp1_size;
bool isInBarrierP2 = pos > LED_COUNT - bp2_size - 1;
if(pos == 15) {
lastPressed = -1;
}
if(pressedP1() && isInBarrierP1 && lastPressed != 2) {
lastPressed = 2;
speed = speeds[getAccelerationP1()];
direction = 2;
Serial.print("\nacc:");
Serial.print(getAccelerationP1());
Serial.print("\nspeed:");
Serial.print(speed);
}
if(pressedP2() && isInBarrierP2 && lastPressed != 1) {
lastPressed = 1;
speed = speeds[getAccelerationP2()];
direction = 1;
Serial.print("\nacc:");
Serial.print(getAccelerationP2());
Serial.print("\nspeed:");
Serial.print(speed);
}
}