print("This is the program for Car Reverse Sensor SYSTEM")
print("BY ANNISHA FOR IOT")
# STEP 1 : IMPORT MODULES OR LIBRARY
from machine import Pin
from machine import I2C
from machine import PWM
from time import sleep
import ssd1306
import hcsr04
# STEP 2: DECLARE CONNECTION
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
ledR = Pin(15, Pin.OUT)
ledG = Pin(2, Pin.OUT)
BUZZ_Pin = Pin(14, Pin.OUT)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# STEP 2.2 : DECLARE THE CONNECTION FOR ULTRASONIC SENSOR
distanceSensor = hcsr04.HCSR04(trigger_pin=17, echo_pin=16)
print("Social Distancing System")
while True:
# ULTRASONIC PART
oled.fill(0)
oled.text("DISTANCE OF ", 5, 5)
oled.text("PERSON : ", 5, 15)
cm = distanceSensor.distance_cm()
cm_string = str("{:.2f}".format(cm))
oled.text(cm_string + "cm", 5, 30)
if cm > 200:
oled.text("Safe Distance", 5, 45)
ledG.on()
sleep(0.5)
ledG.off()
else:
oled.text("Too Close!", 5, 45)
buzzer = PWM(BUZZ_Pin, freq=1000, duty=512) # Turn on buzzer
ledR.on() # Turn on red LED
sleep(0.5)
buzzer.deinit() # Turn off buzzer
ledR.off() # Turn off red LED
sleep(0.1)
oled.show()