from machine import Pin, PWM, I2C
from utime import sleep
import ssd1306
# Declare pin for LED
led_yellow = Pin(12, Pin.OUT)
# Declare pin for buzzer
buzzer_pin = PWM(Pin(13))
# Setup I2C for OLED display
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Main program
while True:
print("##### MARI CHECK INCOMING DISTANCE !! ###### \n")
# Simulate distance values (you can remove this part if you don't need it)
distance_mm = 150 # replace with your logic or remove if not needed
distance_cm = distance_mm / 10.0
# Print the simulated distance on OLED display
oled.fill(0)
oled.text("Distance: {} cm".format(distance_cm), 0, 0)
oled.show()
# Print the simulated distance on the console
print("UNTUK MENGKAJI HUBUNGAN ANTARA : ", distance_mm, 'mm')
print("UNTUK MENGKAJI HUBUNGAN ANTARA : ", distance_cm, 'cm')
# Control the LED based on distance (you can modify or remove this part)
if distance_cm < 400: # You can adjust this threshold as needed
led_yellow.on()
# Generate a sound on the buzzer
buzzer_pin.freq(1000) # Adjust the frequency of the sound
buzzer_pin.duty(50) # Adjust the duty cycle
sleep(0.2) # Adjust the duration of the sound
buzzer_pin.duty(0) # Turn off the buzzer
else:
led_yellow.off()
sleep(2) # In every 2 seconds, the program will simulate new incoming distance