|
1 | | -# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
2 | 2 | # contributions by J Fletcher, adapting code by Prof Gallaugher: |
3 | 3 | # https://www.youtube.com/watch?v=cdx1A1xoEWc&t=5s |
4 | 4 | # tested on ESP32-S3 Reverse TFT Feather: |
|
17 | 17 |
|
18 | 18 | # Create sensor object, communicating over the board's default I2C bus |
19 | 19 | # i2c = board.I2C() # uses board.SCL and board.SDA |
20 | | -i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller |
| 20 | +i2c = board.STEMMA_I2C() |
| 21 | +# For using the built-in STEMMA QT connector on a microcontroller |
21 | 22 | vl53 = adafruit_vl53l1x.VL53L1X(i2c) |
22 | 23 |
|
23 | 24 | # Create a Label to show the readings. If you have a very small |
|
34 | 35 | # set the main_group as the root_group of the built-in DISPLAY |
35 | 36 | board.DISPLAY.root_group = main_group |
36 | 37 | # create a display object placeholder to be updated by the loop |
37 | | -screen = (f"Distance: {''}cm, {''}in, {''}ft") |
| 38 | +screen = f"Distance: {''}cm, {''}in, {''}ft" |
38 | 39 | # initiate repeated sensor readings |
39 | 40 | vl53.start_ranging() |
40 | 41 |
|
|
45 | 46 | # Assuming the first 'try' succeeds, this will be updated once the loop starts over |
46 | 47 | display_output_label.text = screen |
47 | 48 |
|
48 | | - # This 'try' sequence will either update the displayed items with fresh data or repeat the |
49 | | - # last available data. VL53L1X sensors output `None` when no object reflects the laser, |
| 49 | + # This 'try' sequence will either update the displayed items with fresh data or repeat the |
| 50 | + # last available data. VL53L1X sensors output `None` when no object reflects the laser, |
50 | 51 | # e.g., there is nothing within 4 meters, or when objects pass too quickly in and out of |
51 | 52 | # view (usually perpendicular to the field of vision). |
52 | 53 | try: |
53 | | - if vl53.distance: # simple test to see there is a value to read; no value = exception |
54 | | - distance = vl53.distance # sets the variable (used by the display) to the sensor data |
55 | | - inches = distance*0.394 # VL53L1X outputs distance in metric, so we convert to imperial |
56 | | - screen = (f"Distance: {distance: .1f}cm, {inches: .1f}in, {inches/12: .1f}ft") |
| 54 | + if vl53.distance: |
| 55 | + # simple test to see there is a value to read; no value = exception |
| 56 | + distance = vl53.distance |
| 57 | + # sets the variable (used by the display) to the sensor data |
| 58 | + inches = distance * 0.394 |
| 59 | + # VL53L1X outputs distance in metric, so we convert to imperial |
| 60 | + screen = f"Distance: {distance: .1f}cm, {inches: .1f}in, {inches/12: .1f}ft" |
57 | 61 | # if we made it this far, we have new data to display! |
58 | | - except TypeError: |
| 62 | + except TypeError: |
59 | 63 | repeat_screen = screen |
60 | 64 | screen = repeat_screen |
61 | 65 | # if things went sideways, we repeat the previous loop's data so we can try again |
62 | | - |
| 66 | + |
63 | 67 | time.sleep(0.25) |
0 commit comments