Devices & Components
Arduino Uno Rev3
Jumper Wires
3x4 Keypad
Screws
3.7 volt Battery
Car door lock actuator
RGB LED
Plastic Tie
L298n Motor Driver
12 volt Battery Holder
Project description
Code
Code
arduino
1#include <Keypad.h> 2 3int lock_pin = A0; 4int unlock_pin = A1; 5int green_led = 11; 6int blue_led = 12; 7int red_led = 13; 8int j = 0; 9 10const int ROW_NUM = 4; //four rows 11const int COLUMN_NUM = 3; //three columns 12 13char keys[ROW_NUM][COLUMN_NUM] = { 14 {'1', '2', '3'}, 15 {'4', '5', '6'}, 16 {'7', '8', '9'}, 17 {'*', '0', '#'} 18}; 19 20byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad 21byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad 22 23Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); 24 25const String password = "432"; // change your password here 26String input_password; 27 28void setup() { 29 30 pinMode(lock_pin, OUTPUT); 31 pinMode(unlock_pin, OUTPUT); 32 pinMode(green_led, OUTPUT); 33 pinMode(blue_led, OUTPUT); 34 pinMode(red_led, OUTPUT); 35 36 Serial.begin(9600); 37 input_password.reserve(32); // maximum input characters is 33, change if needed 38} 39 40void loop() { 41 char key = keypad.getKey(); 42 43 if (key) { 44 Serial.println(key); 45 46 if (key == '*') { 47 input_password = ""; // clear input password 48 } else if (key == '#') { 49 if (password == input_password) { 50 Serial.println("password is correct"); 51Serial.println(input_password); 52 if (j == 0) 53 { 54 digitalWrite(lock_pin, HIGH); 55 digitalWrite(green_led, HIGH); 56 delay(1000); 57 digitalWrite(lock_pin, LOW); 58 digitalWrite(green_led, LOW); 59 j = 1; 60 } 61 else if (j == 1) 62 { 63 digitalWrite(unlock_pin, HIGH); 64 digitalWrite(blue_led, HIGH); 65 delay(500); 66 digitalWrite(unlock_pin, LOW); 67 digitalWrite(blue_led, LOW); 68 j = 0; 69 } 70 71 } else { 72 Serial.println("password is incorrect, try again"); 73 digitalWrite(red_led, HIGH); //..the green LED not.. 74 delay(500); 75 digitalWrite(red_led, LOW); 76 } 77 78 input_password = ""; // clear input password 79 } else { 80 input_password += key; // append new character to input password string 81 } 82 } 83} 84
Comments
Only logged in users can leave comments