Skip to content

Commit fe196e9

Browse files
authored
Merge pull request #2301 from adafruit/servo_barometer
Updating CLUE barometer
2 parents 10d84a3 + 4913b0b commit fe196e9

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

CLUE_Servo_Barometer/code.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
from adafruit_clue import clue
1313
from 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
1732
display = board.DISPLAY
@@ -26,16 +41,6 @@
2641
blue = 0x0000FF
2742
red = 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
4045
temp_label = label.Label(font, text=temp_header, color=red, x=5, y=10)
4146
temp_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
6671
clock = 0
6772
# units state
68-
us_units = False
73+
metric_units = False
6974

7075
while 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("\nTemperature: %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

Comments
 (0)