1212from adafruit_clue import clue
1313from adafruit_display_text import label
1414
15+ # pwm setup for servo
16+ pwm = pwmio .PWMOut (board .D0 , duty_cycle = 2 ** 15 , frequency = 50 )
17+ gauge = servo .Servo (pwm )
18+
19+ # bmp280 sensor setup
20+ i2c = board .I2C () # uses board.SCL and board.SDA
21+ bmp280 = adafruit_bmp280 .Adafruit_BMP280_I2C (i2c )
22+
23+ # change depending on your location's elevation
24+ NOMINAL_PRESSURE = 1005.94
25+
26+ PRESSURE_RANGE = 40 # hPa, expected pressure range variance caused by local weather
27+ PRESSURE_LOW_LIM = NOMINAL_PRESSURE - PRESSURE_RANGE
28+ PRESSURE_HIGH_LIM = NOMINAL_PRESSURE + PRESSURE_RANGE
29+
1530# board display
1631# scaling for terminalio font
1732display = board .DISPLAY
2641blue = 0x0000FF
2742red = 0xFF0000
2843
29- # pwm setup for servo
30- pwm = pwmio .PWMOut (board .D0 , duty_cycle = 2 ** 15 , frequency = 50 )
31- gauge = servo .Servo (pwm )
32-
33- # bmp280 sensor setup
34- i2c = board .I2C () # uses board.SCL and board.SDA
35- bmp280 = adafruit_bmp280 .Adafruit_BMP280_I2C (i2c )
36-
37- bmp280 .sea_level_pressure = 1013.25
38-
3944# temperature text elements
4045temp_label = label .Label (font , text = temp_header , color = red , x = 5 , y = 10 )
4146temp_data = label .Label (font , text = temp_text , color = red , x = 5 , y = 25 )
@@ -65,26 +70,27 @@ def hpa_to_inHg(hPa):
6570# time.monotonic clock
6671clock = 0
6772# units state
68- us_units = False
73+ metric_units = False
6974
7075while True :
7176 # non-blocking 2 second delay
7277 if (clock + 2 ) < time .monotonic ():
7378 # map servo range to barometric pressure range
74- servo_value = simpleio .map_range (bmp280 .pressure , 970 , 1050 , 180 , 0 )
79+ servo_value = simpleio .map_range (bmp280 .pressure ,
80+ PRESSURE_LOW_LIM , PRESSURE_HIGH_LIM , 180 , 0 )
7581 # set servo to pressure
7682 gauge .angle = servo_value
7783 # print data for debugging
7884 print ("\n Temperature: %0.1f C" % bmp280 .temperature )
7985 print ("Pressure: %0.1f hPa" % bmp280 .pressure )
8086 print (servo_value )
81- # if not US units...
82- if not us_units :
87+ # if metric units...
88+ if metric_units :
8389 # update temp & pressure text in celcius and hPa
8490 temp_data .text = "%0.1f ºC" % bmp280 .temperature
8591 press_data .text = "%0.1f hPa" % bmp280 .pressure
86- # if US units...
87- if us_units :
92+ # if imperial units...
93+ else :
8894 # convert celcius to fahrenheit
8995 temp_fahrenheit = c_to_f (bmp280 .temperature )
9096 # convert hPa to inHg
@@ -94,9 +100,9 @@ def hpa_to_inHg(hPa):
94100 press_data .text = "%0.1f inHg" % pressure_inHg
95101 # reset time.monotonic() clock
96102 clock = time .monotonic ()
97- # if a button is pressed, us_units is False , show imperial
103+ # if a button is pressed, metric_units is True , show metric
98104 if clue .button_a :
99- us_units = False
100- # if b button is pressed, us_units is True , show US units
105+ metric_units = True
106+ # if b button is pressed, metric_units is False , show imperial units
101107 if clue .button_b :
102- us_units = True
108+ metric_units = False
0 commit comments