Skip to content

Commit f83984a

Browse files
committed
Vertical Garden Barometer
first code commit
1 parent 1891d05 commit f83984a

File tree

3 files changed

+261
-0
lines changed

3 files changed

+261
-0
lines changed

Vertical_Garden_Barometer/code.py

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
2+
"""
3+
Read the barometric reading in the air
4+
Visualize air reading changes over time as a color animation on a NeoPixel strip
5+
Display a "sinking" or "rising" graphic on the screen along with recent reading data
6+
7+
Code by Erin St Blaine for Adafruit Industries
8+
"""
9+
10+
import time
11+
import board
12+
import neopixel
13+
from adafruit_clue import clue
14+
import adafruit_fancyled.adafruit_fancyled as fancy
15+
import displayio
16+
from adafruit_display_text import label
17+
from adafruit_bitmap_font import bitmap_font
18+
19+
20+
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
22+
23+
# Barometer or Thermometer? Uncomment the section you want to use
24+
25+
# BAROMETER RANGES (hPa)
26+
# set desired reading range -- the NeoPixel palette choice will be determined by these thresholds
27+
deviceType = 0
28+
min_reading = 960
29+
med_reading = 965
30+
high_reading= 970
31+
max_reading = 975
32+
33+
"""
34+
# THERMOMETER RANGES (C)
35+
# set desired temperature range -- the NeoPixel palette choice will be determined by these thresholds
36+
deviceType = 1
37+
min_reading = 25
38+
med_reading = 27
39+
high_reading= 31
40+
max_reading = 33
41+
"""
42+
43+
#get an initial sensor reading
44+
if deviceType ==0:
45+
reading = clue.pressure
46+
else:
47+
reading = clue.temperature
48+
49+
#set up variables for "remembering" past readings
50+
reading1 = reading
51+
reading2 = reading1
52+
reading3 = reading2
53+
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
56+
57+
clue.display.brightness = 0.8
58+
clue_display = displayio.Group(max_size=4)
59+
60+
# draw the rising image
61+
rising_file = open("rising.bmp", "rb")
62+
rising_bmp = displayio.OnDiskBitmap(rising_file)
63+
rising_sprite = displayio.TileGrid(rising_bmp, pixel_shader=displayio.ColorConverter())
64+
clue_display.append(rising_sprite)
65+
66+
# draw the sinking image
67+
sinking_file = open("sinking.bmp", "rb")
68+
sinking_bmp = displayio.OnDiskBitmap(sinking_file)
69+
sinking_sprite = displayio.TileGrid(sinking_bmp, pixel_shader=displayio.ColorConverter())
70+
clue_display.append(sinking_sprite)
71+
72+
# Create text
73+
# first create the group
74+
text_group = displayio.Group(max_size=5, scale=1)
75+
# Make a label
76+
reading_font = bitmap_font.load_font("/font/RacingSansOne-Regular-29.bdf")
77+
reading_font.load_glyphs("0123456789ADSWabcdefghijklmnopqrstuvwxyz:!".encode('utf-8'))
78+
reading_label = label.Label(reading_font, color=0xffffff, max_glyphs=15)
79+
reading_label.x = 10
80+
reading_label.y = 24
81+
text_group.append(reading_label)
82+
83+
reading2_label = label.Label(reading_font, color=0xdaf5f4, max_glyphs=15)
84+
reading2_label.x = 10
85+
reading2_label.y = 54
86+
text_group.append(reading2_label)
87+
88+
reading3_label = label.Label(reading_font, color=0x4f3ab1, max_glyphs=15)
89+
reading3_label.x = 10
90+
reading3_label.y = 84
91+
text_group.append(reading3_label)
92+
93+
timer_label = label.Label(reading_font, color=0x072170, max_glyphs=15)
94+
timer_label.x = 10
95+
timer_label.y = 114
96+
text_group.append(timer_label)
97+
98+
clue_display.append(text_group)
99+
clue.display.show(clue_display)
100+
101+
# 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
130+
palette = forestPalette
131+
palette2 = waterPalette
132+
palette3 = icePalette
133+
134+
# Declare a NeoPixel object on pin A4 with num_leds pixels, no auto-write.
135+
# Set brightness to max because we'll be using FancyLED's brightness control.
136+
pixels = neopixel.NeoPixel(board.A4, num_leds, brightness=1.0,
137+
auto_write=False)
138+
139+
offset = 0 # Positional offset into color palette to get it to 'spin'
140+
141+
while True:
142+
# use button A to toggle the NeoPixels on or off by changing brightness
143+
if clue.button_a:
144+
if toggle == 1:
145+
toggle = 0
146+
pixels.brightness = 0
147+
clue.display.brightness = 0
148+
elif toggle == 0:
149+
toggle = 1
150+
pixels.brightness = 1.0
151+
clue.display.brightness = 0.8
152+
if clue.button_b:
153+
#Toggle only the display on and off
154+
if displayOn == 0:
155+
clue.display.brightness = 0.8
156+
displayOn = 1
157+
else:
158+
clue.display.brightness = 0
159+
displayOn = 0
160+
161+
#assign color palette to NeoPixel section 1 based on the current reading reading
162+
if reading1 < min_reading:
163+
palette = firePalette
164+
elif reading1 > min_reading and reading1 < med_reading:
165+
palette = sunPalette
166+
elif reading1 > med_reading and reading1 < high_reading:
167+
palette = forestPalette
168+
elif reading1 > high_reading and reading1 < max_reading:
169+
palette = waterPalette
170+
else:
171+
palette = icePalette
172+
#Map colors to pixels. Adjust range numbers to light up specific pixels. This configuration
173+
# maps to a reflected gradient, with pixel 0 in the upper left corner
174+
# Load each pixel's color from the palette using an offset, run it
175+
# through the gamma function, pack RGB value and assign to pixel.
176+
for i in range(23, 31): #center right -- present moment
177+
color = fancy.palette_lookup(palette, offset + i / num_leds)
178+
color = fancy.gamma_adjust(color, brightness=0.25)
179+
pixels[i] = color.pack()
180+
181+
for i in range(63, 71): #center left -- present moment
182+
color = fancy.palette_lookup(palette, offset + i / num_leds)
183+
color = fancy.gamma_adjust(color, brightness=0.25)
184+
pixels[i] = color.pack()
185+
186+
for i in range(16, 23): #top mid right -- 1 cycle ago
187+
color = fancy.palette_lookup(palette2, offset + i / num_leds)
188+
color = fancy.gamma_adjust(color, brightness=0.25)
189+
pixels[i] = color.pack()
190+
191+
for i in range(71, 78): #top mid left -- 1 cycle ago
192+
color = fancy.palette_lookup(palette2, offset + i / num_leds)
193+
color = fancy.gamma_adjust(color, brightness=0.25)
194+
pixels[i] = color.pack()
195+
196+
for i in range(31, 38): #bottom mid right -- 1 cycle ago
197+
color = fancy.palette_lookup(palette2, offset + i / num_leds)
198+
color = fancy.gamma_adjust(color, brightness=0.25)
199+
pixels[i] = color.pack()
200+
201+
for i in range(56, 63): #bottom mid left -- 1 cycle ago
202+
color = fancy.palette_lookup(palette2, offset + i / num_leds)
203+
color = fancy.gamma_adjust(color, brightness=0.25)
204+
pixels[i] = color.pack()
205+
206+
for i in range(0, 16): #top right -- 2 cycles ago
207+
color = fancy.palette_lookup(palette3, offset + i / num_leds)
208+
color = fancy.gamma_adjust(color, brightness=0.25)
209+
pixels[i] = color.pack()
210+
211+
for i in range(77, 79): #top left -- 2 cycles ago
212+
color = fancy.palette_lookup(palette3, offset + i / num_leds)
213+
color = fancy.gamma_adjust(color, brightness=0.25)
214+
pixels[i] = color.pack()
215+
216+
for i in range(38, 56): #bottom -- 2 cycles ago
217+
color = fancy.palette_lookup(palette3, offset + i / num_leds)
218+
color = fancy.gamma_adjust(color, brightness=0.25)
219+
pixels[i] = color.pack()
220+
221+
pixels.show()
222+
offset += 0.01 # Bigger number = faster spin
223+
224+
reading_label.text = "Now {:.1f}".format(reading1)
225+
reading2_label.text = "Last {:.1f}".format(reading2)
226+
reading3_label.text = "Prev {:.1f}".format(reading3)
227+
timer_label.text = "{}".format(counter)
228+
clue.display.show(clue_display)
229+
230+
#Is it time to update?
231+
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
234+
palette3 = palette2
235+
palette2 = palette
236+
reading3 = reading2
237+
reading2 = reading1
238+
reading1 = reading
239+
#take a new sensor reading and reset the counter
240+
if deviceType == 0:
241+
reading = clue.pressure
242+
else:
243+
reading = clue.temperature
244+
counter = 0
245+
#if reading is rising, show rising image and position text at the bottom
246+
if reading1 > reading2:
247+
sinking_sprite.x = 300
248+
reading_label.y = 134
249+
reading2_label.y = 164
250+
reading3_label.y = 194
251+
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
254+
sinking_sprite.x = 0
255+
reading_label.y = 24
256+
reading2_label.y = 54
257+
reading3_label.y = 84
258+
timer_label.y = 114
259+
#otherwise keep counting up
260+
else:
261+
counter = counter + 1

Vertical_Garden_Barometer/rising.bmp

113 KB
Binary file not shown.

Vertical_Garden_Barometer/sinking.bmp

113 KB
Binary file not shown.

0 commit comments

Comments
 (0)