print("\nSmart & Safety Hostel")
print("Practical Test")
print("Created by : Muhammad Faiz")
print("Date: 2/4/2024\n")
# Import Libraries/modules
from machine import Pin
from utime import sleep
from machine import PWM
import Oled_Library # This is Oled Library
from machine import SoftI2C
from machine import PWM
import Servo_Library
# Pin Declaration
PIR_pin = Pin(12, Pin.IN)
Red_LED_pin = Pin(5, Pin.OUT)
Green_LED_pin = Pin(18, Pin.OUT)
Buzzer_pin = PWM(Pin(2, Pin.OUT))
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
ROW_PINS = [35, 32, 33, 25]
COL_PINS = [26, 27, 14, 15]
servo_pin = (Pin(4, Pin.OUT))
Yellow_LED_pin = Pin(23, Pin.OUT)
# Create an OBJECT name for module with Library
# OBJECT name --> Library name.class name()
skrin = Oled_Library.SSD1306_I2C(width=128, height=64, i2c=oled_pin)
kunci = Servo_Library.Servo(pin=servo_pin)
# Keypad configuration
# Define the keypad matrix
KEYS = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
# Initialize keypad rows and columns as input with pull-up
row_pins = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in ROW_PINS]
col_pins = [Pin(pin, Pin.OUT) for pin in COL_PINS]
# Function to read the keypad
def read_keypad():
code = ''
while True:
for col_num, col_pin in enumerate(col_pins):
col_pin.value(0)
for row_num, row_pin in enumerate(row_pins):
if not row_pin.value():
key = KEYS[row_num][col_num]
if key == '#': # Check if the entered code is complete
return code
elif key == '*': # Clear the entered code
code = ''
else:
code += key
sleep(0.2) # Debounce delay
col_pin.value(1)
# Main Program
while True:
Red_LED_pin.off() # Initialize
Green_LED_pin.off() # Initialize
Buzzer_pin.duty(0) # Initialize
motion_status = PIR_pin.value() # .value --> read digital value (1 or 0)
print("The motion status is ", motion_status)
if motion_status == True:
for a in range(6):
skrin.fill(0)
skrin.text("Passcode?", 5, 30, 1)
skrin.show()
Red_LED_pin.on()
Buzzer_pin.freq(500)
Buzzer_pin.duty(50)
sleep(0.5)
Buzzer_pin.duty(0)
sleep(0.5)
Green_LED_pin.off()
# Check for keypad input to turn off the buzzer
password = "1234" # Define your password here
entered_code = read_keypad()
if entered_code == password:
print ("passcode correct, buzer off")
skrin.fill(0)
skrin.text("Welcome!", 5, 30, 1)
skrin.show()
Red_LED_pin.off()
Green_LED_pin.on()
kunci.move(0)
Yellow_LED_pin.on()
sleep(6)
else:
Green_LED_pin.off()
Red_LED_pin.on()
skrin.fill(0)
skrin.text("Hello!", 5, 30, 1)
skrin.show()
kunci.move(90)
Yellow_LED_pin.off()
sleep(5)Loading
esp32-devkit-v1
esp32-devkit-v1