楼主: 驴友花雕
|
[MP动手做] MicroPython动手做(20)——掌控板之三轴加速度 |
7、声光水平测量仪 #MicroPython动手做(20)——掌控板之三轴加速度 #声光水平测量仪
|
8、简易计步器 在走路时通过串口查看加速度传感器的x、y、z和强度的值,会发现变化最明显的是强度值,因为强度值是综合x、y、z三个方向的值得到的矢量和,任一方向的值发生变化,强度值都会变化。所以我们选择强度值变化作为计步标准。Arduino程序如下:
|
10、使用“摇晃”指令的计步器 #MicroPython动手做(20)——掌控板之三轴加速度 #使用“摇晃”指令的计步器
|
11、可以调节感应灵敏度的计步器 调节变量 j 的条件阕值,即可灵活调整计步器的感应灵敏度,以适应不同情况。 #MicroPython动手做(20)——掌控板之三轴加速度 #可以调节感应灵敏度的计步器
|
12、四方向动态体感灯 #MicroPython动手做(20)——掌控板之三轴加速度 #四方向动态体感灯
|
13、摇出好心情 #MicroPython动手做(20)——掌控板之三轴加速度 #摇出好心情 [mw_shl_code=applescript,false]#MicroPython动手做(20)——掌控板之三轴加速度 #摇出好心情 from mpython import * import time from machine import Timer _is_shaked = _is_thrown = False _last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0 def on_shaked():pass def on_thrown():pass tim11 = Timer(11) def timer11_tick(_): global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown if _is_shaked: _count_shaked += 1 if _count_shaked == 5: _count_shaked = 0 if _is_thrown: _count_thrown += 1 if _count_thrown == 10: _count_thrown = 0 if _count_thrown > 0: return x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z() _is_thrown = (x * x + y * y + z * z < 0.25) if _is_thrown: on_thrown();return if _last_x == 0 and _last_y == 0 and _last_z == 0: _last_x = x; _last_y = y; _last_z = z; return diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z _last_x = x; _last_y = y; _last_z = z if _count_shaked > 0: return _is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1) if _is_shaked: on_shaked() tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick) def on_shaked(): global yao yao = yao + 10 myUI = UI(oled) image_picture = Image() yao = 0 oled.fill(0) oled.DispChar('我们准备了一个小惊喜', 3, 0, 1) oled.DispChar('你想知道是什么吗?', 8, 16, 1) oled.show() time.sleep_ms(1500) while True: oled.fill(0) oled.DispChar('请大力摇晃吧', 30, 8, 1) myUI.ProgressBar(30, 35, 70, 8, yao) oled.show() if yao >= 100: break oled.fill(0) oled.DispChar('祝你每天有个好心情', 10, 16, 1) oled.DispChar('笑脸常开', 40, 32, 1) oled.show() time.sleep_ms(2000) oled.fill(0) oled.blit(image_picture.load('face/4.pbm', 0), 32, 0) oled.show()[/mw_shl_code] |
14、三轴X、Y加速度水平测量仪 #MicroPython动手做(20)——掌控板之三轴加速度 #三轴X、Y加速度水平测量仪 [mw_shl_code=applescript,false]#MicroPython动手做(20)——掌控板之三轴加速度 #三轴X、Y加速度水平测量仪 from mpython import * import framebuf import font.dvsm_12 def display_font(_font, _str, _x, _y, _wrap, _z=0): _start = _x for _c in _str: _d = _font.get_ch(_c) if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1] if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0) oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1], framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y) _x += _d[2] while True: oled.fill(0) oled.vline(64, 0, 64, 1) oled.hline(32, 32, 64, 1) oled.circle(64, 32, 31, 1) oled.circle(64, 32, 18, 1) oled.circle(64, 32, 5, 1) oled.fill_circle(64, 32, 4, 0) x = int(numberMap(accelerometer.get_y(),1,(-1),92,32)) y = int(numberMap(accelerometer.get_x(),1,(-1),2,62)) oled.fill_circle(x, y, 4, 1) oled.DispChar("水平仪", 0, 0, 1) display_font(font.dvsm_12, (str("x:") + str(x - 64)), 92, 40, False) display_font(font.dvsm_12, (str("y:") + str(32 - y)), 92, 52, False) oled.show()[/mw_shl_code] |
© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed