print("Door Lock System")
print("Created by : MUHAMMAD AQIL HAKIM BIN MOHD FARID")
print("Date : 1/5/2024")
# Import libraries/modules
from machine import Pin, PWM
from utime import sleep
import Oled # oled Library
import Ultsonic # ultsonic Library
import Servomotor
from machine import Pin, SoftI2C
# Pin Declaration
PIR_Sensor = Pin(27, Pin.IN)
RedLED_pin = Pin(14, Pin.OUT)
GreenLED_pin = Pin(23, Pin.OUT)
Buzz_pin = PWM(Pin(4, Pin.OUT))
oled_Pin = SoftI2C(scl=Pin(22), sda=Pin(21))
Trig = Pin(13)
Echo = Pin(12)
Servo1 = Pin(18, Pin.OUT)
Servo2 = Pin(5, Pin.OUT)
Button1 = Pin(26, Pin.IN)
ButtonShut = Pin(25, Pin.IN)
# Parameter Declaration
# create an Object name module with library
# Object name = Libabry name.class name()
screen = Oled.SSD1306_I2C(width=128, height=64, i2c=oled_Pin)
sonic = Ultsonic.HCSR04(trigger_pin=13, echo_pin=12, echo_timeout_us=500 * 2 * 30)
Lock = Servomotor.Servo(pin=Servo1)
SafeLock = Servomotor.Servo(pin=Servo2)
# Main Program
paused = False
distance_in_mm = sonic.distance_mm()
Buzz_pin.init(freq=1500, duty=0)
while True:
distance_in_cm = sonic.distance_cm()
print("------------------")
print("Distance Door Lock System", distance_in_cm, "cm")
motion_status = PIR_Sensor.value() # .value -> digital value / .read -> analog value(any value)
print("The motion status is", motion_status)
if motion_status == True:
for a in range(1):
RedLED_pin.on()
GreenLED_pin.off()
Buzz_pin.init(freq=1500, duty=100)
sleep(0.5)
RedLED_pin.off()
Buzz_pin.init(freq=1500, duty=0)
sleep(0.5)
screen.fill(0)
screen.text("Alert!!!!", 5, 10, 1) # note (row,column,colorcode)
screen.text("~Aqil~", 15, 25, 1)
screen.text("~XEON~", 60, 50, 1)
screen.show()
Lock.move(angle=180)
sleep(0.5)
if distance_in_cm < 60:
for i in range(10):
Buzz_pin.init(freq=500, duty=50)
RedLED_pin.on()
sleep(1)
screen.fill(0)
screen.text("Someone is", 5, 10, 1) # note (row,column,colorcode)
screen.text("Very Close!!!", 15, 25, 1)
screen.show()
Buzz_pin.init(freq=500, duty=0)
RedLED_pin.off()
sleep(1)
screen.fill(0)
screen.text("Someone is", 5, 10, 1) # note (row,column,colorcode)
screen.text("Very Close!!!", 15, 25, 1)
screen.show()
SafeLock.move(angle=0)
GreenLED_pin.off()
else:
GreenLED_pin.on()
RedLED_pin.off()
screen.fill(0)
screen.text("Save", 5, 10, 1) # note (row,column,colorcode)
screen.text("~Aqil~", 15, 25, 1)
screen.text("~XEON~", 60, 50, 1)
screen.show()
if Button1.value() == 0:
SafeLock.move(angle=90)
Lock.move(angle=90)
screen.text("~Unlock~", 15, 25, 1)
screen.text("~XEON~", 60, 50, 1)
else:
SafeLock.move(angle=0)
Lock.move(angle=180)
screen.text("~Lock~", 15, 25, 1)
screen.text("~XEON~", 60, 50, 1)
sleep(0.5) # system delay