print('This program integrate ESP32 + Ultrasonic')
#Import related libraries/modules
import ultrasonic_lib
from machine import Pin, SoftI2C
import oled_lib
from utime import sleep
from machine import PWM
#Declare pin connection of ultrasonic and create an object
#called Oyen using OOP(Object Oriented Programming)
#OOP format --> library name .class(.....)
Oyen = ultrasonic_lib.HCSR04(trigger_pin=13, echo_pin=14, echo_timeout_us=500*2*30)
#Declare oled connection and create an object for oled -->screen
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128 #based on spec
oled_height = 64 #based on spec
oled = oled_lib.SSD1306_I2C(oled_width, oled_height, i2c_oled)
#Declare buzzer connection
buzzer = PWM(Pin(23), Pin.OUT)
while True:
#Let's test Oyen
jarak_in_cm = Oyen.distance_cm()
#Display the distance from the object
print('Distance from an object is : ',jarak_in_cm, "cm")
print('----------------------------------------------')
print('\n')
#Create text to be displayed on OLED screen
oled.text('object detected')
oled.text(str(jarak_in_cm))
oled.show()
oled.show()
#making desicion on how close the car with an object
if jarak_in_mm < 1000:
for i in range (10):
#turn on the buzzer
buzzer.init(freq=1500, duty=300)
sleep(0.3)
buzzer.init(freq=1, duty=0)
sleep(0.3)
elif 500 <= jarak_in_mm <= 3000:
for i in range (5):
#turn on the buzzer
buzzer.init(freq=1500, duty=300)
sleep(0.8)
buzzer.init(freq=1, duty=0)
sleep(0.8)
else:
buzzer.init(freq=1, duty=0)
sleep (5)