Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. #include <Display.h> // Include the Display library
  2. #include <IRremote.h> // Include the IR Remote library
  3. #define CLK 10 // Define the CLK pin
  4. #define DIO 11 // Define the DIO pin
  5.  
  6. DisplayHardware display(CLK, DIO); //Create a new display variable
  7.  
  8. const int BTN_R = 8, LED_R = 4; // Create the buttons and lights variables
  9. const int BTN_L = 9, LED_G = 5;
  10. const int LED_B = 6;
  11.  
  12. const int buzzer = 3; // Create the buzzer variable
  13.  
  14. int CODE[] = {1,2,3,4}; // Create a variable for the code
  15. int input[] = {0,0,0,0}; // Create a variable for the user input
  16.  
  17. int newPass[] = {1,2,3,4}; // Create a variable for storing the new code
  18.  
  19. bool lock_l = false, lock_r = false; // Create two variables for the button locks
  20. bool entering = false; // Create a variable to check if the user is in input mode
  21. bool editing = false; // Create a variable to check if the user is in change code mode
  22. int digit = 0; // Create a variable for storing the currently edited digit's index
  23. int numbersRec = 0; // Create a variable for counting how many digits are recieved from the remote
  24.  
  25. IRreceive irrecv(2); // Create a new IR receiver variable
  26. decode_results results; // Create a variable for storing the recieved information
  27.  
  28.  
  29. /* Set all pin modes, display and IR reciever */
  30. void setup() {
  31. pinMode(LED_R, OUTPUT);
  32. pinMode(BTN_R, INPUT_PULLUP);
  33. pinMode(LED_G, OUTPUT);
  34. pinMode(BTN_L, INPUT_PULLUP);
  35.  
  36. pinMode(LED_B, OUTPUT);
  37.  
  38. display.show("0000");
  39. irrecv.enable();
  40. }
  41.  
  42. /* Called when a new number is received from the remote */
  43. void edit_digit(int digit){
  44. display.showCharAt(numbersRec, 48 + digit); // Show the number on the display at the correct place
  45. newPass[numbersRec] = digit; // Change the digit in the new code
  46. numbersRec++; // Increase the number of received digits
  47. tone(buzzer, 2000); // Enable the buzzer
  48. delay(100);
  49. noTone(buzzer);
  50. }
  51.  
  52. /* Main functionality */
  53. void loop(){
  54. if (irrecv.decode(&results)){ // If the board receives an IR signal
  55. irrecv.resume(); // Receive the next value
  56. if(results.value == 0xFF15EA && !entering){ // If the signal is the "Play" button and the user is not in input mode
  57. if(!editing){ // If we're not in edit mode
  58. digitalWrite(LED_B, HIGH); // Turn on the blue LED
  59. display.show(" "); // Clear the display
  60. editing = true; // Change to edit mode
  61. }else{ // If we're in edit mode
  62. digitalWrite(LED_B, LOW); // Turn off the blue LED
  63. for(int i=0; i<4; i++)CODE[i] = newPass[i]; // Set the code to the newly written one
  64. display.show("0000"); // Clear the display
  65. editing = false; // Turn off edit mode
  66. numbersRec = 0; // Reset the received numbers counter back to zero
  67. }
  68. }
  69. }
  70.  
  71. if(editing){ // If we're in edit mode
  72. if(numbersRec < 4){ // And we haven't yet received all four numbers of the new code
  73. if(irrecv.decode(&results)){ // Start listening for an IR signal
  74. switch(results.value){ // If the signal corresponds to a number on the remote
  75. case 0xFF16E9: // 0
  76. edit_digit(0);
  77. break;
  78. case 0xFF0CF3: // 1
  79. edit_digit(1);
  80. break;
  81. case 0xFF18E7: // 2
  82. edit_digit(2);
  83. break;
  84. case 0xFF5EA1: // 3
  85. edit_digit(3);
  86. break;
  87. case 0xFF08F7: // 4
  88. edit_digit(4);
  89. break;
  90. case 0xFF1CE3: // 5
  91. edit_digit(5);
  92. break;
  93. case 0xFF5AA5: // 6
  94. edit_digit(6);
  95. break;
  96. case 0xFF42BD: // 7
  97. edit_digit(7);
  98. break;
  99. case 0xFF52AD: // 8
  100. edit_digit(8);
  101. break;
  102. case 0xFF4AB5: // 9
  103. edit_digit(9);
  104. break;
  105. }
  106. }
  107. }else{ // If we recieved four numbers loop back to the first one in case of changes
  108. numbersRec = 0;
  109. }
  110. }
  111.  
  112. if(!digitalRead(BTN_L) && !editing){ // If the "Input" button is pressed while not changing the code
  113. if(!lock_l){ // And it's not locked
  114. lock_l = true; // Lock it, until released
  115.  
  116. if(entering){ // If the user is entering digits
  117. if(++input[digit] == 10)input[digit] = 0; // Increment the digit at the correct index and reset it back to one if it reaches ten
  118. digitalWrite(LED_G, HIGH); // Blink the green light
  119. delay(200);
  120. digitalWrite(LED_G, LOW);
  121. display.showDigitAt(digit, input[digit]); // Update the changed digit on the display
  122. }else{ // If the user is not entering digits
  123. digitalWrite(LED_G, HIGH); // Light both LEDs for 1sec
  124. digitalWrite(LED_R, HIGH);
  125. delay(1000);
  126. digitalWrite(LED_G, LOW);
  127. digitalWrite(LED_R, LOW);
  128. entering = true; // Start entering digits from the next button press
  129. }
  130. if(digitalRead(BTN_L))lock_l = false; // If the button has been released, unlock the lock
  131. }
  132. }else{
  133. lock_l = false; // If the button is not pressed, make sure it's unlocked
  134. }
  135.  
  136. if(!digitalRead(BTN_R) && !editing){ // If the "Confirm" button is pressed while not changing the code
  137. if(!lock_r){ // And it's not locked
  138. lock_r = true; // Lock it, until released
  139. if(entering){ // If the user is entering digits
  140. if(++digit == 4){ // Increment the edited digit's index until it reaches above the last one
  141. digit = 0; // When that happens, set the edited digit's index back to the first one
  142.  
  143. bool correct = true; // A new variable for checking if the input is correct
  144.  
  145. for(int i=0; i<4; i++){ // Compare each digit of the input to each digit of the code
  146. if(input[i] != CODE[i]){ // If there's discrepancy, stop checking
  147. correct = false;
  148. break;
  149. }
  150. }
  151.  
  152. if(correct){ // If the input is the same as the code light up the green LED for 2s and play the buzzer
  153. digitalWrite(LED_G, HIGH);
  154. tone(buzzer, 1500);
  155. delay(200);
  156. tone(buzzer, 1800);
  157. delay(700);
  158. noTone(buzzer);
  159. delay(1100);
  160. digitalWrite(LED_G, LOW);
  161. }else{ // If the input differs from the code light up the red LED for 2s and play the buzzer
  162. digitalWrite(LED_R, HIGH);
  163. tone(buzzer, 900);
  164. delay(600);
  165. tone(buzzer, 400);
  166. delay(500);
  167. noTone(buzzer);
  168. delay(900);
  169. digitalWrite(LED_R, LOW);
  170. }
  171. entering = false; // Stop the user from entering
  172. for(int i=0; i<4; i++)input[i] = 0; // Reset the user input variable
  173. display.show("0000"); // Reset the display
  174. }else{ // If the incremented digit's index hasn't reached the last one
  175. digitalWrite(LED_R, HIGH); // Blink the red LED
  176. delay(200);
  177. digitalWrite(LED_R, LOW);
  178. }
  179. }
  180. if(digitalRead(BTN_R))lock_r = false; // If the button has been released, unlock the lock
  181. }
  182. }else{
  183. lock_r = false; // If the button is not pressed, make sure it's unlocked
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement