-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame1.ino
162 lines (147 loc) · 4.12 KB
/
game1.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
#include <FastLED.h>
#define LED_PIN 7
#define PAD_LEFT 4
#define PAD_RIGHT 8
#define RESET 2
#define NUM_LEDS 300
#define BRIGHTNESS 31 // 0-255
#define DEBOUNCE_DELAY 200 //ms
#define LEFT_COLOUR CRGB::Green
#define RIGHT_COLOUR CRGB::Purple
#define INIT_COLOUR CRGB::Red
CRGB leds[NUM_LEDS];
int leftButton = HIGH;
int rightButton = HIGH;
int leftButtonPrev = HIGH;
int rightButtonPrev = HIGH;
unsigned long leftDebounce = 0;
unsigned long rightDebounce = 0;
int score;
int scorePrev;
void setup() {
Serial.begin(9600);
pinMode(PAD_LEFT, INPUT_PULLUP);
pinMode(PAD_RIGHT, INPUT_PULLUP);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
// Score starts in the middle
score = NUM_LEDS / 2;
scorePrev = score;
ledInit();
Serial.println("Program Started");
Serial.println(score);
}
void loop() {
unsigned long now=millis();
//read and debounce Left
leftButton = digitalRead(PAD_LEFT);
if ((leftButton == HIGH) && (leftButtonPrev == LOW)) {
leftDebounce = now;
} else if ((leftButton == LOW) && (leftButtonPrev == HIGH) && (now - leftDebounce > DEBOUNCE_DELAY)) {
score--;
}
leftButtonPrev = leftButton;
//read and debounce Right
rightButton = digitalRead(PAD_RIGHT);
if ((rightButton == HIGH) && (rightButtonPrev == LOW)) {
rightDebounce = now;
} else if ((rightButton == LOW) && (rightButtonPrev == HIGH) && (now - rightDebounce > DEBOUNCE_DELAY)) {
score++;
}
rightButtonPrev = rightButton;
if (score >= NUM_LEDS) {
Serial.println("Blue Wins");
for (int j = 0; j < 5; j++) {
for (int i = 0; i <= (NUM_LEDS-1)/2; i++) {
leds[i*2] = RIGHT_COLOUR;
leds[i*2+1] = CRGB::Black;
}
FastLED.show();
delay(500);
for (int i = 0; i <= (NUM_LEDS-1)/2; i++) {
leds[i*2] = CRGB::Black;
leds[i*2+1] = RIGHT_COLOUR;
}
FastLED.show();
delay(500);
}
for (int i = 0; i <= (NUM_LEDS-1)/2; i++) {
leds[i*2+1] = CRGB::Black;
}
FastLED.show();
Serial.println("End Game. Press Reset");
while (1) delay (10000);
} else if (score <=0) {
Serial.println("Blue Wins");
for (int j = 0; j < 5; j++) {
for (int i = 0; i <= (NUM_LEDS-1)/2; i++) {
leds[i*2] = LEFT_COLOUR;
leds[i*2+1] = CRGB::Black;
}
FastLED.show();
delay(500);
for (int i = 0; i <= (NUM_LEDS-1)/2; i++) {
leds[i*2] = CRGB::Black;
leds[i*2+1] = LEFT_COLOUR;
}
FastLED.show();
delay(500);
}
for (int i = 0; i <= (NUM_LEDS-1)/2; i++) {
leds[i*2+1] = CRGB::Black;
}
FastLED.show();
Serial.println("End Game. Press Reset");
while (1) delay (10000);
}
if (score != scorePrev) {
Serial.println(score);
for (int i = 0; i <= (NUM_LEDS-1); i++) {
if (i < score) {
leds[i] = RIGHT_COLOUR;
} else {
leds[i] = LEFT_COLOUR;
}
}
FastLED.show();
scorePrev = score;
}
//delay(5);
}
void ledInit() {
// countdown
for (int i = 0; i <= (NUM_LEDS-1); i++) {
leds[i] = INIT_COLOUR;
}
FastLED.show();
delay(1000);
for (int i = NUM_LEDS*0.75; i <= (NUM_LEDS-1); i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(1000);
for (int i = NUM_LEDS*0.5; i <= (NUM_LEDS*0.75-1); i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(1000);
for (int i = NUM_LEDS*0.25; i <= (NUM_LEDS*0.5-1); i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(1000);
for (int i = 0; i <= (NUM_LEDS*0.25-1); i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(1000);
// reset score to the middle
for (int i = 0; i <= (NUM_LEDS-1); i++) {
if (i < score) {
leds[i] = RIGHT_COLOUR;
} else {
leds[i] = LEFT_COLOUR;
}
}
FastLED.show();
}