1
-
2
1
"""
3
2
Read the barometric reading in the air
4
3
Visualize air reading changes over time as a color animation on a NeoPixel strip
5
4
Display a "sinking" or "rising" graphic on the screen along with recent reading data
6
5
7
6
Code by Erin St Blaine for Adafruit Industries
8
7
"""
9
-
10
8
import time
11
9
import board
12
10
import neopixel
16
14
from adafruit_display_text import label
17
15
from adafruit_bitmap_font import bitmap_font
18
16
19
-
20
17
num_leds = 79 #number of LEDs in your strip
21
- timeToCheck = 100 # set the amount of time between sensor checks. 7800 is approx. 1 hour
18
+ timeToCheck = 23400 # set the amount of time between sensor checks. 7800 is approx. 1 hour
22
19
23
20
# Barometer or Thermometer? Uncomment the section you want to use
24
21
25
- # BAROMETER RANGES (hPa)
26
- # set desired reading range -- the NeoPixel palette choice will be determined by these thresholds
22
+ #BAROMETER RANGES (hPa)
23
+ #set desired reading range -- the NeoPixel palette choice will be determined by these thresholds
27
24
deviceType = 0
28
25
min_reading = 960
29
26
med_reading = 965
32
29
33
30
"""
34
31
# THERMOMETER RANGES (C)
35
- # set desired temperature range -- the NeoPixel palette choice will be determined by these thresholds
32
+ # set desired temperature range - NeoPixel palette choice determined by these thresholds
36
33
deviceType = 1
37
34
min_reading = 25
38
- med_reading = 27
39
- high_reading= 31
40
- max_reading = 33
35
+ med_reading = 26
36
+ high_reading= 27
37
+ max_reading = 28
41
38
"""
42
39
43
- #get an initial sensor reading
40
+ # get an initial sensor reading
44
41
if deviceType == 0 :
45
42
reading = clue .pressure
46
43
else :
51
48
reading2 = reading1
52
49
reading3 = reading2
53
50
counter = 0
54
- toggle = 1 #for on/off switch on button A
55
- displayOn = 1 #to turn the display on and off with button B
51
+ toggle = 1 # for on/off switch on button A
52
+ displayOn = 1 # to turn the display on and off with button B
53
+ button_b_pressed = False
54
+ button_a_pressed = False
56
55
57
56
clue .display .brightness = 0.8
58
57
clue_display = displayio .Group (max_size = 4 )
99
98
clue .display .show (clue_display )
100
99
101
100
# Define color Palettes
102
- waterPalette = [
103
- 0x00d9ff ,
104
- 0x006f82 ,
105
- 0x43bfb9 ,
106
- 0x0066ff ]
107
- icePalette = [
108
- 0x8080FF ,
109
- 0x8080FF ,
110
- 0x8080FF ,
111
- 0x0000FF ,
112
- 0xC88AFF ]
113
- sunPalette = [
114
- 0xffaa00 ,
115
- 0xffdd00 ,
116
- 0x7d5b06 ,
117
- 0xfffca8 ]
118
- firePalette = [
119
- 0xff0000 ,
120
- 0xff5500 ,
121
- 0x8a3104 ,
122
- 0xffaa00 ]
123
- forestPalette = [
124
- 0xccffa8 ,
125
- 0x69f505 ,
126
- 0x05f551 ,
127
- 0x2c8247 ]
128
-
129
- #set up default initial palettes, just for startup
101
+ waterPalette = [0x00d9ff , 0x006f82 , 0x43bfb9 , 0x0066ff ]
102
+ icePalette = [0x8080FF , 0x8080FF , 0x8080FF , 0x0000FF , 0xC88AFF ]
103
+ sunPalette = [0xffaa00 , 0xffdd00 , 0x7d5b06 , 0xfffca8 ]
104
+ firePalette = [0xff0000 , 0xff5500 , 0x8a3104 , 0xffaa00 ]
105
+ forestPalette = [0x76DB00 , 0x69f505 , 0x05f551 , 0x3B6D00 ]
106
+
107
+ # set up default initial palettes, just for startup
130
108
palette = forestPalette
131
109
palette2 = waterPalette
132
110
palette3 = icePalette
140
118
141
119
while True :
142
120
# use button A to toggle the NeoPixels on or off by changing brightness
143
- if clue .button_a :
121
+ if clue .button_a and not button_a_pressed : # If button A pressed...
122
+ print ("Button A pressed." )
144
123
if toggle == 1 :
145
124
toggle = 0
146
125
pixels .brightness = 0
149
128
toggle = 1
150
129
pixels .brightness = 1.0
151
130
clue .display .brightness = 0.8
152
- if clue .button_b :
153
- #Toggle only the display on and off
131
+ button_a_pressed = True # Set to True.
132
+ time .sleep (0.03 ) # Debounce.
133
+ if not clue .button_a and button_a_pressed : # On button release...
134
+ button_a_pressed = False # Set to False.
135
+ time .sleep (0.03 ) # Debounce.
136
+ if clue .button_b and not button_b_pressed : # If button B pressed...
137
+ print ("Button B pressed." )
138
+ # Toggle only the display on and off
154
139
if displayOn == 0 :
155
140
clue .display .brightness = 0.8
156
141
displayOn = 1
157
142
else :
158
143
clue .display .brightness = 0
159
144
displayOn = 0
145
+ button_b_pressed = True # Set to True.
146
+ time .sleep (0.03 ) # Debounce.
147
+ if not clue .button_b and button_b_pressed : # On button release...
148
+ button_b_pressed = False # Set to False.
149
+ time .sleep (0.03 ) # Debounce.
160
150
161
- #assign color palette to NeoPixel section 1 based on the current reading reading
151
+ # assign color palette to NeoPixel section 1 based on the current reading reading
162
152
if reading1 < min_reading :
163
153
palette = firePalette
164
- elif reading1 > min_reading and reading1 < med_reading :
154
+ elif min_reading > reading1 > med_reading :
165
155
palette = sunPalette
166
- elif reading1 > med_reading and reading1 < high_reading :
156
+ elif med_reading > reading1 > high_reading :
167
157
palette = forestPalette
168
- elif reading1 > high_reading and reading1 < max_reading :
158
+ elif high_reading > reading1 > max_reading :
169
159
palette = waterPalette
170
160
else :
171
161
palette = icePalette
172
- #Map colors to pixels. Adjust range numbers to light up specific pixels. This configuration
162
+ # Map colors to pixels. Adjust range numbers to light up specific pixels. This configuration
173
163
# maps to a reflected gradient, with pixel 0 in the upper left corner
174
164
# Load each pixel's color from the palette using an offset, run it
175
165
# through the gamma function, pack RGB value and assign to pixel.
227
217
timer_label .text = "{}" .format (counter )
228
218
clue .display .show (clue_display )
229
219
230
- #Is it time to update?
220
+ # Is it time to update?
231
221
if counter > timeToCheck :
232
- #This moves the current data to the "1 hour old" section of pixels and the "1 hour old" data
233
- #to the "2 hours old" section of pixels
222
+ # This moves the current data to the "1 hour old" section of pixels and the "1 hour old"
223
+ # data to the "2 hours old" section of pixels
234
224
palette3 = palette2
235
225
palette2 = palette
236
226
reading3 = reading2
237
227
reading2 = reading1
238
228
reading1 = reading
239
- #take a new sensor reading and reset the counter
229
+ # take a new sensor reading and reset the counter
240
230
if deviceType == 0 :
241
231
reading = clue .pressure
242
232
else :
243
233
reading = clue .temperature
244
234
counter = 0
245
- #if reading is rising, show rising image and position text at the bottom
235
+ # if reading is rising, show rising image and position text at the bottom
246
236
if reading1 > reading2 :
247
237
sinking_sprite .x = 300
248
238
reading_label .y = 134
249
239
reading2_label .y = 164
250
240
reading3_label .y = 194
251
241
timer_label .y = 224
252
- #if reading is falling, show sinking image and position text at the top
253
- elif reading2 < reading3 : #reading is falling
242
+ # if reading is falling, show sinking image and position text at the top
243
+ elif reading1 < reading2 : #reading is falling
254
244
sinking_sprite .x = 0
255
245
reading_label .y = 24
256
246
reading2_label .y = 54
257
247
reading3_label .y = 84
258
248
timer_label .y = 114
259
- #otherwise keep counting up
249
+ # otherwise keep counting up
260
250
else :
261
- counter = counter + 1
251
+ counter = counter + 1
0 commit comments