|
12 | 12 | from adafruit_pyportal import PyPortal
|
13 | 13 |
|
14 | 14 | # ------------- Inputs and Outputs Setup ------------- #
|
15 |
| -# init. the temperature sensor |
16 |
| -i2c_bus = busio.I2C(board.SCL, board.SDA) |
17 |
| -adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48) |
18 |
| -adt.high_resolution = True |
| 15 | +try: # attempt to init. the temperature sensor |
| 16 | + i2c_bus = busio.I2C(board.SCL, board.SDA) |
| 17 | + adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48) |
| 18 | + adt.high_resolution = True |
| 19 | +except ValueError: |
| 20 | + # Did not find ADT7410. Probably running on Titano or Pynt |
| 21 | + adt = None |
19 | 22 |
|
20 | 23 | # init. the light sensor
|
21 | 24 | light_sensor = AnalogIn(board.LIGHT)
|
@@ -332,10 +335,14 @@ def switch_view(what_view):
|
332 | 335 | while True:
|
333 | 336 | touch = ts.touch_point
|
334 | 337 | light = light_sensor.value
|
335 |
| - tempC = round(adt.temperature) |
336 |
| - tempF = tempC * 1.8 + 32 |
337 | 338 |
|
338 |
| - sensor_data.text = 'Touch: {}\nLight: {}\n Temp: {}°F'.format(touch, light, tempF) |
| 339 | + if adt: # Only if we have the temperature sensor |
| 340 | + tempC = round(adt.temperature) |
| 341 | + tempF = tempC * 1.8 + 32 |
| 342 | + sensor_data.text = 'Touch: {}\nLight: {}\n Temp: {}°F'.format(touch, light, tempF) |
| 343 | + |
| 344 | + else: # No temperature sensor on Titano and Pynt |
| 345 | + sensor_data.text = 'Touch: {}\nLight: {}'.format(touch, light) |
339 | 346 |
|
340 | 347 | # ------------- Handle Button Press Detection ------------- #
|
341 | 348 | if touch: # Only do this if the screen is touched
|
|
0 commit comments