from machine import I2C, Pin,PWM
from time import sleep
from pico_i2c_lcd import I2cLcd
# cofiguraitng pins
# -----------------for servo motro ------------------------
servo = PWM(Pin(2))
servo.freq(50)
#----------------------------------------------------------
# -----------------for i2c lcd pannel ---------------------
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
#----------------------------------------------------------
actual_password='' #variable for storing passcode
#<<<<<<<<<<<<<<<<<<<< funcitons >>>>>>>>>>>>>>>>>>>>>>>>>
#-------------- algorithm for reading keys ---------------
ROWS = [28,27,26,22] # GPIO pins for rows21
COLS = [21,20,19,18] # GPIO pins for columns 20
# Keypad layout
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Initialize row pins as inputs with pull-up resistors
for row_pin in ROWS:
Pin(row_pin, Pin.IN, Pin.PULL_UP)
# Initialize column pins as outputs
for col_pin in COLS:
Pin(col_pin, Pin.OUT)
# Function to read key press from keypad
def read_key():
for col_num, col_pin in enumerate(COLS):
Pin(col_pin, Pin.OUT, value=0) # Set column pin low
for row_num, row_pin in enumerate(ROWS):
if not Pin(row_pin).value(): # If row pin is low
while not Pin(row_pin).value(): # Wait for key release
pass
return keys[row_num][col_num]
Pin(col_pin, Pin.IN) # Restore column pin to input
return None # No key pressed
#------------------------------------------------------------------
#-------------- functions for operating servo motor ---------------
def angle (d,f):
d1 = ((d * 65)/1.8) + 1500
f1 = ((f * 65)/1.8) + 1500
servo.duty_u16(int(d1))
utime.sleep(1)
servo.duty_u16(int(f1))
utime.sleep(1)
#time.sleep(1)
#time.sleep(1)
def servo_lock():
# locks the door by giving signal to the servomotor
#servo1(pwm, 180)
angle(0,180)
def servo_unlock():
# unlocks the door by giving signal to the servomotor
#servo1(pwm, 10)
angle(180,0)
#------------------------------------------------------------------
#------------- functions for displaying on lcd panel --------------
def show_one_line(mes):
# this funciton takes a string as input
# makes the string to be shown on the lcd displays first line
"""
-----------------------------------------
| |
| display message in lcs's first line |
| |
| |
| |
-----------------------------------------
"""
lcd.clear()
lcd.move_to(0,0)
lcd.putstr(mes)
def show_two_line(mes1,mes2,x1=0,x2=0):
# this funciton takes a two string as input
# and another two varialbe x1, x2 for the
# postion of cursor on line 1 and line 2
# makes the string to be shown on the lcd display
"""
-----------------------------------------
| |
| display message in lcs's first line |
| |
| display message in lcs's second line |
| |
-----------------------------------------
"""
lcd.clear()
lcd.move_to(x1,0)
lcd.putstr(mes1)
lcd.move_to(x2,1)
lcd.putstr(mes2)
def show_lcd_scroll(stng,y=0):
# this funtion takes a string and Y POSITION and display it as revaling chars
"""
-----------------------------------------
| |
| display's str one by one revealing |
| |
| |
| |
-----------------------------------------
"""
for i in range(len(stng)):
lcd.move_to(i,y)
lcd.putstr(stng[i])
sleep(0.1)
def start_up_message():
# this function creates startup templates for lcd screen
"""
-----------------------------------------
| |
| WELCOME !! |
| |
| |
| |
-----------------------------------------
"""
lcd.clear()
show_lcd_scroll(' WELCOME !!')
sleep(1)
"""
-----------------------------------------
| |
| ELECTRONIC DOOR |
| |
| LOCK !! |
| |
-----------------------------------------
"""
lcd.clear()
show_lcd_scroll('ELECTRONIC DOOR')
show_lcd_scroll(' LOCK !!',1)
sleep(1)
"""
-----------------------------------------
| |
| INITIALIZING.... |
| |
| SETUP............................ |
| |
-----------------------------------------
"""
lcd.clear()
show_lcd_scroll('INITIALIZING...')
show_lcd_scroll('SETUP...........',1)
"""
-----------------------------------------
| |
| SETUP.. |
| |
| SUCESSFUL................. |
| |
-----------------------------------------
"""
lcd.clear()
show_lcd_scroll('SETUP..')
show_lcd_scroll('SUCCESSFUL....',1)
def show_home_screen():
# this functions crates home screen template for lcd display
"""
-----------------------------------------
| |
| # to unlcok door |
| |
| A to change passcode |
| |
-----------------------------------------
"""
show_two_line('# to unlock door',' A to New code')
def show_status_screen(mess,time=0.1):
# this funciton takes a string and time to display on lcd
# this will create a proccesing status on lcd pannel
"""
-----------------------------------------
| |
| display message in lcs's first line |
| |
| [================..............] |
| |
-----------------------------------------
"""
show_two_line(mess," [............] ")
# this for loop is responsible for creating processing animtion on lcd pannle
for i in range(2,14):
lcd.move_to(i,1)
lcd.putstr( "=" )
sleep(time)
def show_tolock_screen():
# this funciton creates to lock screen template for lcd display
"""
-----------------------------------------
| |
| # to lock door |
| |
| |
| |
-----------------------------------------
"""
show_one_line(' # to lock door')
def show_passcode_screen():
# this funciton creates enter passcode screen template for lcd displa
"""
-----------------------------------------
| |
| Enter Your Code : |
| |
| [ _ _ _ _ ] |
| |
-----------------------------------------
"""
show_two_line('Enter Your code: ','[____]',0,5)
def show_Access_denied():
# this function creates a access denied screen template
"""
-----------------------------------------
| |
| Access denied !! |
| |
| [================..............] |
| |
-----------------------------------------
"""
show_two_line('code mismatch !!','Access denied !!')
sleep(1)
show_status_screen('Access Denied !!',0.8)
show_one_line(' Door locked!!')
sleep(1)
#----------------------------------------------------------------------
#-------------------- algorithms for operation project -------------------
def initial_setup():
# This function runs only once at intial condition
show_tolock_screen() # display meassage on lcd screen
while True:
key= read_key() # read keys in keypad
if key=='#':
if change_passcode(): # changes the passcode
# if passcode change sucssefully locks the door
lock__door() # locks door
return
else:
# else show error and return to again intial setup
show_two_line('code mismatch !!','door not locked!') # display's message on lcd screen
sleep(1)
show_tolock_screen() # show tolock_screen template on lcd panel
def get_passcode():
# this function gets passcode and returns the entered passcode
entered_passcode='' # temparay variable to store entereed pass code
while len(entered_passcode)!=4: # this ensures auto return when passcode reached 4 digits
key=read_key() # reads key form keypad
if key:
# if key pressed it add key value to entered passcode
entered_passcode+=key
#-------------- shows animation on lcd display-------------------
lcd.move_to(5+(len(entered_passcode)),1)
lcd.putchar(entered_passcode[-1])
sleep(0.1)
lcd.move_to(5+(len(entered_passcode)),1)
lcd.putchar('*')
#------------------------------------------------------------------
return entered_passcode
def change_passcode():
# this function is responisble for changing passcode
global actual_password # it ensures to change the data in actual_password
show_two_line("Enter New code:",'[____]',0,5) # display's enter code template on lcd panel
actual_password=get_passcode() # takes new passcode and stores in variable
show_two_line('Confirm New Code','[____]',0,5) # display's confirm code template on lcd panel
sleep(0.5)
flag=actual_password==get_passcode() # this line verifies the first entered passcode and confirmed passcode
show_status_screen('Authenticating !',0.07) # display's authenticating ! template on lcd panel
return flag
def lock__door():
# this fucntion locks the door and display's message on lcd screen
show_one_line(' code matched !!') # display's message on lcd screen
sleep(0.5)
show_status_screen(' LOCKING !!') # display's message on lcd screen
servo_lock() # lock the door using servo motor
show_two_line(' DOOR'," LOCKED !!" ) # display's message on lcd screen
sleep(1)
return
def lock_door():
# this function is different fromm lock__door()
# this function is called when door is unlocked
# and ensures to be locked again
show_tolock_screen() # display's message on lcd screen
while True:
key=read_key() # read keys pressed on key pad
if key=='#':
servo_lock() # locks the door using servo motor
# display's message on lcd screen
show_two_line(' DOOR'," LOCKED !!" )
sleep(1)
show_home_screen() # shows home screen template on lcd screen
return
def unlock_door():
show_one_line('code matched !!') # display's message on lcd screen
sleep(0.5)
show_status_screen(' UNLOCKING !!') # display's message on lcd screen
servo_unlock() # unlocks the door using servomotor
show_two_line(' DOOR'," UNLOCKED !!" ) # display's message on lcd screen
sleep(1)
lock_door() # this funciton is called to ensure the door to be locked again
def Authenticate_passcode():
# this funciton authenticates passcode
passcode=get_passcode() # gets pascode entered in keypad
show_status_screen('Authenticating !',0.07) # display's message on lcd screen
return passcode==actual_password # returns True or false according to the authentication
def main():
# this is main function responsible for operation
show_home_screen() # display's message on lcd screen
while True:
key=read_key() # gets keys entered in keypad
if key=='#':
show_passcode_screen() # display's message on lcd screen
if Authenticate_passcode(): # authenticates passcode
unlock_door() # unlocks the door
else:
# if passcode doesn't match
show_Access_denied() # display's message on lcd screen
show_home_screen() # display's home screen on lcd panel
elif key=='A':
# when A is pressed, it requests to change passcode
if change_passcode(): # gets new pass code and authenticates the passcode
# if True
unlock_door() # unlocks the door
else:
# if authentication failed
show_Access_denied() # display's message on lcd screen
show_home_screen() # display's home screen on lcd panle
if __name__ == "__main__":
start_up_message()
initial_setup()
main()