Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. /**
  2.  
  3. * This puzzle requires the user to enter a set of inputs in the correct order.
  4. * i.e. press a sequence of buttons.
  5. * The number of inputs and the length of sequence are customisable.
  6. */
  7.  
  8. // DEFINES
  9. // Provides debugging information over serial connection
  10. #define DEBUG
  11. // definisce i numeri dei pin
  12. const int stepPin = 3; //3
  13. const int dirPin = 4; //4
  14.  
  15. // CONSTANTS
  16. // Definisce il numero di possibili "ingressi", ovvero il numero di interruttori, pulsanti ecc. Che il lettore può premere
  17. const byte numInputs = 5;
  18. // A quali pin sono collegati quei pulsanti? (l'altro filo dovrebbe andare a terra)
  19. const byte inputPins[numInputs] = { 5, 6, 2, 4, 7, };
  20. // Definisce il numero di passaggi nella sequenza che il giocatore deve seguire
  21. const byte numSteps = 5;
  22. // La sequenza corretta di input richiesta per risolvere il puzzle.
  23. 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.
  24. // Questi pin sono usati per illuminare i LED per mostrare i progressi del giocatore, quindi un pin di uscita per passo nel puzzle.
  25. const byte ledPins[numSteps] = { 11, 8, 9, 12, 10,};
  26. // Questo pin verrà spinto BASSO per rilasciare un blocco quando il puzzle è risolto
  27. const byte lockPin = A0;
  28.  
  29. // GLOBALS
  30. // Assume the default state of each switch is HIGH.
  31. bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
  32. // What step of the sequence is the player currently on?
  33. int currentStep = 0;
  34.  
  35. // Switches can "bounce" when they open/close, generating a flurry of false readings
  36. // To prevent this, we'll add a short delay between each time an input value
  37. // is read.
  38. // The last time the input switch was toggled
  39. unsigned long lastDebounceTime = 0;
  40. // The amount of time (in ms) to wait before reading again
  41. unsigned long debounceDelay = 50;
  42.  
  43. // Setup function runs once when first starting (or resetting) the board
  44. void setup() {
  45. pinMode(stepPin,OUTPUT);
  46. pinMode(dirPin,OUTPUT);
  47. // Initialise the input pins that have switches attached
  48. for(int i=0; i< numInputs; i++){
  49. pinMode(inputPins[i], INPUT_PULLUP);
  50. }
  51.  
  52. // Initialise the LED pins that show progress through the sequence
  53. for(int i=0; i< numSteps; i++){
  54. pinMode(ledPins[i], OUTPUT);
  55. }
  56. for(int x = 1; x < 500; x++) {
  57. digitalWrite(stepPin,HIGH);
  58. delayMicroseconds(5000);
  59. digitalWrite(stepPin,LOW);
  60. delayMicroseconds(5000);
  61. }
  62.  
  63. // Set the lock pin as output and secure the lock
  64. pinMode(lockPin, OUTPUT); //OUTPUT
  65. digitalWrite(lockPin, HIGH);
  66.  
  67.  
  68. #ifdef DEBUG
  69. // Open communications on serial port
  70. Serial.begin(9600);
  71. Serial.println(F("Serial communication started"));
  72. #endif
  73. }
  74.  
  75. // The main program loop runs continuously
  76. void loop() {
  77.  
  78. // Check that we've waited at least "debounceDelay" since last input
  79. if ( (millis() - lastDebounceTime) > debounceDelay) {
  80.  
  81. // Loop through all the inputs
  82. for(int i=0; i<numInputs; i++){
  83. int currentInputState = digitalRead(inputPins[i]);
  84.  
  85. // If the input has changed, reset the debounce timer
  86. if(currentInputState != lastInputState[i]) {
  87. lastDebounceTime = millis();
  88. }
  89.  
  90. // If the input is currently being pressed (and wasn't before)
  91. // Note that since the input pins are configured as INPUT_PULLUP,
  92. // they read as LOW when pressed and HIGH when not.
  93. if(currentInputState == LOW && lastInputState[i] == HIGH) {
  94. // Was this the correct input for this step of the sequence?
  95. if(steps[currentStep] == i) {
  96. currentStep++;
  97.  
  98. #ifdef DEBUG
  99. Serial.print(F("Correct input! Onto step #"));
  100. Serial.println(currentStep);
  101. #endif
  102. }
  103. // Incorrect input!
  104. else {
  105. currentStep = 0;
  106. Serial.println(F("Incorrect input! Back to the beginning!"));
  107. }
  108. }
  109.  
  110. // Update the stored value for this input
  111. lastInputState[i] = currentInputState;
  112. }
  113. }
  114.  
  115. // Check whether the puzzle has been solved
  116. if(currentStep == numSteps){
  117. onSolve();
  118. }
  119.  
  120. // Turn on the number of LEDs corresponding to the current step
  121. for(int i=0; i<numSteps; i++){
  122. digitalWrite(ledPins[i], (i < currentStep ? HIGH : LOW));
  123. }
  124. }
  125.  
  126. // Takes action when the puzzle becomes solved
  127. void onSolve(){
  128.  
  129. #ifdef DEBUG
  130. // Print a message
  131. Serial.println(F("Puzzle Solved!"));
  132. #endif
  133.  
  134. // Release the lock
  135. digitalWrite(lockPin, HIGH);
  136.  
  137.  
  138. // Loop forever
  139. while(true){
  140.  
  141. // Flash LEDs
  142. for(int i=0; i<numSteps; i++){
  143. digitalWrite(ledPins[i], HIGH);
  144. delay(100);
  145. }
  146. delay(100);
  147. for(int i=0; i<numSteps; i++){
  148. digitalWrite(ledPins[i], LOW);
  149. delay(100);
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement