from microbit import *
while True:
reading = accelerometer.get_x()
if reading > 20:
display.show("R")
elif reading < -20:
display.show("L")
else:
display.show("-")
BPI:bit 板载 MPU9250 九轴传感器,I2C 地址为 0x69。
9 轴是 3 个独立的三轴传感器的组合。欲了解该芯片的更多详细信息,请点击这里 MPU 9250 数据表 查看数据表。
BPI-BIT MPU9250 库和使用说明:https://github.com/BPI-STEAM/MPU9250
该模块可以获取板子当前的九轴姿态值,分别是加速度、重力、磁感应强度 (X, Y, Z).
最基本的用法是读取当前 X、Y、Z 值,判断开发板的运动状态。例如,Z 轴加速度略有增加,表示开发板正在沿 Z 轴正方向加速移动。
简单的介绍之后,我们就可以设计一个简单的判断了,比如获取板子的平衡情况,以加速度计加速度模块为例,获取它的X轴的值,就可以得到一个基本值,如果该值大于20则说明它在右边,如果小于20,则说明它在左边,如果介于两者之间,则说明它是平衡的,于是就有了下面的代码,L表示左边,而R表示板子右边,试试吧!
from microbit import *
while True:
reading = accelerometer.get_x()
if reading > 20:
display.show("R")
elif reading < -20:
display.show("L")
else:
display.show("-")
可检测上下、左右、前后、摇晃和自由落体等姿态。以下代码会在开发板正面朝上时显示笑脸,朝下时显示生气表情。
from microbit import *
while True:
gesture = accelerometer.current_gesture()
if gesture == "face up":
display.show(Image.HAPPY)
else:
display.show(Image.ANGRY)