print("Hello, ESP32!")
print("date: 2/4/2026")
#import libraries/modules
from machine import Pin, SoftI2C
import oled_library
import ultrasonic_library
from machine import Pin, PWM
from utime import sleep
#pin declaration
oled_pin = SoftI2C(scl = Pin(22) , sda = Pin(21) )
TRIG = Pin(27)
ECHO = Pin(26)
buzzer = PWM(Pin(5, Pin.OUT))
merohh = (Pin(18,Pin.OUT))
kuninn = (Pin(17,Pin.OUT))
ijoo = (Pin(16,Pin.OUT))
#object declaration
#required only when external library is used
#objectname = libraryname.classname (...........)
skrin = oled_library.SSD1306_I2C(width = 128 , height = 64 , i2c = oled_pin)
sensor = ultrasonic_library.HCSR04(trigger_pin = TRIG, echo_pin = ECHO)
#main program
while True:
#measure nearby object
jarak_dalam_cm= sensor.distance_cm()
#display on terminal
print("objek terdekat adalah dalam : ", jarak_dalam_cm, "cm")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
if jarak_dalam_cm >=200:
skrin.fill(0) #1 for white, 0 for black
skrin.text("LAB410", 10, 20, col=1)
skrin.show()
merohh.on()
kuninn.off()
ijoo.off()
for i in range(3):
buzzer.freq(1300)
buzzer.duty(500)
sleep(0.2)
buzzer.freq(1300)
buzzer.duty(0)
sleep(0.2)
elif 100 <= jarak_dalam_cm <200:
skrin.fill(0) #1 for white, 0 for black
skrin.text("LAB410", 10, 20, col=1)
skrin.text("THIS IS", 10, 30, col=1)
skrin.text("IOT LAB", 10, 40, col=1)
skrin.show()
merohh.off()
kuninn.on()
ijoo.off()
for i in range(5):
buzzer.freq(1300)
buzzer.duty(200)
sleep(0.3)
buzzer.freq(1300)
buzzer.duty(0)
sleep(0.3)
else:
skrin.fill(0) #1 for white, 0 for black
skrin.text("WELCOME TO", 10, 20, col=1)
skrin.text("IOT LAB", 10, 30, col=1)
skrin.text("HAVE FUN LEARNING", 10, 40, col=1)
skrin.show()
merohh.off()
kuninn.off()
ijoo.on()
for i in range(5):
buzzer.freq(1300)
buzzer.duty(0)
sleep(0.3)
buzzer.freq(1300)
buzzer.duty(0)
sleep(0.3)
sleep(1)