#Smart Parking System Using Micropython ESP32
#Created by Alif Iman
#5/12/2023
from machine import Pin, PWM, SoftI2C
from utime import sleep, sleep_ms
import oled
import time
#Declare LED & Buzzer Pin
led_green = Pin(12, Pin.OUT)
led_red = Pin(26, Pin.OUT)
buzzer = PWM(Pin(14), Pin.OUT) # Assuming pin 14 for the buzzer
#Pin Declaration for oled
Pin_scl_sda = SoftI2C(scl=Pin(22), sda=Pin(23))
#create an object name using opp (object oriented programming)
# library nama.class name
skrin = oled.SSD1306_I2C(width=128, height=64, i2c=Pin_scl_sda)
# initialize the PIR sensor on pin 27, LED on pin 12, buzzer on pin 14, and servo motor on pin 21
pir_sensor = Pin(27, Pin.IN)
sg90 = PWM(Pin(21, mode=Pin.OUT)) # SERVO MOTOR = TOLL BAR
sg90.freq(50)
# initialize the PIR sensor on pin 27, LED on pin 12, buzzer on pin 14, and servo motor on pin 21
sg901 = PWM(Pin(19, mode=Pin.OUT)) # SERVO MOTOR = TOLL BAR
sg901.freq(50)
# loop forever
while True:
led_red.on() #during the toll bar is closed
led_green.off() #during the toll bar is open
# check if motion is detected by the PIR sensor
if pir_sensor.value() == 1:
skrin.fill(1)
skrin.text("HI",0,20,0)
skrin.text("CAR DETECTED",0,30,0)
skrin.text("YOU WELCOME",0,40,0)
skrin.show() #a must
for i in range (2):
buzzer.init(freq=1500, duty = 400)
sleep(0.5)
buzzer.init(freq=1, duty = 0)
sleep(0.5)
led_red.on()
#Move the servo motor to 180 degrees
led_green.on()
led_red.off()
sg90.duty_u16(1638)
sg901.duty_u16(7864)
sleep(1)
#Wait for 2 seconds
sleep(2)
#Move the servo motor back to 0 degrees
skrin.fill (1)
skrin.text("YEAYY",0,20,0)
skrin.text("YOU ARE READY",0,30,0)
skrin.text("TO GO",0,40,0)
skrin.text("PASS",0,50,0)
skrin.show()
sg90.duty_u16(4860)
sg901.duty_u16(4860)
sleep(1)
led_green.on()
led_red.off()
sleep(1)
# wait for a short time to reduce CPU usage
sleep_ms(4)