Advertisement
Guest User

change

a guest
Nov 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. // How to Make an Arduino Keypad Lock
  2. // Jlaservideo.com
  3.  
  4. #include <Servo.h>
  5. #include <Keypad.h>
  6.  
  7. Servo ServoMotor;
  8. char* password = "158";  // change the password here, just pick any 3 numbers
  9. int position = 0;
  10. long x = 0;
  11. int rot = 180;
  12. const byte ROWS = 4;
  13. const byte COLS = 4;
  14. char keys[ROWS][COLS] = {
  15. {'1','2','3','A'},
  16. {'4','5','6','B'},
  17. {'7','8','9','C'},
  18. {'*','0','#','D'}
  19. };
  20.  
  21. byte rowPins[ROWS] = { 8, 7, 6, 9 };
  22. byte colPins[COLS] = { 5, 4, 3, 2 };
  23. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  24. int RedpinLock = 12;
  25. int GreenpinUnlock = 13;
  26.  
  27. void setup()
  28. {
  29. Serial.begin(9600);
  30. pinMode(RedpinLock, OUTPUT);
  31. pinMode(GreenpinUnlock, OUTPUT);
  32. ServoMotor.attach(11);
  33. LockedPosition(true);
  34. }
  35.  
  36. void loop()
  37. {
  38.   char key = keypad.getKey();
  39.   if(key) {
  40.     Serial.println(key);
  41.   }
  42.   if (key == '*' || key == '#')
  43.   {
  44.     position = 0;
  45.     LockedPosition(true);
  46.   }
  47.   if (key == password[position])
  48.   {
  49.     position ++;
  50.   }
  51.   if (position > 3)
  52.   {
  53.     LockedPosition(false);
  54.   }
  55.   else
  56.   {
  57.     Serial.println(position);
  58.     x++;
  59.     if(x > 100000)
  60.     {
  61.       x = 0;
  62.     }
  63.     spin(x);
  64.   }
  65. }
  66.  
  67. void spin(long i){
  68.   if(i == 0)
  69.   {
  70.     ServoMotor.write(rot);
  71.     if(rot == 180)
  72.     {
  73.       rot = 0;
  74.     }
  75.     else{
  76.       rot = 360;
  77.     }
  78.   }
  79. }
  80. void LockedPosition(int locked)
  81. {
  82. if (locked)
  83. {
  84. digitalWrite(RedpinLock, HIGH);
  85. digitalWrite(GreenpinUnlock, LOW);
  86. ServoMotor.write(11);
  87. }
  88. else
  89. {
  90. digitalWrite(RedpinLock, LOW);
  91. digitalWrite(GreenpinUnlock, HIGH);
  92. ServoMotor.write(0);
  93. delay(1200);
  94. ServoMotor.write(180);
  95. delay(800);
  96. ServoMotor.write(0);
  97. delay(800);
  98. ServoMotor.write(90);
  99. delay(800);
  100. ServoMotor.write(135);
  101. delay(800);
  102. ServoMotor.write(30);
  103. delay(800);
  104. ServoMotor.write(0);
  105. delay(800);
  106. delay(3000);
  107. delay(3000);
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement