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