Build Sheets

Source

Escape_Game_Final_Directional.ino

// INCLUDES
// For debouncing button input
#include <Bounce2.h>

const int buttonLed = 3;
const int dirButtonPins[4] = {4, 5, 6, 7};
const int numSteps = 8;
const int submitButtonPin = 8;


Bounce buttons[4] = {Bounce(), Bounce(), Bounce(), Bounce()};
Bounce submitButton = Bounce();

int currentStep = 0;
bool inputCorrect = true;
bool messageSent;
bool submitted;
// Enumerate each of the possible directional inputs
enum Dir {Down, Right, Up, Left};
const byte correctSequence[numSteps] = {Up, Right, Down, Down, Left, Up, Right, Up};


void setup(){

  Serial.begin(9600);
  pinMode(buttonLed, OUTPUT);
  digitalWrite(buttonLed, LOW);
    // Attach a debouncer to every direction switch
  for(int i=0; i<4; i++){
    buttons[i].attach(dirButtonPins[i], INPUT_PULLUP);
  }
  // And for the optional check button
  submitButton.attach(submitButtonPin, INPUT_PULLUP);
  messageSent = false;
  submitted = false;

}

void loop(){
  
  submitButton.update();
  if (submitButton.fell()){
    digitalWrite(buttonLed, HIGH);
    delay(500);
    digitalWrite(buttonLed, LOW);
    currentStep = 0;
  };

  for(int i=0; i<4; i++){    
    // Update the state of this switch
    buttons[i].update();
    // ...has it just been pressed?
    if(buttons[i].fell()){
    
      if(i == correctSequence[currentStep]){
        
        inputCorrect = true;
        Serial.println("true");
        currentStep++;
        Serial.println(currentStep);

      } else {
        // Set flag that user has made at least one incorrect entry in the sequence
        inputCorrect = false;
        Serial.println("false");
        Serial.println(currentStep);
        currentStep = 0;
        }
  
      if(currentStep == numSteps){
        
        if (inputCorrect == true && messageSent == false){
        digitalWrite(buttonLed, HIGH);
        On_Solve();
        Serial.println("buttonpush");
        Serial.println("solved");
        delay(500);
        Serial.println("solved");
        messageSent = true;
           
        }     
      }     
    }
  }
}

void On_Solve(){ 
  digitalWrite(buttonLed, HIGH);
  submitted = true;    
  messageSent = true;
  currentStep = 0; 
}