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