#import all libraries/modules
from machine import Pin, I2C
import library_ultrasonic 
import ssd1306
from machine import Pin, PWM
from utime import sleep

#Pin declaration
i2c = I2C(scl=Pin(22), sda=Pin(21))
TRIG = Pin(13, Pin.IN)
ECHO = Pin(12, Pin.OUT)
BUZZ_Pin = Pin(15, Pin.OUT)

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

sensor_jarak = library_ultrasonic.HCSR04(trigger_pin=TRIG, echo_pin=ECHO)

while True:
    #ULTRASONIC PART 
    print("\n============DISTANCE OF INCOMING OBJECT===========")
    distance_in_cm = sensor_jarak.distance_cm()
    print('An object is detected within  : ', distance_in_cm, 'cm')
    distance_in_mm = sensor_jarak.distance_mm()
    print('An object is detected within  : ', distance_in_mm, 'mm')

    oled.fill(0)
    #oled.show()
    distance = sensor_jarak.distance_mm()
    print('Distance:', distance, 'mm')
    oled.text("Distance (mm)", 0, 15)
    oled.text(str(distance), 0, 35)
    oled.show()
    sleep(1)

    #BUZZER PART
    for afif in range (3):
        bunyi_buzzer = PWM(BUZZ_Pin, freq = 1500, duty = 50)
        sleep(0.7)
        bunyi_buzzer = PWM(BUZZ_Pin, freq = 1500, duty = 0)
        sleep(0.7)

    sleep(5)