驴友花雕
发表于 2020-5-2 16:58:29
7、声光水平测量仪
#MicroPython动手做(20)——掌控板之三轴加速度
#声光水平测量仪
#MicroPython动手做(20)——掌控板之三轴加速度
#声光水平测量仪
from mpython import *
import time
import music
oled.fill(0)
oled.DispChar('声光水平测量仪', 20, 22, 1)
oled.show()
time.sleep(3)
while True:
xxx = int(((127 - 0) / (1 - (-1))) * (accelerometer.get_y() - (-1)) + 0)
yyy = int(((63 - 0) / (1 - (-1))) * (accelerometer.get_x() - (-1)) + 0)
oled.fill(0)
oled.circle(64, 32, 6, 1)
oled.circle(64, 32, 31, 1)
oled.hline(44, 32, 40, 1)
oled.vline(64, 12, 40, 1)
oled.fill_circle(xxx, yyy, 4, 1)
oled.show()
if xxx == 64 and yyy == 32:
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
music.pitch(294, 500)
music.pitch(494, 500)
else:
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
驴友花雕
发表于 2020-5-2 17:07:43
mPython 图形编程
驴友花雕
发表于 2020-5-2 18:14:36
视频:声光水平测量仪
https://v.youku.com/v_show/id_XNDY1NzUxNDg0OA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dtitle
https://v.youku.com/v_show/id_XNDY1NzUxNDg0OA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dtitle
驴友花雕
发表于 2020-5-2 18:18:32
8、简易计步器
在走路时通过串口查看加速度传感器的x、y、z和强度的值,会发现变化最明显的是强度值,因为强度值是综合x、y、z三个方向的值得到的矢量和,任一方向的值发生变化,强度值都会变化。所以我们选择强度值变化作为计步标准。Arduino程序如下:
<blockquote>//MicroPython动手做(20)——掌控板之三轴加速度
//简易计步器
#include <MPython.h>
// 动态变量
volatile float mind_n_BuShu;
// 主程序开始
void setup() {
mPython.begin();
mind_n_BuShu = 0;
display.setCursor(0, 25);
display.print("计步器:");
}
void loop() {
if (((accelerometer.getStrength())>1500)) {
mind_n_BuShu += 1;
display.setCursor(65, 25);
display.print(" ");
display.setCursor(65, 25);
display.print(mind_n_BuShu);
}
delay(300);
}
驴友花雕
发表于 2020-5-2 19:15:53
Mind+ 图形编程
驴友花雕
发表于 2020-5-3 05:17:16
驴友花雕
发表于 2020-5-3 10:43:45
9、Mind+计步器2
驴友花雕
发表于 2020-5-3 10:49:45
驴友花雕
发表于 2020-5-3 13:20:08
10、使用“摇晃”指令的计步器
#MicroPython动手做(20)——掌控板之三轴加速度
#使用“摇晃”指令的计步器
#MicroPython动手做(20)——掌控板之三轴加速度
#使用“摇晃”指令的计步器
from mpython import *
import framebuf
import font.digiface_30
import time
def on_button_a_down(_):
global a, b
time.sleep_ms(10)
if button_a.value() == 1: return
a = 1
def on_button_b_down(_):
global a, b
time.sleep_ms(10)
if button_b.value() == 1: return
a = 0
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 a, b
if a == 1:
b = b + 1
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: _x = _start; _y += _d
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d, _d, 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d), _d, _d,
framebuf.MONO_HLSB), (_x+int(_d/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
a = 0
b = 0
while True:
if a == 0:
oled.fill(0)
oled.DispChar('计步器', 0, 0, 1)
oled.DispChar('每天1万步,活出好身体', 0, 16, 1)
oled.DispChar('按下A键开始计步', 0, 32, 1)
oled.DispChar('按下B键步数清零', 0, 48, 1)
oled.show()
b = 0
elif a == 1:
oled.fill(0)
oled.DispChar('步数', 0, 15, 1)
display_font(font.digiface_30, (str(b)), 30, 10, False, 2)
oled.show()
if b <= 10000:
oled.fill(0)
oled.DispChar((''.join(])), 0, 48, 1)
oled.show()
else:
oled.fill(0)
oled.DispChar('目标完成,你真棒!', 0, 48, 1)
oled.show()
time.sleep(1)
驴友花雕
发表于 2020-5-3 13:23:57
mPython 图形编程
驴友花雕
发表于 2020-5-3 13:30:20
驴友花雕
发表于 2020-5-7 07:30:02
11、可以调节感应灵敏度的计步器
调节变量 j 的条件阕值,即可灵活调整计步器的感应灵敏度,以适应不同情况。
#MicroPython动手做(20)——掌控板之三轴加速度
#可以调节感应灵敏度的计步器
#MicroPython动手做(20)——掌控板之三轴加速度
#可以调节感应灵敏度的计步器
import math
from mpython import *
import time
import framebuf
import font.digiface_it_42
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: _x = _start; _y += _d
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d, _d, 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d), _d, _d,
framebuf.MONO_HLSB), (_x+int(_d/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d
k = 0
while True:
j = math.sqrt(accelerometer.get_x() * accelerometer.get_x() + (accelerometer.get_y() * accelerometer.get_y() + accelerometer.get_z() * accelerometer.get_z()))
if j >= 1.2:
k = k + 1
rgb = (int(0), int(51), int(0))
rgb.write()
time.sleep_ms(1)
oled.fill(0)
display_font(font.digiface_it_42, (str(k)), 4, 10, False, 2)
oled.DispChar('步', 113, 48, 1)
oled.show()
驴友花雕
发表于 2020-5-7 07:42:21
mPython 图形编程
驴友花雕
发表于 2020-5-7 07:58:48
驴友花雕
发表于 2020-5-7 09:48:06
12、四方向动态体感灯
#MicroPython动手做(20)——掌控板之三轴加速度
#四方向动态体感灯
#MicroPython动手做(20)——掌控板之三轴加速度
#四方向动态体感灯
from mpython import *
import time
while True:
oled.fill(0)
oled.DispChar("掌控体感灯", 36, 32, 1)
oled.show()
if accelerometer.get_x() < -0.3:
oled.fill(0)
oled.DispChar("向前倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(153), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_x() > 0.3:
oled.fill(0)
oled.DispChar("向后倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(51), int(51), int(255)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_y() > 0.3:
oled.fill(0)
oled.DispChar("向左倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(0), int(153), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
if accelerometer.get_y() < -0.3:
oled.fill(0)
oled.DispChar("向右倾斜", 38, 16, 1)
oled.show()
rgb.fill((int(204), int(153), int(51)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(1000)
驴友花雕
发表于 2020-5-7 09:57:14
mPython X 图形编程
驴友花雕
发表于 2020-5-7 10:45:00
驴友花雕
发表于 2020-5-8 09:18:53
13、摇出好心情
#MicroPython动手做(20)——掌控板之三轴加速度
#摇出好心情
#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()
驴友花雕
发表于 2020-5-8 09:23:55
mPython 图形编程
驴友花雕
发表于 2020-5-8 09:55:07
#MicroPython动手做(20)——掌控板之三轴加速度
#摇出好心情(演示视频)
https://v.youku.com/v_show/id_XNDY2NDE3ODIwMA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dtitle
https://v.youku.com/v_show/id_XNDY2NDE3ODIwMA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dtitle
驴友花雕
发表于 2020-5-8 11:30:54
14、三轴X、Y加速度水平测量仪
#MicroPython动手做(20)——掌控板之三轴加速度
#三轴X、Y加速度水平测量仪
#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: _x = _start; _y += _d
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d, _d, 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d), _d, _d,
framebuf.MONO_HLSB), (_x+int(_d/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d
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()