/*
A simple input sequence game for escape rooms. Replace the green LED
with a latch style lock or a maglock (change line 40, 126 and 128)
*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 26
#define DEBUG
const byte numInputs = 4;
const byte inputPins[numInputs] = {27, 14, 12, 13};
// Change how many steps players need
const byte numSteps = 5;
// Change the order they need to input
const byte steps[numSteps] = {0, 2, 3, 1, 0};
const byte lockPin = 2;
bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
int currentStep = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
for(int i=0; i< numInputs; i++){
pinMode(inputPins[i], INPUT_PULLUP);
}
pinMode(lockPin, OUTPUT);
// Change to HIGH for traditional maglock
digitalWrite(lockPin, LOW);
#ifdef DEBUG
Serial.begin(9600);
Serial.println(F("Let The Game Begin!!"));
#endif
}
void loop() {
if ( (millis() - lastDebounceTime) > debounceDelay) {
for(int i=0; i<numInputs; i++){
int currentInputState = digitalRead(inputPins[i]);
if(currentInputState != lastInputState[i]) {
lastDebounceTime = millis();
}
if(currentInputState == LOW && lastInputState[i] == HIGH) {
if(steps[currentStep] == i) {
// Change the color that comes on as they press buttons
colorWipe(strip.Color(255, 100, 0), 5); // Orange
delay(150);
colorWipe(strip.Color(0, 0, 0), 5); // Black
currentStep++;
#ifdef DEBUG
Serial.print(F("Correct input! Onto step #"));
Serial.println(currentStep);
#endif
}
else {
// Change the color that comes on as they press buttons
// (Must be the same as the previous on line 57)
colorWipe(strip.Color(255, 100, 0), 5); // Orange
delay(150);
colorWipe(strip.Color(0, 0, 0), 5); // Black
currentStep = 0;
Serial.println(F("Incorrect input! Back to the beginning!"));
}
}
lastInputState[i] = currentInputState;
}
}
if(currentStep == numSteps){
onSolve();
}
}
void onSolve()
{
#ifdef DEBUG
// Change the colors for the success sequence)
colorWipe(strip.Color(0, 255, 0), 5); // Green
// delay(500);
colorWipe(strip.Color(0, 0, 255), 5); // Blue
// delay(500);
colorWipe(strip.Color(0, 255, 0), 5); // Green
// delay(500);
colorWipe(strip.Color(0, 0, 255), 5); // Blue
// delay(500);
colorWipe(strip.Color(0, 255, 0), 5); // Green
// delay(500);
colorWipe(strip.Color(0, 0, 255), 5); // Blue
// delay(500);
colorWipe(strip.Color(0, 255, 0), 5); // Green
// delay(500);
colorWipe(strip.Color(0, 0, 255), 5); // Blue
// delay(500);
colorWipe(strip.Color(0, 255, 0), 5); // Green
// delay(500);
colorWipe(strip.Color(0, 0, 255), 5); // Blue
// delay(500);
colorWipe(strip.Color(0, 255, 0), 5); // Green
// delay(500);
colorWipe(strip.Color(0, 0, 255), 5); // Blue
// delay(500);
colorWipe(strip.Color(0, 255, 0), 5); // Green
Serial.println(F("Puzzle Solved!"));
#endif
// Set up for a momentary lock
// Switch HIGH and LOW for a maglock
digitalWrite(lockPin, HIGH);
delay(750);
digitalWrite(lockPin, LOW);
// Change for longer auto reset time
delay(5000);
colorWipe(strip.Color(0, 0, 0), 5); // Black
ESP.restart();
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}