方向
micro:bit 上有一块指南针。若你做一个气象站,可以用它判断风向。
指南针
它还能告诉你北方在哪,像这样:
from bibo import *
compass.calibrate()
while True:
needle = ((15 - compass.heading()) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])
程序读取 compass.heading,再用一点简单却巧妙的数学——整除 // 和取模 %——算出该用第几根「钟针」图像来大致指向北方。