print('Automatic Parking Monitoring System Using ESP32,SG90 Servo Motor, HC-05 Ultrasonic Sensor, Buzzer, LED & Favoriot Platform\n')
#Import all necessary libraries
import Ultrasonic #This is Ultrasonic Library (TAK PERLU .PY)
import Servo
import ssd1306 #This is OLED Library
from machine import Pin, PWM, SoftI2C
from utime import sleep
#Declare Pin
Trig = Pin(25, Pin.IN) #Ultrasonic
Echo = Pin(27, Pin.OUT) #Ultrasonic
led_red = Pin(2, Pin.OUT)
servo = Pin(14, Pin.OUT)
buzzer = PWM(Pin(26), Pin.OUT)
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
#Create the object name for sensor with library --> LIBRARYNAME.CLASSNAME
#bagi nama = Import Library Name.Class Name (trigger_pin, echo_pin,)
sonic = Ultrasonic.HCSR04(trigger_pin=Trig, echo_pin=Echo)
gate = Servo.Servo(pin=servo)
screen = ssd1306.SSD1306_I2C(width=oled_width, height=oled_height, i2c=i2c_oled) #library name.class name()
#Main Program
while True:
#Initial Condition
#ULTRASONIC PART
led_red.on()
sleep(.2)
print("===== SILA PASTIKAN KENDERAAN ANDA BERADA DIDALAM PETAK ====")
#parameter(create your own name) = bagi nama.function(define)
distance_in_cm = sonic.distance_cm()
#print,parameter(Create your own name)
print(" Ketinggian anda dalam CM : ", distance_in_cm, "cm")
if distance_in_cm <=100: #Palang bukak (kereta datang)
gate.move(90)
sleep(1)
gate.move(0)
sleep(5)
buzzer.init(freq=1000, duty=1023) #(adjust bunyi,adjust duration0-1023)
sleep(0.5)
buzzer.init(freq=1, duty=10)
sleep(0.5)
gate.move(90)
sleep(1)
#for i in range (10):
else:
gate.move(90)
for i in range (10):
buzzer.init(freq=1000, duty=200) #(adjust bunyi,adjust duration)
sleep(0.1)
buzzer.init(freq=1, duty=10)
sleep(0.1)
led_red.off()
sleep(.2)
# SERVO MOTOR PART
#gate.move(0)
#sleep(1)
#sleep(1)
#gate.move(0)
#sleep(1)
sleep(2) #In every 2 second, ultrasonic will sense new incoming distance