print("This program integrates Esp32 with Ultrasonic and Oled!")
#Import all related module/ library
from machine import Pin, SoftI2C, PWM #Pulse Width Modulation
import Perpustakaan_Ultrasonic
import Perpustakaan_Oled
from utime import sleep
#Declare pin connection for ultrasonic and
#Create an object for ultrasonic using OOP(ObjectOrientedProgramming)
#formatOOP --> library name.class name()
Zubir = Perpustakaan_Ultrasonic.HCSR04( trigger_pin= 12, echo_pin= 14, echo_timeout_us=500*2*30)
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
#declare buzzer connection
buzzer = PWM(Pin(15), Pin.OUT)
Red_LED = Pin(18, Pin.OUT)
Yellow_LED = Pin(5, Pin.OUT)
Green_LED = Pin(4, Pin.OUT)
oled = Perpustakaan_Oled.SSD1306_I2C(oled_width, oled_height, i2c_oled)
while True: #forever loop
#Test ciku!
range_in_mm = Zubir.distance_mm()
range_in_cm = Zubir.distance_cm()
#Seen by programmer
print('The distance from an object in mm is',range_in_mm)
print('The distance from an object in mm is',range_in_cm)
print('---------------------------------------------------')
print("\n")
#Display the distance value on oled screen:
Green_LED.on()
Yellow_LED.off()
Red_LED.off()
oled.fill(1)
oled.text('Jarak dikesan', 0, 10, 0)#x- axis, y- axis, colour font
oled.text(str(range_in_mm), 0, 30, 0)
oled.text('mm', 40, 30, 0)
oled.text(str(range_in_cm), 0, 50, 0)
oled.text('cm', 70, 50, 0)
oled.show()
sleep(3)
#Making decision on how close the object is
if range_in_cm < 100:
#Making sound for buzzer
for i in range(4):
Red_LED.on()
Green_LED.off()
Yellow_LED.off()
buzzer.init(freq= 3500, duty=300)
sleep(2)
buzzer.init(freq= 1, duty=0)
sleep(2)
elif 150 <= range_in_cm < 170:
for i in range(4):
Yellow_LED.on()
Red_LED.off()
Green_LED.off()
buzzer.init(freq= 500, duty=300)
sleep(2)
buzzer.init(freq= 1, duty=0)
sleep(2)
else:
buzzer.init(freq= 1, duty=0)
sleep(3)