print("Hello, ULTRASONIC")
print("date: 27/11/2023")
print ("created by Nas ") #nak cari library utk ultra sonic kene search
#ultra sonic library+python+esp32
#import all libraries/module
import libarary_ultrasonic
from machine import Pin,PWM
from utime import sleep
#pin declaration
TRIG = Pin(26,Pin.IN)
ECHO = Pin(14,PTin.OUT)
Buzzer=Pin(4,Pin.OUT)
#declare/create object name for sensor with libraries
#object name =library_name.class name (.......)
sensor_sonic = libarary_ultrasonic.HCSR04( trigger_pin=TRIG, echo_pin=ECHO)
#main program
while True :
#ultra sonic PART
print("\n====== DISTANCE OF INCOMING OBJECT=======\n")
Jarak_dalam_cm=sensor_sonic.distance_cm()
print ('An object is detected within :',Jarak_dalam_cm, 'cm')
Jarak_dalam_mm =sensor_sonic.distance_mm()
print ('An object is detected within :',Jarak_dalam_mm, 'mm')
#buzzer PART
for abu in range (100):
tone_buzzer =PWM(Buzzer, freq =1500, duty = 50)
sleep(0.5)
tone_buzzer =PWM(Buzzer, freq =1, duty = 0)
sleep(0.5)
sleep(5) # In every 5 sec,ultrasonic will sense a new object within range (limit 400s)