Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const unsigned char BUTTON_PIN = 0;
- const unsigned char LED_PIN_1 = 11;
- const unsigned char LED_PIN_2 = 10;
- const unsigned char LED_PIN_3 = 9;
- const int BUTTON_VALUE_1 = 1015;
- const int BUTTON_VALUE_2 = 925;
- const int BUTTON_VALUE_3 = 845;
- const unsigned char NUMBER_OF_ITEMS = 3;
- // GLOBAL
- int currentReading;
- unsigned long currentTimer;
- // GAME VARS
- unsigned char gameCombination[100];
- unsigned long gameTimer;
- unsigned char gameState;
- unsigned char gameLevel;
- unsigned char gameStep;
- unsigned char gameInputDelay;
- unsigned char gameAccel;
- int gameStateDelay;
- int gameMinDelay;
- // LED Light
- class Light {
- unsigned char pin;
- unsigned long timerCheck; // checks led timing
- unsigned char ledState; // led current state
- public:
- Light() = default;
- Light(unsigned char p) { // pin number
- pin = p;
- ledState = 0; // off by default
- timerCheck = 0;
- pinMode(pin, OUTPUT);
- }
- void blink(unsigned long onTime) {
- timerCheck = currentTimer + onTime;
- ledState = 1;
- digitalWrite(pin, HIGH);
- }
- void update() {
- // Check if led is on and if its time to switch it off yet
- if (ledState == 1) {
- if (timerCheck < currentTimer) {
- digitalWrite(pin, LOW);
- ledState = 0;
- }
- }
- }
- };
- // BUTTON
- class Button {
- int triggerValue; // trigger value
- unsigned char triggerFlac; // trigger flactuation
- unsigned char inputDelay; // debounce delay
- unsigned char buttonPressed; // button state
- unsigned char debounce;
- public:
- Button() = default;
- Button(int tV) : triggerValue{tV} {
- debounce = 0;
- buttonPressed = 0;
- triggerFlac = 40;
- inputDelay = 150;
- }
- unsigned char isPressed() {
- return buttonPressed;
- }
- void update() {
- // Check current reading
- if (currentReading > triggerValue - triggerFlac && currentReading < triggerValue + triggerFlac) {
- debounce++;
- } else {
- debounce = 0;
- buttonPressed = 0;
- }
- // The button state is changed if the reading is within
- // the trigger value boundaries for a certain amount of time
- if (debounce > inputDelay) {
- buttonPressed = 1;
- }
- }
- };
- // UTILS
- void _delay(int dl) {
- gameTimer = currentTimer + dl;
- }
- // GAME LIGHTS
- Light gameLights[] = {
- Light(LED_PIN_1),
- Light(LED_PIN_2),
- Light(LED_PIN_3)
- };
- // GAME BUTTONS
- Button gameButtons[] = {
- Button(BUTTON_VALUE_1),
- Button(BUTTON_VALUE_2),
- Button(BUTTON_VALUE_3)
- };
- // SETUP
- void setup() {
- pinMode(BUTTON_PIN, INPUT);
- // RANDOMIZER
- randomSeed(analogRead(5));
- }
- // TODO: Refactor
- // this is currently wathing the last user input
- unsigned char lastUserInput;
- // LOOP
- void loop() {
- currentReading = analogRead(BUTTON_PIN);
- currentTimer = millis();
- if (gameTimer < currentTimer) {
- // STATE :: Start game
- if (gameState == 0) {
- // initialize new game
- gameTimer = 0;
- gameLevel = 0;
- gameState = 1;
- gameStep = 0;
- gameAccel = 0;
- gameStateDelay = 1000;
- gameMinDelay = 500;
- gameInputDelay = 150;
- _delay(gameStateDelay);
- }
- // STATE :: Set combination
- if (gameState == 1) {
- // add new random number to current combination
- gameCombination[gameLevel] = random(3);
- gameLevel = gameLevel + 1;
- gameStep = 0; // reset step
- if (gameStateDelay > gameMinDelay) {
- gameStateDelay -= gameAccel; // decrease game intervals
- }
- _delay(gameStateDelay);
- gameState = 2; // ->> go to STATE 2
- }
- // STATE :: Play current combination
- else if (gameState == 2) {
- if (gameStep < gameLevel) {
- gameLights[gameCombination[gameStep]].blink(150);
- gameStep = gameStep + 1;
- _delay(gameStateDelay);
- }
- else {
- gameStep = 0;
- gameState = 3; // ->> go to STATE 3
- _delay(gameInputDelay);
- }
- }
- // STATE :: User input
- else if (gameState == 3) {
- if (gameStep < gameLevel) {
- // cycle through all buttons
- for (unsigned char i = 0; i < NUMBER_OF_ITEMS; i++) {
- gameButtons[i].update(); // update each button
- // BUTTON PRESSED
- if (gameButtons[i].isPressed() == 1) {
- if (lastUserInput != i) {
- lastUserInput = i;
- gameLights[i].blink(150);
- if (gameCombination[gameStep] == i) {
- // correct press
- gameStep++;
- _delay(gameInputDelay);
- }
- else {
- // wrong press
- gameLevel = gameStep; // used for blinking this led on game over
- gameStep = 0;
- gameState = 4; // ->> got to STATE 4
- }
- }
- break;
- }
- else if (i == 2) { // Reached last button
- // reset user input because none of the buttons are pressed
- lastUserInput = 255;
- }
- }
- }
- else { // reached last button - reset user input
- gameState = 1; // ->> go to STATE 1
- }
- }
- // STATE :: Game over
- else if (gameState == 4) {
- // blink the correct light 10 times
- if (gameStep < 10) {
- gameStep++;
- gameLights[gameCombination[gameLevel]].blink(100);
- _delay(gameStateDelay/5);
- }
- else {
- gameState = 0; // ->> got to STATE 0
- _delay(gameStateDelay);
- }
- }
- }
- // UPDATE LEDs
- for (unsigned char i = 0; i < NUMBER_OF_ITEMS; i++) {
- gameLights[i].update();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement