SHOW:
|
|
- or go back to the newest paste.
1 | const unsigned char BUTTON_PIN = 0; | |
2 | const unsigned char LED_PIN_1 = 11; | |
3 | const unsigned char LED_PIN_2 = 10; | |
4 | const unsigned char LED_PIN_3 = 9; | |
5 | - | const int BUTTON_VALUE_1 = 845; |
5 | + | const int BUTTON_VALUE_1 = 1015; |
6 | const int BUTTON_VALUE_2 = 925; | |
7 | - | const int BUTTON_VALUE_3 = 1015; |
7 | + | const int BUTTON_VALUE_3 = 845; |
8 | ||
9 | // GLOBAL | |
10 | int currentReading; | |
11 | unsigned long currentTimer; | |
12 | ||
13 | // GAME VARS | |
14 | unsigned char gameCombination[100]; | |
15 | unsigned long gameTimer = 0; | |
16 | unsigned char gameState = 0; | |
17 | unsigned char gameLevel = 0; | |
18 | - | unsigned char gameTempo = 100; |
18 | + | unsigned char gameAccel = 25; |
19 | unsigned char gameStep = 0; | |
20 | unsigned char gameInputDelay = 150; | |
21 | int gameStateDelay = 1000; | |
22 | int gameMinDelay = 500; | |
23 | ||
24 | // LED Light | |
25 | class Light { | |
26 | unsigned char pin; | |
27 | unsigned long timerCheck; // checks led timing | |
28 | ||
29 | unsigned char ledState; // led current state | |
30 | ||
31 | public: | |
32 | Light() = default; | |
33 | Light(unsigned char p) { // pin number | |
34 | pin = p; | |
35 | ledState = 0; // off by default | |
36 | ||
37 | timerCheck = 0; | |
38 | ||
39 | pinMode(pin, OUTPUT); | |
40 | } | |
41 | ||
42 | unsigned char getPin() { | |
43 | return pin; | |
44 | } | |
45 | ||
46 | void blink(unsigned long onTime) { | |
47 | timerCheck = currentTimer + onTime; | |
48 | ledState = 1; | |
49 | digitalWrite(pin, HIGH); | |
50 | } | |
51 | ||
52 | void update() { | |
53 | // Check if led is on and if its time to switch it off yet | |
54 | if (ledState == 1) { | |
55 | if (timerCheck < currentTimer) { | |
56 | digitalWrite(pin, LOW); | |
57 | ledState = 0; | |
58 | } | |
59 | } | |
60 | ||
61 | } | |
62 | }; | |
63 | ||
64 | // BUTTON | |
65 | class Button { | |
66 | int triggerValue; // trigger value | |
67 | unsigned char triggerFlac; // trigger flactuation | |
68 | unsigned char inputDelay; // debounce delay | |
69 | ||
70 | unsigned char buttonPressed; // button state | |
71 | unsigned char debounce; | |
72 | ||
73 | public: | |
74 | ||
75 | Button() = default; | |
76 | Button(int tV) : triggerValue{tV} { | |
77 | debounce = 0; | |
78 | buttonPressed = 0; | |
79 | ||
80 | triggerFlac = 40; | |
81 | inputDelay = 150; | |
82 | } | |
83 | ||
84 | unsigned char isPressed() { | |
85 | return buttonPressed; | |
86 | } | |
87 | ||
88 | void update() { | |
89 | ||
90 | // Check current reading | |
91 | if (currentReading > triggerValue - triggerFlac && currentReading < triggerValue + triggerFlac) { | |
92 | debounce++; | |
93 | } else { | |
94 | debounce = 0; | |
95 | buttonPressed = 0; | |
96 | } | |
97 | ||
98 | // The button state is changed if the reading is within | |
99 | // the trigger value boundaries for a certain amount of time | |
100 | if (debounce > inputDelay) { | |
101 | buttonPressed = 1; | |
102 | } | |
103 | ||
104 | } | |
105 | }; | |
106 | ||
107 | // UTILS | |
108 | void _delay(int dl) { | |
109 | gameTimer = currentTimer + dl; | |
110 | } | |
111 | ||
112 | ||
113 | ||
114 | // GAME LIGHTS | |
115 | Light gameLights[] = { | |
116 | Light(LED_PIN_1), | |
117 | Light(LED_PIN_2), | |
118 | Light(LED_PIN_3) | |
119 | }; | |
120 | ||
121 | // GAME BUTTONS | |
122 | Button gameButtons[] = { | |
123 | Button(BUTTON_VALUE_1), | |
124 | Button(BUTTON_VALUE_2), | |
125 | Button(BUTTON_VALUE_3) | |
126 | }; | |
127 | ||
128 | ||
129 | // SETUP | |
130 | void setup() { | |
131 | ||
132 | pinMode(BUTTON_PIN, INPUT); | |
133 | ||
134 | // RANDOMIZER | |
135 | randomSeed(analogRead(5)); | |
136 | ||
137 | } | |
138 | ||
139 | // TODO: Refactor | |
140 | // this is currently wathing the last user input | |
141 | unsigned char lastUserInput; | |
142 | ||
143 | - | // reset all variables |
143 | + | |
144 | // LOOP | |
145 | void loop() { | |
146 | ||
147 | - | gameTimer = currentTimer + gameStateDelay; |
147 | + | |
148 | currentTimer = millis(); | |
149 | ||
150 | if (gameTimer < currentTimer) { | |
151 | ||
152 | // STATE :: Start game | |
153 | if (gameState == 0) { | |
154 | // play intro | |
155 | if (gameStep < 3) { | |
156 | - | gameTimer = currentTimer + gameStateDelay; |
156 | + | |
157 | ||
158 | gameStep++; | |
159 | _delay(gameStateDelay); | |
160 | } | |
161 | // initialize new game | |
162 | gameLevel = 0; | |
163 | gameState = 1; | |
164 | gameStep = 0; | |
165 | - | gameTimer = currentTimer + gameStateDelay; |
165 | + | _delay(gameStateDelay); |
166 | } | |
167 | ||
168 | // STATE :: Set combination | |
169 | if (gameState == 1) { | |
170 | - | gameTimer = currentTimer + gameInputDelay; |
170 | + | |
171 | gameCombination[gameLevel] = random(3); | |
172 | gameLevel = gameLevel + 1; | |
173 | gameStep = 0; // reset step | |
174 | if (gameStateDelay > gameMinDelay) { | |
175 | gameStateDelay -= gameAccel; // decrease game intervals | |
176 | } | |
177 | _delay(gameStateDelay); | |
178 | gameState = 2; // ->> go to STATE 2 | |
179 | } | |
180 | ||
181 | // STATE :: Play current combination | |
182 | else if (gameState == 2) { | |
183 | if (gameStep < gameLevel) { | |
184 | gameLights[gameCombination[gameStep]].blink(150); | |
185 | gameStep = gameStep + 1; | |
186 | _delay(gameStateDelay); | |
187 | } | |
188 | else { | |
189 | gameStep = 0; | |
190 | gameState = 3; // ->> go to STATE 3 | |
191 | _delay(gameInputDelay); | |
192 | - | gameTimer = currentTimer + gameInputDelay; |
192 | + | |
193 | } | |
194 | ||
195 | // STATE :: User input | |
196 | else if (gameState == 3) { | |
197 | if (gameStep < gameLevel) { | |
198 | ||
199 | // cycle through all buttons | |
200 | for (unsigned char i = 0; i < 3; i++) { | |
201 | gameButtons[i].update(); // update each button | |
202 | ||
203 | // BUTTON PRESSED | |
204 | if (gameButtons[i].isPressed() == 1) { | |
205 | if (lastUserInput != i) { | |
206 | lastUserInput = i; | |
207 | ||
208 | gameLights[i].blink(150); | |
209 | ||
210 | if (gameCombination[gameStep] == i) { | |
211 | // correct press | |
212 | gameStep++; | |
213 | _delay(gameInputDelay); | |
214 | } | |
215 | else { | |
216 | // wrong press | |
217 | gameLevel = gameStep; // used for blinking this led on game over | |
218 | gameStep = 0; | |
219 | gameState = 4; // ->> got to STATE 4 | |
220 | } | |
221 | - | gameTimer = currentTimer + gameStateDelay/5; |
221 | + | |
222 | ||
223 | break; | |
224 | } | |
225 | - | gameTimer = currentTimer + gameStateDelay; |
225 | + | |
226 | // reset user input because none of the buttons are pressed | |
227 | lastUserInput = 255; | |
228 | } | |
229 | } | |
230 | } | |
231 | else { // reached last button - reset user input | |
232 | gameState = 1; // ->> go to STATE 1 | |
233 | } | |
234 | } | |
235 | ||
236 | // STATE :: Game over | |
237 | else if (gameState == 4) { | |
238 | // blink the correct light 10 times | |
239 | if (gameStep < 10) { | |
240 | gameStep++; | |
241 | gameLights[gameCombination[gameLevel]].blink(100); | |
242 | _delay(gameStateDelay/5); | |
243 | } | |
244 | else { | |
245 | gameState = 0; // ->> got to STATE 0 | |
246 | _delay(gameStateDelay); | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | ||
252 | for (unsigned char i = 0; i < 3; i++) { | |
253 | gameLights[i].update(); | |
254 | } | |
255 | ||
256 | } |