compass

from microbit import *

#Please turn microbit in all directions to accomplish calibration without interruption,nor re-download.
#Please accomplish calibration operations or plug and play USB once more if you need to burn the program again. 

compass.calibrate()

while True:
  needle = ((15 - compass.heading()) // 30) % 12
  display.show(Image.ALL_CLOCKS[needle])

这是一个指南针程序。

第六行:校准,将microbit倾斜,直到在显示屏的外边缘上画出一圈像素。

第八行到第十行:采用compass.heading()和一些数字进行计算,取整://和取模:%,计算出在屏幕上显示的时钟指针的数量,这样它就可以大概的指出北方。

注意:

** 在使用指南针前必须校准。否则不能进行任何其他操作。该calibration()是一个有趣的小游戏,以帮助设备在与地球磁场相关的地方工作。**