Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This puzzle requires the user to enter a set of inputs in the correct order.
- * i.e. press a sequence of buttons.
- * The number of inputs and the length of sequence are customisable.
- */
- // DEFINES
- // Provides debugging information over serial connection
- #define DEBUG
- // definisce i numeri dei pin
- const int stepPin = 3; //3
- const int dirPin = 4; //4
- // CONSTANTS
- // Definisce il numero di possibili "ingressi", ovvero il numero di interruttori, pulsanti ecc. Che il lettore può premere
- const byte numInputs = 5;
- // A quali pin sono collegati quei pulsanti? (l'altro filo dovrebbe andare a terra)
- const byte inputPins[numInputs] = { 5, 6, 2, 4, 7, };
- // Definisce il numero di passaggi nella sequenza che il giocatore deve seguire
- const byte numSteps = 5;
- // La sequenza corretta di input richiesta per risolvere il puzzle.
- const byte steps[numSteps] = { 4, 3, 2, 1, 0};// i.e. press button #2 once, then button #3 twice, then button #0, then button #1.
- // Questi pin sono usati per illuminare i LED per mostrare i progressi del giocatore, quindi un pin di uscita per passo nel puzzle.
- const byte ledPins[numSteps] = { 11, 8, 9, 12, 10,};
- // Questo pin verrà spinto BASSO per rilasciare un blocco quando il puzzle è risolto
- const byte lockPin = A0;
- // GLOBALS
- // Assume the default state of each switch is HIGH.
- bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
- // What step of the sequence is the player currently on?
- int currentStep = 0;
- // Switches can "bounce" when they open/close, generating a flurry of false readings
- // To prevent this, we'll add a short delay between each time an input value
- // is read.
- // The last time the input switch was toggled
- unsigned long lastDebounceTime = 0;
- // The amount of time (in ms) to wait before reading again
- unsigned long debounceDelay = 50;
- // Setup function runs once when first starting (or resetting) the board
- void setup() {
- pinMode(stepPin,OUTPUT);
- pinMode(dirPin,OUTPUT);
- // Initialise the input pins that have switches attached
- for(int i=0; i< numInputs; i++){
- pinMode(inputPins[i], INPUT_PULLUP);
- }
- // Initialise the LED pins that show progress through the sequence
- for(int i=0; i< numSteps; i++){
- pinMode(ledPins[i], OUTPUT);
- }
- for(int x = 1; x < 500; x++) {
- digitalWrite(stepPin,HIGH);
- delayMicroseconds(5000);
- digitalWrite(stepPin,LOW);
- delayMicroseconds(5000);
- }
- // Set the lock pin as output and secure the lock
- pinMode(lockPin, OUTPUT); //OUTPUT
- digitalWrite(lockPin, HIGH);
- #ifdef DEBUG
- // Open communications on serial port
- Serial.begin(9600);
- Serial.println(F("Serial communication started"));
- #endif
- }
- // The main program loop runs continuously
- void loop() {
- // Check that we've waited at least "debounceDelay" since last input
- if ( (millis() - lastDebounceTime) > debounceDelay) {
- // Loop through all the inputs
- for(int i=0; i<numInputs; i++){
- int currentInputState = digitalRead(inputPins[i]);
- // If the input has changed, reset the debounce timer
- if(currentInputState != lastInputState[i]) {
- lastDebounceTime = millis();
- }
- // If the input is currently being pressed (and wasn't before)
- // Note that since the input pins are configured as INPUT_PULLUP,
- // they read as LOW when pressed and HIGH when not.
- if(currentInputState == LOW && lastInputState[i] == HIGH) {
- // Was this the correct input for this step of the sequence?
- if(steps[currentStep] == i) {
- currentStep++;
- #ifdef DEBUG
- Serial.print(F("Correct input! Onto step #"));
- Serial.println(currentStep);
- #endif
- }
- // Incorrect input!
- else {
- currentStep = 0;
- Serial.println(F("Incorrect input! Back to the beginning!"));
- }
- }
- // Update the stored value for this input
- lastInputState[i] = currentInputState;
- }
- }
- // Check whether the puzzle has been solved
- if(currentStep == numSteps){
- onSolve();
- }
- // Turn on the number of LEDs corresponding to the current step
- for(int i=0; i<numSteps; i++){
- digitalWrite(ledPins[i], (i < currentStep ? HIGH : LOW));
- }
- }
- // Takes action when the puzzle becomes solved
- void onSolve(){
- #ifdef DEBUG
- // Print a message
- Serial.println(F("Puzzle Solved!"));
- #endif
- // Release the lock
- digitalWrite(lockPin, HIGH);
- // Loop forever
- while(true){
- // Flash LEDs
- for(int i=0; i<numSteps; i++){
- digitalWrite(ledPins[i], HIGH);
- delay(100);
- }
- delay(100);
- for(int i=0; i<numSteps; i++){
- digitalWrite(ledPins[i], LOW);
- delay(100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement