/
rebound_build07_code.txt
executable file
·306 lines (252 loc) · 7.97 KB
/
rebound_build07_code.txt
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
/*
"rebound" 1 player version
author: John Hamm
created 4.2.15
build 07 - finalize scoring + display
build 06 - add RGB LED with result feedback
+ correct button being pressed for period of time (ignore after 1st press)
+ start sequence
+ add "great" zone/alter "good" zone
+ add serial feedback of zone + speed
build 05 - 1 player game setup (no brightness controls of LED runs)
*/
#include <ShiftRegister.h> //uses shiftRegisterLib from Leonardo Banderali
ShiftRegister sr(1, 8, 10, 9, 12);
/*
-parameters:
1 shift register
pin 8 -> data
pin 10 -> clock
pin 9 -> latch
pin 12 -> reset
*/
int buttonApin = 13;
int ballSpeed = 300; //initial time between ball positions
int speedIncrease = 0; //factor to increase speed
int score=0; //scoring value
int buttonPress = 0; //track if button pushed in time 0=no, 1=good, 2=great
int earlyZone = 50; //% of ballSpeed interval where button press in rebound zone is too early
int goodZone = 25; //% of ballSpeed interval where button press in rebound zone is good
int greatZone = 25; //% of ballSpeed interval where button press in rebound zone is great (early + good + great=100%=interval between led 0 and home)
int lateZone = 50; //% of ballSpeed interval where button press in rebound zone is late, but accepted
int redHomePin = 3; //pin for homeRGB LED connected to red
int greenHomePin = 5; //pin for homeRGB LED connected to red
int blueHomePin = 6; //pin for homeRGB LED connected to red
int scoreTens = 0; //final scoring tabulation
int scoreOnes = 0;
int counter = 0; //for score display calculation
//------------------------------setup---------------------------------
void setup() {
pinMode(buttonApin, INPUT_PULLUP);
Serial.begin(9600);
sr.setShiftOrder(MSBFIRST); //set order in which bits are shifted (MSBFIRST/LSBFIRST)
sr.clear(); //set all bits to low
while (! Serial); // Wait untilSerial is ready
Serial.println("Ready to start..."); //serial output used throughout to verify gameplay
allOff();
delay(1000);
startSeq(); //start sequence - home LED blinks purple 3x, then green once
}
//-----------------------------game loop---------------------------------------
void loop() {
Serial.print("ball speed is ");
Serial.println(ballSpeed);
runUpDown();
reboundCheck();
if (buttonPress == 0) {
endGameLate();
}
if (buttonPress == 1) {
ballSpeedIncrease();
}
if (buttonPress == 2) {
ballSpeedDecrease();
}
}
//------------------functions---------------------------
//cycle of LEDs from start to end (0-7) and back (6-1) with check for early button push
void runUpDown() {
buttonPress=0;
for (int i = 0; i < 8; i++) {
for (int k = 0; k < 100; k++) {
sr.setBit(i, HIGH);
checkButtonBad();
delay(ballSpeed / 100);
}
sr.setBit(i, LOW);
}
allOff(); // to turn off home RGB LED feedback from previous rebound
for (int i = 6; i > 0; i--) {
for (int k = 0; k < 100; k++) {
sr.setBit(i, HIGH);
checkButtonBad();
delay(ballSpeed / 100);
}
sr.setBit(i, LOW);
}
}
//check bad button press during runUpDown + early rebound
void checkButtonBad() {
if (digitalRead(buttonApin) == LOW) {
Serial.println("too soon! ");
endGameEarly();
}
}
//check for button press in the rebound "zone"
void reboundCheck() {
for (int k = 0; k < earlyZone; k++) { //check 100x/interval for early button press 1st % of interval time
sr.setBit(0, HIGH);
checkButtonBad();
delay(ballSpeed / 100);
}
for (int k = 0; k < goodZone; k++) { //check for early button press in good % of interval time
if (buttonPress == 0) {
checkButtonGood();
}
delay(ballSpeed / 100);
}
for (int k = 0; k < greatZone; k++) { //check for early button press in great % of interval time
if (buttonPress == 0) {
checkButtonGreat();
}
delay(ballSpeed / 100);
}
sr.setBit(0, LOW); //turn off 1st ball
for (int k = 0; k < lateZone; k++) { //check for late button press % after
if (buttonPress == 0) {
checkButtonLate();
}
delay(ballSpeed / 100);
}
}
//check button press during "good" zone
void checkButtonGood() {
if (digitalRead(buttonApin) == LOW) {
buttonPress=1;
score = score + 1;
analogWrite(greenHomePin, 120);
analogWrite(blueHomePin, 0);
Serial.println("good! ");
Serial.print("score: ");
Serial.println(score);
}
}
//check button press during "great" zone
void checkButtonGreat() {
if (digitalRead(buttonApin) == LOW) {
buttonPress=2;
score = score + 3;
analogWrite(greenHomePin, 0);
Serial.println("great! ");
Serial.print("score: ");
Serial.println(score);
}
}
//check button press during "late" zone
void checkButtonLate() {
if (digitalRead(buttonApin) == LOW) {
buttonPress=1;
analogWrite(greenHomePin, 120);
analogWrite(redHomePin, 120);
Serial.println("late, but still good! ");
}
}
void ballSpeedIncrease() {
speedIncrease = ballSpeed / 10; //increase ballSpeed by 10% if in good zone, or late zone
ballSpeed = ballSpeed - speedIncrease;
}
void ballSpeedDecrease() {
speedIncrease = ballSpeed / 10;
ballSpeed = ballSpeed + speedIncrease; //decrease ballSpeed by 10% if in good zone, or late zone
}
//button pressed too early
void endGameEarly() {
Serial.print("Game over. Final score: ");
Serial.println(score);
scoreDisplay();
}
//button not pressed in time
void endGameLate() {
Serial.println("too late sucker!");
Serial.print("Game over. Final score: ");
Serial.println(score);
scoreDisplay();
}
//turns all LEDs off
void allOff() {
for (int i=0; i<8; i++) {
sr.setBit(i, LOW);
analogWrite(redHomePin, 255);
analogWrite(greenHomePin, 255);
analogWrite(blueHomePin, 255);
}
}
//signify end of game, then display score
void scoreDisplay() {
for (int k=0; k<10; k++){ //flash all LEDs + home Red 10x
for (int i=0; i<8; i++) {
sr.setBit(i, HIGH);
}
analogWrite(redHomePin, 0);
delay(50);
allOff();
delay(50);
}
delay (1000);
for (int k=1; k<10; k++) { //calculate how many "tens" and "ones" in score
counter=k*10;
if (score > counter -1) {
scoreTens = counter/10;
scoreOnes = score - counter;
}
if (score < 10) {
scoreOnes = score;
}
}
Serial.print("scoreTens: ");
Serial.println(scoreTens);
Serial.print("scoreOnes: ");
Serial.println(scoreOnes);
for (int k = 0; k <3; k++) { //display score 3x
for (int i=0; i<scoreTens; i++) { //display "tens" as green home flashes
analogWrite(greenHomePin, 0);
delay(500);
allOff();
delay(500);
}
if (scoreOnes > 0) {
for (int i=7; i>7-scoreOnes; i--) { //display "ones" as red leds from 8 to 0
sr.setBit(i, HIGH);
if (i < 0) {
analogWrite(redHomePin, 0);
}
delay (500);
}
}
delay (1000);
allOff();
delay(500);
}
score = 0; //reset values + start loop again
ballSpeed = 300;
scoreTens = 0;
scoreOnes = 0;
buttonPress = 0;
Serial.println("Try again...");
startSeq();
}
//start sequence
void startSeq() {
for (int i = 0; i < 3; i++) {
allOff();
analogWrite(redHomePin, 0);
analogWrite(blueHomePin, 0);
delay (50);
allOff();
delay (ballSpeed - 50);
}
analogWrite(greenHomePin, 0);
delay (50);
allOff();
delay (ballSpeed - 50);
}