Skip to content

Commit ffde4b2

Browse files
committed
Updated Vertical Garden code
Added a button debounce
1 parent f83984a commit ffde4b2

File tree

1 file changed

+48
-58
lines changed

1 file changed

+48
-58
lines changed

Vertical_Garden_Barometer/code.py

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
21
"""
32
Read the barometric reading in the air
43
Visualize air reading changes over time as a color animation on a NeoPixel strip
54
Display a "sinking" or "rising" graphic on the screen along with recent reading data
65
76
Code by Erin St Blaine for Adafruit Industries
87
"""
9-
108
import time
119
import board
1210
import neopixel
@@ -16,14 +14,13 @@
1614
from adafruit_display_text import label
1715
from adafruit_bitmap_font import bitmap_font
1816

19-
2017
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
2219

2320
# Barometer or Thermometer? Uncomment the section you want to use
2421

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
2724
deviceType = 0
2825
min_reading = 960
2926
med_reading = 965
@@ -32,15 +29,15 @@
3229

3330
"""
3431
# 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
3633
deviceType = 1
3734
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
4138
"""
4239

43-
#get an initial sensor reading
40+
# get an initial sensor reading
4441
if deviceType ==0:
4542
reading = clue.pressure
4643
else:
@@ -51,8 +48,10 @@
5148
reading2 = reading1
5249
reading3 = reading2
5350
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
5655

5756
clue.display.brightness = 0.8
5857
clue_display = displayio.Group(max_size=4)
@@ -99,34 +98,13 @@
9998
clue.display.show(clue_display)
10099

101100
# 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
130108
palette = forestPalette
131109
palette2 = waterPalette
132110
palette3 = icePalette
@@ -140,7 +118,8 @@
140118

141119
while True:
142120
# 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.")
144123
if toggle == 1:
145124
toggle = 0
146125
pixels.brightness = 0
@@ -149,27 +128,38 @@
149128
toggle = 1
150129
pixels.brightness = 1.0
151130
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
154139
if displayOn == 0:
155140
clue.display.brightness = 0.8
156141
displayOn = 1
157142
else:
158143
clue.display.brightness = 0
159144
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.
160150

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
162152
if reading1 < min_reading:
163153
palette = firePalette
164-
elif reading1 > min_reading and reading1 < med_reading:
154+
elif min_reading > reading1 > med_reading:
165155
palette = sunPalette
166-
elif reading1 > med_reading and reading1 < high_reading:
156+
elif med_reading > reading1 > high_reading:
167157
palette = forestPalette
168-
elif reading1 > high_reading and reading1 < max_reading:
158+
elif high_reading > reading1 > max_reading:
169159
palette = waterPalette
170160
else:
171161
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
173163
# maps to a reflected gradient, with pixel 0 in the upper left corner
174164
# Load each pixel's color from the palette using an offset, run it
175165
# through the gamma function, pack RGB value and assign to pixel.
@@ -227,35 +217,35 @@
227217
timer_label.text = "{}".format(counter)
228218
clue.display.show(clue_display)
229219

230-
#Is it time to update?
220+
# Is it time to update?
231221
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
234224
palette3 = palette2
235225
palette2 = palette
236226
reading3 = reading2
237227
reading2 = reading1
238228
reading1 = reading
239-
#take a new sensor reading and reset the counter
229+
# take a new sensor reading and reset the counter
240230
if deviceType == 0:
241231
reading = clue.pressure
242232
else:
243233
reading = clue.temperature
244234
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
246236
if reading1 > reading2:
247237
sinking_sprite.x = 300
248238
reading_label.y = 134
249239
reading2_label.y = 164
250240
reading3_label.y = 194
251241
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
254244
sinking_sprite.x = 0
255245
reading_label.y = 24
256246
reading2_label.y = 54
257247
reading3_label.y = 84
258248
timer_label.y = 114
259-
#otherwise keep counting up
249+
# otherwise keep counting up
260250
else:
261-
counter = counter + 1
251+
counter = counter + 1

0 commit comments

Comments
 (0)