Advertisement
Guest User

Arduino Safe code

a guest
Nov 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <OneButton.h>
  3.  
  4. Servo s1;
  5. OneButton button(A0, true);
  6. int pot = A4; //potentiometer
  7. int val; //code variable
  8. int closed =97; //Servo position when closed
  9. int opened = 125; //Servo position when opened
  10.  
  11. void setup()
  12. {
  13. pinMode(pot, INPUT);
  14.  
  15. Serial.begin(9600); //optional
  16.  
  17. button.attachDoubleClick(doubleclick); //Variable to close the box with a double click
  18.  
  19. s1.attach(3); //Servo Pin
  20. s1.write(closed); //Locks the box when it gets power
  21.  
  22. delay(1000); //delay to make sure the servo had enough time to move
  23. }
  24.  
  25. void loop()
  26. {
  27. button.tick(); //variable to check for the double click button
  28. int pStatus = analogRead(pot); //variable to check the potentiometer value
  29. pStatus = map(pStatus, 1, 1024, 1, 4); //mapping the potentiometer from 1 to 4. This means I have a total of 3 different digits which I can enter. This number can be increased as much as you want
  30.  
  31. if(pStatus == 1 && val == 0) //when the potentiometer is on position 1 and the value (for the code) is zero 1 is added to the value
  32. {
  33. val ++;
  34. }
  35. if(pStatus == 3 && val == 1)
  36. {
  37. val ++;
  38. }
  39. if(pStatus == 1 && val == 2)
  40. {
  41. val = 0;
  42. }
  43. if(pStatus == 2 && val == 2)
  44. {
  45. val++;
  46. }
  47. if(pStatus == 1 && val == 3) //safety point. Without this the potentiometer could just be turned from one to the other side a few times and the save would open anyways
  48. {
  49. val = 0;
  50. }
  51. if(pStatus == 3 && val == 3)
  52. {
  53. val++;
  54. }
  55. if(pStatus == 1 && val == 4) //when this point is reached the safe opens
  56. {
  57. s1.write(opened);
  58. }
  59. else //to ensure the safe stays closed and closes again when double click is performed
  60. {
  61. s1.write(closed);
  62. }
  63. Serial.println(val); //optional
  64. }
  65.  
  66. void doubleclick() //void for the double click
  67. {
  68. val = 0; //when the double click is performed the code value is set to 0 again, meaning the safe closes.
  69. }
  70.  
  71. //PROBLEMS / ISSUES
  72. //With this few code digits the save is still easy to acces trough trying.
  73. //Because of this and the fact that there is only one "safety point" the save ironicly isn't that safe.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement