Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. //These are the states
  4. #define S_START 0
  5. #define S_BEGIN 1
  6. #define S_LOCK1 2
  7. #define S_LOCK2 3
  8. #define S_LOCK3 4
  9. #define S_UNLOCK 5
  10. #define S_SET 6
  11.  
  12. //button debounce
  13. #define DEBOUNCE 150
  14.  
  15. //The servo will act as the "lock" of the safe
  16. Servo unlockServo;
  17.  
  18. /* Password/Button key
  19. 0 - top
  20. 1 - left
  21. 2 - bottom
  22. 3 - right
  23. 4 - far right (not usable on password)
  24. -1 - no button (not usable on password)
  25. */
  26.  
  27. int State = S_START; //the default state is START
  28. int buttonval = 0; //used by loop_update to find button value
  29. int password[] = {2, 0, 3}; //the safe's password
  30. int next = 0; //variable used by loop update
  31. int count = 0; //variable used to keep track of password set
  32. int next_set = 0; //variable used to update password
  33.  
  34. void setup() {
  35. Serial.begin(9600);
  36. unlockServo.attach(10);
  37. }
  38.  
  39. void loop() {
  40. next = loop_update();
  41.  
  42. switch (State) {
  43. case S_START:
  44. //make sure the "lock" is in lock position and no tones are playing
  45. unlockServo.write(0);
  46. noTone(8);
  47. //if the light sensor is reading less than 100, we're good to start
  48. if (analogRead(A1) < 100) { State = S_BEGIN; }
  49. break;
  50.  
  51. case S_BEGIN:
  52. if (next == 4) {
  53. //if the far right button is being pressed, enter setup mode.
  54. State = S_SET;
  55. }
  56. else {
  57. if (next != -1) {
  58. //if another button is being pressed and that button actually exists (not -1) start the lock combination
  59. State = S_LOCK1;
  60. }
  61. }
  62. break;
  63.  
  64. case S_SET:
  65. count = 0;
  66. //reset the count and start the loop
  67. //pressing the far right button will go back to the BEGIN state, but will also keep any password changes
  68. //count < 3 is the limiter because the password is 3 digits long
  69. while (next_set != 4 && count < 3) {
  70. noTone(8);
  71. next_set = loop_update();
  72. if (next_set != 4 && count < 3) {
  73. if (next_set != -1) {
  74. password[count] = next_set;
  75. tone(8, 300); // play a short tone to indicate you updated something
  76. count = count + 1;
  77. }
  78. }
  79. }
  80. State = S_BEGIN; //go back to the BEGIN state when finished
  81. break;
  82.  
  83. case S_LOCK1:
  84. if (next == password[0]) { //if the button pressed equals the password's respective digit go to the next lock state & play a short tone to indicate you were correct
  85. tone(8, 200);
  86. State = S_LOCK2;
  87. }
  88. else {
  89. if (next != -1) { //if the button pressed was incorrect (and a button was actually pressed) go back to the begin state and play a short tone to indicate you were wrong
  90. tone(8, 100);
  91. State = S_BEGIN;
  92. }
  93. }
  94. break;
  95.  
  96. case S_LOCK2:
  97. noTone(8); //resets the previously played success tone
  98. if (next == password[1]) {
  99. tone(8, 200);
  100. State = S_LOCK3;
  101. }
  102. else {
  103. if (next != -1) {
  104. tone(8, 100);
  105. State = S_BEGIN;
  106. }
  107. }
  108. break;
  109.  
  110. case S_LOCK3:
  111. noTone(8);
  112. if (next == password[2]) {
  113. tone(8, 200);
  114. State = S_UNLOCK; //by now all 3 digits are correct, so go to the unlock state
  115. }
  116. else {
  117. if (next != -1) {
  118. tone(8, 100);
  119. State = S_BEGIN;
  120. }
  121. }
  122. break;
  123.  
  124. case S_UNLOCK:
  125. noTone(8);
  126. unlockServo.write(180); //switch the servo to unlock position
  127.  
  128. if (next == 4) { //if the far left button is pressed, go back to the begin state, therefore locking the safe again
  129. State = S_BEGIN;
  130. }
  131. break;
  132. }
  133. }
  134.  
  135. int loop_update() {
  136. buttonval = analogRead(A2);
  137. //Top
  138. if (buttonval >= 100 and buttonval < 200) {
  139. delay(DEBOUNCE);
  140. return 0;
  141. }
  142. // Left
  143. else if (buttonval >= 0 and buttonval <= 60) {
  144. delay(DEBOUNCE);
  145. return 1;
  146. }
  147. //Bottom
  148. else if (buttonval >= 300 and buttonval < 400) {
  149. delay(DEBOUNCE);
  150. return 2;
  151. }
  152. //Right
  153. else if (buttonval > 450 and buttonval < 580) {
  154. delay(DEBOUNCE);
  155. return 3;
  156. }
  157. //Far Right
  158. else if (buttonval > 650 and buttonval < 800) {
  159. delay(DEBOUNCE);
  160. return 4;
  161. }
  162. //nothing pressed
  163. else return -1;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement