Ignore 0 readings

This commit is contained in:
Dave Astels 2018-10-11 16:03:11 -04:00
parent 62762a7080
commit 62b129295c

View file

@ -126,20 +126,21 @@ while True:
calibrate(True)
x, y, _ = compass.magnetic
y = y * -1 # negation of y & z becasue the board is mounted upside-down
y = y * -1
normalized_x = normalize(x - corrections[0], mins[0], maxes[0])
normalized_y = normalize(y - corrections[1], mins[1], maxes[1])
if x != 0.0 and y != 0.0:
normalized_x = normalize(x - corrections[0], mins[0], maxes[0])
normalized_y = normalize(y - corrections[1], mins[1], maxes[1])
compass_heading = int(math.atan2(normalized_y, normalized_x) * 180.0 / math.pi)
# compass_heading is between -180 and +180 since atan2 returns -pi to +pi
# this translates it to be between 0 and 360
compass_heading += 180
compass_heading = int(math.atan2(normalized_y, normalized_x) * 180.0 / math.pi)
# compass_heading is between -180 and +180 since atan2 returns -pi to +pi
# this translates it to be between 0 and 360
compass_heading += 180
direction_index = ((compass_heading + 15) % 360) // 30
direction_index = ((compass_heading + 15) % 360) // 30
pixels.fill(BLACK)
for l in led_patterns[direction_index]:
pixels[l] = RED
pixels.show()
time.sleep(0.050)
pixels.fill(BLACK)
for l in led_patterns[direction_index]:
pixels[l] = RED
pixels.show()
time.sleep(0.050)