from servo import Servo
from hcsr04 import HCSR04
from time import sleep
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
#Ultra Sonic Sensor setup
sensor = HCSR04(trigger_pin = 5, echo_pin = 18, echo_timeout_us = 100000)
#Servo setup
motor = Servo(pin=19)
#I2C LCD Setup
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
#Global Variables
freeSlots = 5
entryFlag = 0
exitFlag = 0
#PIR pin declaration
entry_PIR = Pin(13, Pin.IN)
exit_PIR = Pin(25, Pin.IN)
#Functions Definition
def displayWelcome():
lcd.move_to(0,0)
lcd.putstr("Welcome, DriveIn")
def displaySlots():
lcd.move_to(2,0)
lcd.putstr("Free Slots: " + str(freeSlots))
while True:
distance = sensor.distance_cm()
if(freeSlots > 0):
if(distance < 50):
#Open Servo
motor.move(0)
#Welcome
displayWelcome()
sleep(2)
lcd.clear()
elif(distance > 50):
motor.move(90)
if(entry_PIR.value() == 1 and entryFlag == 0):
#Decrease free slots
freeSlots = freeSlots - 1
if(freeSlots < 0):
freeSlots = 0
entryFlag = 1
elif(entry_PIR.value() == 0):
entryFlag = 0
if(freeSlots < 5):
if(exit_PIR.value() == 1 and exitFlag == 0):
#Increase free slots
freeSlots = freeSlots + 1
if(freeSlots < 0):
freeSlots = 0
exitFlag = 1
elif(exit_PIR.value() == 0):
exitFlag = 0
print('Distance: ', distance)
displaySlots()