霍尔模块/Hall¶
内置霍尔传感器,可以测量磁场强度。
获取磁场强度值¶
Hall.
get_magnetic
()¶
返回: 磁场强度值(符号表示方向,绝对值表示强度),范围 -100~100 返回类型: float Examples
# 实时检测磁场强度 from wonderbits import Hall, Display hall1 = Hall() display1 = Display() while True: display1.print(1, 1, hall1.get_magnetic())
校准¶
Hall.
calibrate
(block=True)¶ 校准霍尔传感器注意:校准过程中请确保没有磁性物体靠近模块,否则会导致校准后不准确。校准时,模块指示灯会变为黄色,等待指示灯变蓝说明校准完成了。
参数: block (bool) – 阻塞参数
False: 不阻塞True: 阻塞Examples
# 校准霍尔传感器,并实时检测磁场强度 from wonderbits import Hall, Display hall1 = Hall() display1 = Display() display1.print(1,1,'calibrating..') hall1.calibrate() # 校准传感器,程序将阻塞在这儿一段时间 display1.print(1,1,'calibrated. ') while True: display1.print(1, 1, hall1.get_magnetic())
当检测到磁铁¶
Hall.
when_magnet_detected
(val=2)¶ 当检测到磁铁时,执行被修饰的函数
参数: val (int) – 磁感强度大于val被认为检测到磁铁,才会触发事件。范围:0~10 Examples
# 当hall1检测到障碍物会在display1模块中显示磁场强度 from wonderbits import Hall,Display display1 = Display() hall1 = Hall() @hall1.when_magnet_detected() def run1(val): display1.print(1, 1, val) while True: pass