Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // How to Make an Arduino Keypad Lock
- // Jlaservideo.com
- #include <Servo.h>
- #include <Keypad.h>
- Servo ServoMotor;
- char* password = "158"; // change the password here, just pick any 3 numbers
- int position = 0;
- long x = 0;
- int rot = 180;
- const byte ROWS = 4;
- const byte COLS = 4;
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- byte rowPins[ROWS] = { 8, 7, 6, 9 };
- byte colPins[COLS] = { 5, 4, 3, 2 };
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- int RedpinLock = 12;
- int GreenpinUnlock = 13;
- void setup()
- {
- Serial.begin(9600);
- pinMode(RedpinLock, OUTPUT);
- pinMode(GreenpinUnlock, OUTPUT);
- ServoMotor.attach(11);
- LockedPosition(true);
- }
- void loop()
- {
- char key = keypad.getKey();
- if(key) {
- Serial.println(key);
- }
- if (key == '*' || key == '#')
- {
- position = 0;
- LockedPosition(true);
- }
- if (key == password[position])
- {
- position ++;
- }
- if (position > 3)
- {
- LockedPosition(false);
- }
- else
- {
- Serial.println(position);
- x++;
- if(x > 100000)
- {
- x = 0;
- }
- spin(x);
- }
- }
- void spin(long i){
- if(i == 0)
- {
- ServoMotor.write(rot);
- if(rot == 180)
- {
- rot = 0;
- }
- else{
- rot = 360;
- }
- }
- }
- void LockedPosition(int locked)
- {
- if (locked)
- {
- digitalWrite(RedpinLock, HIGH);
- digitalWrite(GreenpinUnlock, LOW);
- ServoMotor.write(11);
- }
- else
- {
- digitalWrite(RedpinLock, LOW);
- digitalWrite(GreenpinUnlock, HIGH);
- ServoMotor.write(0);
- delay(1200);
- ServoMotor.write(180);
- delay(800);
- ServoMotor.write(0);
- delay(800);
- ServoMotor.write(90);
- delay(800);
- ServoMotor.write(135);
- delay(800);
- ServoMotor.write(30);
- delay(800);
- ServoMotor.write(0);
- delay(800);
- delay(3000);
- delay(3000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement