# this program is to create an automated sliding door using PIR sensor,
#Ultrasonic HC-SR04 sensor, ESP 32.
#MUHAMMAD BUKHARI BIN BADRUL HISHAM
#51225122052
print('this automated sliding door uses PIR sensor, Ultrasonic and ESP 32')
#import from library
from machine import Pin, PWM, SoftI2C
from time import sleep
import machine
import oled_lib
#Declare pin for ESP 32
#pin for buzzer
buzzer = PWM(Pin(26),Pin.OUT)
#pin for PIR sensor
pir= Pin(34,Pin.IN)
#pin for servo motor
servo=machine.PWM(Pin(27, Pin.OUT),freq=50)
#pin for oled
oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = oled_lib.SSD1306_I2C(oled_width, oled_height, oled)
motion=False
#buat function dipanggil handle_interupt
#this function will be called every time when motion is detected
def handle_interupt(pin): #def ni variable yang boleh ubah def is a function
global motion
motion=True
global interrupt_pin
interrupt_pin=pin
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interupt)
#Function to move servo motor to a certain angle
def move_servo(angle):
duty = int(40 + (angle / 180) * 90) # calculate duty cycle for given angle
servo.duty(duty)
while True:
if motion==True:
print('Welcome to Student Lounge')
move_servo(90) # move servo to 90 degree position
buzzer.init(freq=1000,duty=512)
sleep(0.1)
buzzer.init(freq=1,duty=0)
sleep(0.1)
buzzer.init(freq=500,duty=256)
sleep(0.1)
buzzer.init(freq=1,duty=0)
sleep(0.1)
oled.fill(0)
oled.text('###########',12,0,1) #x-axis,y-axis,color font
oled.text('# WELCOME #',12,10,1)
oled.text('# TO #',12,20,1)
oled.text('# STUDENT #',12,30,1)
oled.text('# LOUNGE #',12,40,1)
oled.text('###########',12,50,1)
oled.show()
sleep (7)
motion=False
move_servo(0) # move servo to 0 degree position
else:
print ('############################')
print ('# #')
print ('# SALAM AIDILFITRI #')
print ('# MAAF ZAHIR #')
print ('# DAN BATIN #')
print ('# #')
print ('############################')
move_servo(0) # move servo to 0 degree position
oled.fill(0)
oled.text ('##############',5,10,1)
oled.text ('# SALAM #',5,20,1)
oled.text ('# AIDILFITRI #',5,30,1)
oled.text ('# MAAF ZAHIR #',5,40,1)
oled.text ('# DAN BATIN #',5,50,1)
oled.text ('##############',5,60,1)
oled.show()
sleep (1)