print('This program intergrate ESP32 + Ultrasonic')
#import all related module?library
from machine import Pin, SoftI2C, PWM #SoftI2C = SCL,SDA,
import ultrasonic_lib
from utime import sleep
import Oled_ssd
import machine
#Declare pin connection for ultrasonic and
#Create an object for ultrasonic using OOP (Object Oreinted Programming)
#Format OOp --> Library name.class name(trigger_pin, echo_pin, echo_timeout_us=500*2*30)
segments = [33, 25, 26, 27, 14, 12, 13, 15, 5, 18]
#2.Pin declration on OLED
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
#3. Define the OLED variables(Screen Dimensions in pixels)
oled_width = 128 #based on spec
oled_height = 64 #based on spec
#4.Use oop (Object Oreiented Programming) to create the object
#Remarks: Object format --> Library name. class name
#Format (width, Height, i2c)
oled = Oled_ssd.SSD1306_I2C(oled_width, oled_height, i2c_oled)
ciko = ultrasonic_lib.HCSR04(trigger_pin=32, echo_pin=34, echo_timeout_us=500*2*30)
buzzer = PWM(Pin(23), Pin.OUT)
for Pin in segments:
gpio = machine.Pin(Pin, machine.Pin.OUT)
gpio.off()
while True :
#TEST IT !
jauh_in_mm = ciko.distance_mm()
jauh_in_cm = ciko.distance_cm()
#Display the distance value in mm and cm
print('The distance from an object in mm is :', jauh_in_mm)
print('The distance from an object in cm is :', jauh_in_cm)
print('----------------------------------------------------')
print('\n')
oled.show()
if jauh_in_cm < 100 :
#Making sound for buzzer
for i in range (5):
buzzer.init(freq = 1000, duty = 300)
sleep(1)
buzzer.init(freq = 1, duty = 0)
sleep(0.5)
oled.fill(0)
oled.text('Object Detected', 0,0,1) #x-axis , y-axis, colorfont(1-white,0- black)
oled.text(str(jauh_in_mm), 10, 20, 1)
oled.text('mm', 45,20,1)
oled.text(str(jauh_in_cm), 10, 30, 1)
oled.text('cm', 75,30,1)
elif 100 <= jauh_in_cm < 150:
for i in range (5):
buzzer.init(freq = 400, duty = 300)
sleep(1)
buzzer.init(freq = 1, duty = 0)
sleep(0.5)
oled.fill(0)
oled.text('Object Detected', 0,0,1) #x-axis , y-axis, colorfont(1-white,0- black)
oled.text(str(jauh_in_mm), 10, 20, 1)
oled.text('mm', 45,20,1)
oled.text(str(jauh_in_cm), 10, 30, 1)
oled.text('cm', 75,30,1)
else:
buzzer.init(freq = 10 , duty = 0)
oled.fill(0)
oled.text('Object Detected', 0,0,1) #x-axis , y-axis, colorfont(1-white,0- black)
oled.text(str(jauh_in_mm), 10, 20, 1)
oled.text('mm', 45,20,1)
oled.text(str(jauh_in_cm), 10, 30, 1)
oled.text('cm', 75,30,1)
oled.text('Jauh lagi tuu', 0, 50, 1)
sleep(5)