2018-12-1 09:31:52 [显示全部楼层]
3710浏览
查看: 3710|回复: 1

[入门教程] 【掌控】mpython_一起爱上掌控(new)

[复制链接]
这是新写的教程,因为有了music库,比用频率来使蜂鸣器奏乐,好用了许多。


【学习目标】
1、学习掌控的触摸按键
2、学习循环积木和条件
3、完成小项目,一起爱上掌控


【触摸按键】
掌控板板载6个触摸焊盘,依次从左到右分别touchPad_P、touchPad_Y、touchPad_T、touchPad_H、touchPad_O、touchPad_N。

这6个电容触摸按键合起来是什么单词。
PYTHON
就是python。

【Python 简介】
Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。
Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结构。
  • Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节。类似于PHP和Perl语言。
  • Python 是交互式语言: 这意味着,您可以在一个Python提示符,直接互动执行写你的程序。
  • Python 是面向对象语言: 这意味着Python支持面向对象的风格或代码封装在对象的编程技术。
  • Python 是初学者的语言:Python 对初级程序员而言,是一种伟大的语言,它支持广泛的应用程序开发,从简单的文字处理到 WWW 浏览器再到游戏。

【触摸按键电容值读取】
写下面程序下载到掌控中


  1. from mpython import *
  2. display.fill(0)
  3. display.DispChar('触摸按钮电平读取',16,16)
  4. display.show()
  5. while 1:
  6.     display.fill(0)
  7.     display.DispChar((str(touchPad_P.read())),16,16)
  8.     display.show()
复制代码

没有触摸时读数为________.
用手指触摸P,读数为_______.
你打算将触摸时的阈值设为_________.

【一起爱上掌控】
参考下面程序完成任务,要求开机时清屏,然后触摸p时,显示‘一’,触摸Y时显示‘起’,依次完成全部程序,分别显示‘一起爱上掌控’,重复执行。
请注意计算字符显示的坐标位置,如果下载后有误,重新修改后下载。

  1. from mpython import *
  2. display.fill(0)
  3. while 1:
  4.     if touchPad_P.read() < 200:
  5.         display.DispChar('一',16,16)
  6.         display.show()
  7.     elif touchPad_Y.read() < 200:
  8.         display.DispChar('起',32,16)
  9.         display.show()
  10.     elif touchPad_T.read() < 200:
  11.         display.DispChar('爱',48,16)
  12.         display.show()
  13.     elif touchPad_H.read() < 200:
  14.         display.DispChar('上',64,16)
  15.         display.show()
  16.     elif touchPad_O.read() < 200:
  17.         display.DispChar('掌',80,16)
  18.         display.show()
  19.     elif touchPad_N.read() < 200:
  20.         display.DispChar('控',96,16)
  21.         display.show()
复制代码

【音符响起】
导入音乐库
掌控的板载蜂鸣,可以演奏出音符来。

例如:music.play(['C4:4'])  中音 do 1拍

  1. from mpython import *
  2. import music
  3. display.fill(0)
  4. while 1:
  5.     if touchPad_P.read() < 200:
  6.         display.DispChar('一',16,16)
  7.         display.show()
  8.         music.play(['C4:4'])
  9.     elif touchPad_Y.read() < 200:
  10.         display.DispChar('起',32,16)
  11.         display.show()
  12.         music.play(['D4:4'])
  13.     elif touchPad_T.read() < 200:
  14.         display.DispChar('爱',48,16)
  15.         display.show()
  16.         music.play(['E4:4'])
  17.     elif touchPad_H.read() < 200:
  18.         display.DispChar('上',64,16)
  19.         display.show()
  20.         music.play(['F4:4'])
  21.     elif touchPad_O.read() < 200:
  22.         display.DispChar('掌',80,16)
  23.         display.show()
  24.         music.play(['G4:4'])
  25.     elif touchPad_N.read() < 200:
  26.         display.DispChar('控',96,16)
  27.         display.show()
  28.         music.play(['A4:4'])
复制代码

【边弹边亮灯】
  1. from mpython import *
  2. import music
  3. display.fill(0)
  4. while 1:
  5.     if touchPad_P.read() < 200:
  6.         display.DispChar('一',16,16)
  7.         display.show()
  8.         rgb.fill((255, 0, 0))
  9.         rgb.write()
  10.         music.play(['C4:4'])        
  11.     elif touchPad_Y.read() < 200:
  12.         display.DispChar('起',32,16)
  13.         display.show()
  14.         rgb.fill((255, 255, 0))
  15.         rgb.write()
  16.         music.play(['D4:4'])
  17.     elif touchPad_T.read() < 200:
  18.         display.DispChar('爱',48,16)
  19.         display.show()
  20.         rgb.fill((255, 0, 255))
  21.         rgb.write()
  22.         music.play(['E4:4'])
  23.     elif touchPad_H.read() < 200:
  24.         display.DispChar('上',64,16)
  25.         display.show()
  26.         rgb.fill((0, 255, 255))
  27.         rgb.write()
  28.         music.play(['F4:4'])
  29.     elif touchPad_O.read() < 200:
  30.         display.DispChar('掌',80,16)
  31.         display.show()
  32.         rgb.fill((120, 126, 0))
  33.         rgb.write()
  34.         music.play(['G4:4'])
  35.     elif touchPad_N.read() < 200:
  36.         display.DispChar('控',96,16)
  37.         display.show()
  38.         rgb.fill((0, 60, 126))
  39.         rgb.write()
  40.         music.play(['A4:4'])
复制代码

注意设置完RGB灯颜色后要加上一个RGB显示代码rgb.write()。
【小练习】
1、如果把灯光颜色改成随机,会什么样?
2、自己有什么新想法,可以尝试下,和小伙伴交流。

rzyzzxw  版主
 楼主|

发表于 2018-12-27 10:12:55

  1. from mpython import *
  2. oled.fill(0)
  3. while True:
  4.     if touchPad_P.read() < 200:
  5.         oled.DispChar('一',16,16)
  6.         oled.show()
  7.     elif touchPad_Y.read() < 200:
  8.         oled.DispChar('起',32,16)
  9.         oled.show()
  10.     elif touchPad_T.read() < 200:
  11.         oled.DispChar('爱',48,16)
  12.         oled.show()
  13.     elif touchPad_H.read() < 200:
  14.         oled.DispChar('上',64,16)
  15.         oled.show()
  16.     elif touchPad_O.read() < 200:
  17.         oled.DispChar('掌',80,16)
  18.         oled.show()
  19.     elif touchPad_N.read() < 200:
  20.         oled.DispChar('控',96,16)
  21.         oled.show()
复制代码


新版库中改用oled取代display.
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail