SAMENVATTING: In ons eerste project meten we afstanden met de ultrasone sensor en visualiseren we de gemeten afstand door de grafiek op het TFT-
scherm in meer of mindere mate te vullen. In ons voorbeeld vullen we het scherm volledig vanaf een gemeten afstand van 100 cm.
# Load libraries
from
machine
import
Pin, SPI
import
ST7735
import
time
import
lcd_gfx
# Initialization of GPIO16 as input and GPIO17 as output
trig
=
Pin(17, Pin.OUT)
echo
=
Pin(16, Pin.IN, Pin.PULL_DOWN)
# Initialize LCD
spi
=
SPI(0, baudrate=8000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(19),
miso=Pin(16))
lcd
=
ST7735.ST7735(spi, rst=6, ce=17, dc=3)
backlight
=
Pin(2, Pin.OUT)
backlight.high()
lcd.reset()
lcd.begin()
lcd.fill_screen(lcd.rgb_to_565(255, 255, 255))
def
translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan
=
leftMax
-
leftMin
rightSpan
=
rightMax
-
rightMin
# Convert the left range into a 0-1 range (float)
valueScaled
=
float(value
-
# Convert the 0-1 range into a value in the right range.
return
rightMin
+
(valueScaled
# Endless loop for measuring the distance
while
True:
# Distance measurement is started using the 10us trigger signal
trig.value(0)
time.sleep(0.1)
trig.value(1)
# Now wait at the echo input until the signal has been activated
# Then the time is measured for how long it remains activated
time.sleep_us(2)
trig.value(0)
while
echo.value()==0:
pulse_start
=
time.ticks_us()
while
echo.value()==1:
pulse_end
=
time.ticks_us()
pulse_duration
=
pulse_end
leftMin)
/
float(leftSpan)
*
rightSpan)
-
pulse_start
Initialisatie van de ultrasone sensor
en het TFT-scherm
Hulpfunctie voor het aanpassen
van het waardebereik
Afstandsmeting
18