Format Trinket Ultrasonic Rangefinder
MODIFY
Trinket_Ultrasonic_Rangefinder/Trinket_Ultrasonic_Rangefinder.py
Trinket_Ultrasonic_Rangefinder/Trinket_Ultrasonic_Rangefinder.py
# * LV-EZ1 Ultrasonic Sensor PW pin to Trinket GPIO #1
# * Backlight can be hard wired by connecting LCD pin 16, 17 or 18 to GND
+import time
+
+import adafruit_character_lcd
import board
import busio
-import time
import pulseio
-import adafruit_character_lcd
ez1pin = board.D1 # Trinket GPIO #1
@@ -43,65 +44,76 @@ rangevalue = [0, 0, 0, 0, 0, 0, 0, 0, 0]
# 25ms sensor power up pause
time.sleep(.25)
-# EZ1 ultrasonic sensor is measuring "time of flight"
-# * convert time of flight into distance
-# * centimeters (divide by 58mS)
-# * inches (divide by 147mS)
-# * centimeter is default output, flip comments for inches
-def tof_to_distance(tof):
-# convert_to_inches = 147
- convert_to_cm = 58
-# inches = tof / convert_to_inches
+def tof_cm(tof):
+ """
+ EZ1 ultrasonic sensor is measuring "time of flight"
+ Converts time of flight into distance in centimeters
+ """
+ convert_to_cm = 58
cm = tof / convert_to_cm
-# return inches
return cm
-# find the mode (most common value reported)
-# will return median (center of sorted list)
-# should mode not be found
-def mode(x, n):
- maxcount = 0
+def tof_inches(tof):
+ """
+ EZ1 ultrasonic sensor is measuring "time of flight"
+ Converts time of flight into distance in inches
+ """
+ convert_to_inches = 147
+ inches = tof / convert_to_inches
+
+ return inches
+
+
+def find_mode(x):
+ """
+ find the mode (most common value reported)
+ will return median (center of sorted list)
+ should mode not be found
+ """
+ n = len(x)
+
+ max_count = 0
mode = 0
bimodal = 0
- prevcount = 0
counter = 0
- i = 0
+ index = 0
- while ( i < ( n - 1 ) ):
- prevcount = counter
+ while index < (n - 1):
+ prev_count = counter
counter = 0
- while ( ( x[i] ) == ( x[i+1] ) ):
+ while (x[index]) == (x[index + 1]):
counter += 1
- i += 1
+ index += 1
- if ( ( counter > prevcount ) and ( counter > maxcount ) ):
- mode = x[i]
- maxcount = counter
+ if (counter > prev_count) and (counter > max_count):
+ mode = x[index]
+ max_count = counter
bimodal = 0
- if ( counter == 0 ):
- i += 1
+ if counter == 0:
+ index += 1
# If the dataset has 2 or more modes.
- if ( counter == maxcount ):
+ if counter == max_count:
bimodal = 1
# Return the median if there is no mode.
- if ( ( mode == 0 ) or ( bimodal == 1 ) ):
+ if (mode == 0) or (bimodal == 1):
mode = x[int(n / 2)]
return mode
+
while True:
# wait between samples
time.sleep(.5)
- if ( len(pulses) == samples ):
+ if len(pulses) == samples:
j = 0 # rangevalue array counter
# only save the values within range
@@ -110,12 +122,11 @@ while True:
for i in range(0, samples):
tof = pulses[i] # time of flight - PWM HIGH
- if ( ( tof > 880 ) and ( tof < 37500 ) ):
- if ( j < len(rangevalue) ):
- rangevalue[j] = tof_to_distance(tof)
+ if 880 < tof < 37500:
+ if j < len(rangevalue):
+ rangevalue[j] = tof_cm(tof)
j += 1
-
# clear pulse samples
pulses.clear() # clear all values in pulses[]
@@ -123,7 +134,7 @@ while True:
rangevalue = sorted(rangevalue)
# returns mode or median
- mode_result = int(mode(rangevalue, len(rangevalue)))
+ mode_result = int(find_mode(rangevalue))
# python console prints both centimeter and inches distance
cm2in = .393701

Format Trinket Ultrasonic Rangefinder