print("Hello, ULTRASONIC")
print("Date: 27/11/2023")
print("By Luqman")
#Import all libraries and modules
import library_ultrasonic
from machine import Pin, PWM
from utime import sleep
#Pin declaration
TRIG = Pin(14, Pin.IN)
ECHO = Pin(27, Pin.OUT)
Buzzer_Pin = Pin(4, Pin.OUT)
#Declare/Create object name for sensors with libraries
#Object NAME = library.NAME.CLASS_NAME(........)
sensor_jarak = library_ultrasonic.HCSR04(trigger_pin = TRIG , echo_pin = ECHO)
#Main program
while True:
#ULTRASONIC PART
# print("\n======DISTANCE OF INCOMING OBJECT======\n")
jarak_dalam_cm = sensor_jarak.distance_cm()
print("An object is detected within:",jarak_dalam_cm,"cm")
# jarak_dalam_mm = sensor_jarak.distance_mm()
# print("An object is detected within:",jarak_dalam_mm,"mm")
#BUZZER PART
# for ali in range(3):
# tone_buzzer = PWM(Buzzer_Pin, freq = 1500, duty = 50)
# sleep(0.5)
# tone_buzzer = PWM(Buzzer_Pin, freq = 1500, duty = 0)
# sleep(0.5)
# sleep(5) #In every 5 sec, ultrasonic sensor will sense new object
#CAR REVERSE SENSOR
if jarak_dalam_cm > 200:
for u in range(5):
print("Back of the car is too far. Continue reverse.")
tone_buzzer = PWM(Buzzer_Pin, freq = 1600, duty = 400)
sleep(1.5)
tone_buzzer = PWM(Buzzer_Pin, freq = 1, duty = 0)
sleep(1.5)
elif 101 <= jarak_dalam_cm <200:
for u in range (5):
print("Back of the car is far. Continue reverse. ")
tone_buzzer = PWM(Buzzer_Pin, freq = 1600, duty = 400)
sleep(1)
tone_buzzer = PWM(Buzzer_Pin, freq = 1, duty = 0)
sleep(1)
elif 51 <= jarak_dalam_cm <100:
for u in range (5):
print("Careful back of the car is close. Continue reverse. ")
tone_buzzer = PWM(Buzzer_Pin, freq = 1600, duty = 400)
sleep(0.5)
tone_buzzer = PWM(Buzzer_Pin, freq = 1, duty = 0)
sleep(0.5)
else:
print("Warning!! back of your car is very close to the wall!!")
tone_buzzer = PWM(Buzzer_Pin, freq = 1600, duty = 400)
sleep(5)
#HOMEWORK
#buat 3 condition utk reverse (tmbah loop)
#if distance > 2cm = alarm (beep slow)
#if distance 1m < j < 2m = alarm (beep laju)
#if distance 50m < j < 1m = alarm (beep lagi laju)
#if kena dinding (beep lama)