2. Compass Directions
2.1. Compass Readings
Use compass.heading()
to get an angle from True North where North is 0.
from microbit import *
compass.calibrate()
while True:
display.scroll(compass.heading(), delay=80)
sleep(500)
Exercises
Use the code above and turn the microbit till it reads a number close to 0.
Turn about 90 degrees clockwise to the East and get the reading.
Turn another 90 degrees clockwise to the South and get the reading.
Turn another 90 degrees clockwise to the West and get the reading.
2.2. Compass Pointer
from microbit import *
# compass.calibrate() # should be OK without recalibrating
while True:
display.scroll(compass.heading(), delay=60)
needle = ((-compass.heading() + 15) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])
sleep(500)
Tasks
What are the readings for 1 o’clock? Write code to test three values at either end of the range.
What are the readings for 2 o’clock? Write code to test three values at either end of the range.
What are the readings for 11 o’clock? Write code to test three values at either end of the range.
What are the readings for 10 o’clock? Write code to test three values at either end of the range.
What are the readings for 1 o’clock? Write code to test three values at either end of the range.
About 345 to 316
from microbit import *
compass_heading_list = [346, 345, 344, 316, 315, 314]
while True:
for compass_heading in compass_heading_list:
display.scroll(compass_heading, delay=60)
needle = ((-compass_heading + 15) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])
sleep(500)
What are the readings for 2 o’clock? Write code to test three values at either end of the range.
About 315 to 286
from microbit import *
compass_heading_list = [316, 315, 314, 286, 285, 284]
while True:
for compass_heading in compass_heading_list:
display.scroll(compass_heading, delay=60)
needle = ((-compass_heading + 15) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])
sleep(500)
What are the readings for 11 o’clock? Write code to test three values at either end of the range.
About 16 to 45
from microbit import *
compass_heading_list = [14, 15, 16, 44, 45, 46]
while True:
for compass_heading in compass_heading_list:
display.scroll(compass_heading, delay=60)
needle = ((-compass_heading + 15) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])
sleep(500)
What are the readings for 10 o’clock? Write code to test three values at either end of the range.
About 46 to 75
from microbit import *
compass_heading_list = [44, 45, 46, 74, 75, 76]
while True:
for compass_heading in compass_heading_list:
display.scroll(compass_heading, delay=60)
needle = ((-compass_heading + 15) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])
sleep(500)
Exercises
Place a small piece of paper on the ground. Turn so the clock hand points to 0 o’clock and walk 5 paces. Turn so the clock hand points to 4 o’clock and walk 5 paces. Turn so the clock hand points to 8 o’clock and walk 5 paces. Did you end up at your starting point?
Place a small piece of paper on the ground. Turn so the clock hand points to 0 o’clock and walk 5 paces. Turn so the clock hand points to 3 o’clock and walk 5 paces. Turn so the clock hand points to 6 o’clock and walk 5 paces. Turn so the clock hand points to 9 o’clock and walk 5 paces. Did you end up at your starting point?