# Complete project details at https://RandomNerdTutorials.com/micropython-hc-sr04-ultrasonic-esp32-esp8266/
from machine import Pin, I2C
import ssd1306
from machine import Pin, PWM
from hcsr04 import HCSR04
from time import sleep

# ESP32 Pin assignment 
i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=10000)
BUZZ_Pin = Pin(15, Pin.OUT)

sensor_jarak = sensor(trigger_pin, echo_pin)

# ESP8266 Pin assignment
#i2c = I2C(scl=Pin(5), sda=Pin(4))
#sensor = HCSR04(trigger_pin=12, echo_pin=14, echo_timeout_us=10000)

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


while True:

  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.distance_mm()
  print('Distance:', distance, 'mm')
  oled.text("Distance (mm)", 0, 15)
  oled.text(str(distance), 0, 35)
  oled.show()
  sleep(1)

for aiman 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)