from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd
# Make it so the LCD screen says 'Danger, danger Will Robinson! Object detected' When something is close.
# When something is not close 'Whew, the coast is clear! You're safe for now'
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
led = machine.Pin(18, machine.Pin.OUT)
# print("Hello, Pi Pico W!")
# You will have some time on this tomorrow, so don't worry about the due date
# This code will get the distance sensor working
# You just have add the led and the if statement
# Distance sensor basic:
print("Hello, Pi Pico W!")
from machine import Pin
import utime
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
distance = 0
def ultra():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
global distance
distance = (timepassed * 0.0343) / 2
print("The distance from object is ", distance, "cm")
while True:
# if distance < 50:
# led.value(1)
# else:
# led.value(0)
# led.value(1)
# sleep(1)
# led.value(0)
formatdistance = "{:.2f}".format(distance)
print (distance)
ultra()
# utime.sleep(1)
# print(I2C_ADDR)
# lcd.blink_cursor_on()
lcd.putstr(str("Danger, danger Will Robinson! Object detected") )
# lcd.putstr("I2C Address:"+str(I2C_ADDR)+"\n")
# lcd.putstr("Tom's Hardware")
sleep(0.2)
lcd.clear()
# lcd.putstr("I2C Address:"+str(hex(I2C_ADDR))+"\n")
# lcd.putstr("Tom's Hardware")
# sleep(2)
# lcd.blink_cursor_off()
# lcd.clear()
# lcd.putstr("Backlight Test")
# for i in range(10):
# lcd.backlight_on()
# sleep(0.2)
# lcd.backlight_off()
# sleep(0.2)
# lcd.backlight_on()
# lcd.hide_cursor()
# for i in range(20):
# lcd.putstr(str(i))
# sleep(0.4)
# lcd.clear()
# put it here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!