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

2018-10-049052
AI 快速预览详细 收起
本文介绍了如何使用mPython-10开发板制作声控灯项目。首先展示了如何显示板载声音传感器的值,然后通过布尔变量控制RGB灯的开关状态。该项目适合初学者和家长陪孩子一起动手实践,激发孩子的创造力和编程兴趣。
你是创客,为你鼓掌。
【掌控】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

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(4)
rzyzzxw
rzyzzxw
作者
[code]from mpython import *
import time
state = True
while True:
oled.fill(0)
oled.DispChar('声音:',0,16)
oled.text("%d" % (sound.read()),40,20)
oled.show()
if sound.read() > 100:
if state == True:
rgb[0] = (255,0,0) # 设置红色
rgb[1] = (0,255,0) # 设定为绿色
rgb[2] = (0,0,255) # 设置为蓝色
rgb.write()
state = False
else:
rgb.fill((0, 0, 0))
rgb.write()
state = True
time.sleep_ms(400)[/code]
rzyzzxw
rzyzzxw
作者
[code]from mpython import *
import time
state = True
while True:
oled.fill(0)
oled.DispChar('声音:',0,16)
oled.text("%d" % (sound.read()),40,20)
oled.show()
if sound.read() > 100:
if state == True:
rgb[0] = (255,0,0) # 设置红色
rgb[1] = (0,255,0) # 设定为绿色
rgb[2] = (0,0,255) # 设置为蓝色
rgb.write()
state = not state
else:
rgb.fill((0, 0, 0))
rgb.write()
state = not state
time.sleep_ms(400)[/code]
rzyzzxw
rzyzzxw
作者
建议上课时改成吹气哈,效果很好的,如果一片掌声,校长都要引来了。如果是用吹的,建议延时再长些。
rzegkly
rzegkly
掌控版的麦克风的效果很好,(声音读取函数:sound.read()),40,20)?
- 没有更多了 -