2018-10-4 20:30:14 [显示全部楼层]
4847浏览
查看: 4847|回复: 4

[入门教程] 【掌控】mpython-10…你是创客,为你鼓掌--布尔声控灯

[复制链接]
你是创客,为你鼓掌。
【掌控】mpython-10…你是创客,为你鼓掌--布尔声控灯图2
掌声响起来
我心更明白
你在创新中国的路上勇往直前



【1】显示板载声音传感器的值
程序:
  1. from mpython import *
  2. import time
  3. while True:
  4.     display.fill(0)
  5.     display.DispChar('声音:',0,16)
  6.     display.text("%d" % (sound.read()),40,20)
  7.     display.show()
  8.     #time.sleep_ms(10)
复制代码


刷入掌控。运行。


【2】你是创客,为你鼓掌
程序:
  1. from mpython import *
  2. import time
  3. a=0
  4. while True:
  5.     display.fill(0)     
  6.     display.DispChar('声音:',0,16)
  7.     display.text("%d" % (sound.read()),40,20)
  8.     display.show()
  9.     if sound.read() > 10:
  10.         a = a + 1
  11.         if a == 1:
  12.             rgb[0] = (255,0,0)    # 设置红色
  13.             rgb[1] = (0,255,0)  # 设定为绿色
  14.             rgb[2] = (0,0,255)   # 设置为蓝色
  15.             rgb.write()
  16.         if a == 2:
  17.             rgb[0] = (0,0,0)   
  18.             rgb[1] = (0,0,0)  
  19.             rgb[2] = (0,0,0)   
  20.             rgb.write()
  21.             a = 0
  22.     time.sleep_ms(200)   
复制代码


#延时一下很有必要的。
【3】布尔声控灯
还记得狄老师的《布尔的小夜灯》,用布尔变量来开关灯,所以今天的最终目标是学布尔用布尔变量做声控灯。
【掌控】mpython-10…你是创客,为你鼓掌--布尔声控灯图3

用mpython定代码如下:
  1. from mpython import *
  2. import time
  3. state = True
  4. while True:
  5.     display.fill(0)     
  6.     display.DispChar('声音:',0,16)
  7.     display.text("%d" % (sound.read()),40,20)
  8.     display.show()
  9.     if sound.read() > 10:
  10.         if state == True:
  11.             rgb[0] = (255,0,0)    # 设置红色
  12.             rgb[1] = (0,255,0)  # 设定为绿色
  13.             rgb[2] = (0,0,255)   # 设置为蓝色
  14.             rgb.write()
  15.             state = not state
  16.         else:
  17.             rgb[0] = (0,0,0)   
  18.             rgb[1] = (0,0,0)  
  19.             rgb[2] = (0,0,0)   
  20.             rgb.write()
  21.             state = not state
  22.     time.sleep_ms(200)   
复制代码


这段代码是在带孩子外公园玩时写的哈,孩子们去玩,我写代码;P。测试过了,公园噪声大,不录视频了。
【掌控】mpython-10…你是创客,为你鼓掌--布尔声控灯图4

学习感悟:

python确实是很好上手的代码语言。
可以轻松从图形化过渡过来。
它严谨的格式,各种库和函数,我们好好学习。
以及高深的算法,向我们更向往。

http://www.runoob.com/python/python-tutorial.html

【掌控】mpython-10…你是创客,为你鼓掌--布尔声控灯图1

rzegkly  版主

发表于 2018-10-5 10:11:44

掌控版的麦克风的效果很好,(声音读取函数:sound.read()),40,20)?
回复

使用道具 举报

rzyzzxw  版主
 楼主|

发表于 2018-10-5 10:26:31

本帖最后由 rzyzzxw 于 2018-10-5 15:05 编辑

建议上课时改成吹气哈,效果很好的,如果一片掌声,校长都要引来了。如果是用吹的,建议延时再长些。
回复

使用道具 举报

rzyzzxw  版主
 楼主|

发表于 2018-12-28 11:03:18

  1. from mpython import *
  2. import time
  3. state = True
  4. while True:
  5.     oled.fill(0)     
  6.     oled.DispChar('声音:',0,16)
  7.     oled.text("%d" % (sound.read()),40,20)
  8.     oled.show()
  9.     if sound.read() > 100:
  10.         if state == True:
  11.             rgb[0] = (255,0,0)    # 设置红色
  12.             rgb[1] = (0,255,0)  # 设定为绿色
  13.             rgb[2] = (0,0,255)   # 设置为蓝色
  14.             rgb.write()
  15.             state = not state
  16.         else:
  17.             rgb.fill((0, 0, 0))  
  18.             rgb.write()
  19.             state = not state
  20.     time.sleep_ms(400)
复制代码

回复

使用道具 举报

rzyzzxw  版主
 楼主|

发表于 2018-12-28 11:04:51

  1. from mpython import *
  2. import time
  3. state = True
  4. while True:
  5.     oled.fill(0)     
  6.     oled.DispChar('声音:',0,16)
  7.     oled.text("%d" % (sound.read()),40,20)
  8.     oled.show()
  9.     if sound.read() > 100:
  10.         if state == True:
  11.             rgb[0] = (255,0,0)    # 设置红色
  12.             rgb[1] = (0,255,0)  # 设定为绿色
  13.             rgb[2] = (0,0,255)   # 设置为蓝色
  14.             rgb.write()
  15.             state = False
  16.         else:
  17.             rgb.fill((0, 0, 0))  
  18.             rgb.write()
  19.             state = True
  20.     time.sleep_ms(400)
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail