Skip to content

Commit

Permalink
Update weather.py to show 16 wind directions
Browse files Browse the repository at this point in the history
This also resolves the issue with having to tare readings more than once.
  • Loading branch information
MrDrem authored Apr 24, 2024
1 parent 2c62557 commit 99326de
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions enviro/boards/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,27 @@ def wind_speed(sample_time_ms=1000):

def wind_direction():
# adc reading voltage to cardinal direction taken from our python
# library - each array index represents a 45 degree step around
# the compass (index 0 == 0, 1 == 45, 2 == 90, etc.)
# library - each array index represents a 22.5 degree step around
# the compass (index 0 == 0, 1 == 22.5, 2 == 45, etc.)
# we find the closest matching value in the array and use the index
# to determine the heading
ADC_TO_DEGREES = (0.9, 2.0, 3.0, 2.8, 2.5, 1.5, 0.3, 0.6)
ADC_TO_DEGREES = (2.533, 1.308, 1.487, 0.270, 0.300, 0.212, 0.595, 0.408,
0.926, 0.789, 2.031, 1.932, 3.046, 2.667, 2.859, 2.265)

closest_index = -1
last_index = None
closest_value = float('inf')

# ensure we have two readings that match in a row as otherwise if
# you read during transition between two values it can glitch
# fixes https://github.com/pimoroni/enviro/issues/20
while True:
value = wind_direction_pin.read_voltage()
value = wind_direction_pin.read_voltage()

closest_index = -1
closest_value = float('inf')
for i in range(16):
distance = abs(ADC_TO_DEGREES[i] - value)
if distance < closest_value:
closest_value = distance
closest_index = i

for i in range(8):
distance = abs(ADC_TO_DEGREES[i] - value)
if distance < closest_value:
closest_value = distance
closest_index = i

if last_index == closest_index:
break

last_index = closest_index

return closest_index * 45
voltage = value
resistance = (voltage * 10000) / (3.3 - voltage)
return closest_index * 22.5

def rainfall(seconds_since_last):
amount = 0
Expand Down

0 comments on commit 99326de

Please sign in to comment.