MicroPython动手做(17)——掌控板之触摸引脚-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

MicroPython动手做(17)——掌控板之触摸引脚

[复制链接]
 楼主| 发表于 2020-4-25 15:47 | 显示全部楼层
6、OLED屏幕同时显示每个触摸引脚的值

[mw_shl_code=arduino,true]#MicroPython动手做(17)——掌控板之触摸引脚
#OLED屏幕同时显示每个触摸引脚的值

from mpython import *

while True:
    oled.fill(0)
    oled.DispChar((str("P") + str(touchPad_P.read())), 0, 0, 1)
    oled.DispChar((str("Y") + str(touchPad_Y.read())), 93, 0, 1)
    oled.DispChar((str("T") + str(touchPad_T.read())), 0, 16, 1)
    oled.DispChar((str("H") + str(touchPad_H.read())), 93, 16, 1)
    oled.DispChar((str("O") + str(touchPad_O.read())), 0, 32, 1)
    oled.DispChar((str("N") + str(touchPad_N.read())), 93, 32, 1)
    oled.show()[/mw_shl_code]
 楼主| 发表于 2020-4-25 15:49 | 显示全部楼层
mPython X 图形编程

10.jpg
 楼主| 发表于 2020-4-25 16:43 | 显示全部楼层
16.gif
 楼主| 发表于 2020-4-25 17:56 | 显示全部楼层
7、6个触摸按键控制RGB灯并显示按键名

[mw_shl_code=arduino,true]#MicroPython动手做(17)——掌控板之触摸引脚
#6个触摸按键控制RGB灯并显示按键名

from mpython import *

from machine import Timer

import time

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
        touchPad_P.read();pass
    except:
        return
    if touchPad_P.read() < 400:
        if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
        if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
        if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
        if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
        if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
        if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_P_pressed():
    global i
    oled.fill(0)
    oled.DispChar('P', 60, 22, 1)
    oled.show()
    rgb[0] = (int(255), int(0), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_H_pressed():
    global i
    oled.fill(0)
    oled.DispChar('H', 60, 22, 1)
    oled.show()
    rgb[0] = (int(255), int(102), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_Y_pressed():
    global i
    oled.fill(0)
    oled.DispChar('Y', 60, 22, 1)
    oled.show()
    rgb[1] = (int(0), int(153), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_O_pressed():
    global i
    oled.fill(0)
    oled.DispChar('O', 60, 22, 1)
    oled.show()
    rgb[0] = (int(255), int(102), int(0))
    rgb.write()
    time.sleep_ms(1)
    rgb[1] = (int(255), int(102), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_T_pressed():
    global i
    oled.fill(0)
    oled.DispChar('T', 60, 22, 1)
    oled.show()
    rgb[2] = (int(51), int(51), int(255))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_N_pressed():
    global i
    oled.fill(0)
    oled.DispChar('N', 60, 22, 1)
    oled.show()
    rgb.fill((int(255), int(102), int(0)))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)[/mw_shl_code]
 楼主| 发表于 2020-4-25 17:58 | 显示全部楼层
mPython 图形编程


17.jpg
 楼主| 发表于 2020-4-25 19:26 | 显示全部楼层
8、简易触摸按键电子琴(6键)

00.jpg
 楼主| 发表于 2020-4-25 19:28 | 显示全部楼层
[mw_shl_code=arduino,true]#MicroPython动手做(17)——掌控板之触摸引脚
#简易触摸按键电子琴(6键)

from mpython import *

import music
while True:
    music.stop()
    if touchPad_P.read() < 400:
        music.pitch(262, 500)
    else:
        if touchPad_Y.read() < 400:
            music.pitch(294, 500)
        else:
            if touchPad_T.read() < 400:
                music.pitch(330, 500)
            else:
                if touchPad_H.read() < 400:
                    music.pitch(349, 500)
                else:
                    if touchPad_O.read() < 400:
                        music.pitch(392, 500)
                    else:
                        if touchPad_N.read() < 400:
                            music.pitch(440, 500)
[/mw_shl_code]
 楼主| 发表于 2020-4-25 19:33 | 显示全部楼层
mPython 图形编程
24.jpg
 楼主| 发表于 2020-4-25 20:08 | 显示全部楼层
视频:掌控板模拟简易触摸按键电子琴(6键)

https://v.youku.com/v_show/id_XN ... 5~5!2~5!2~5!2~5~5~A



 楼主| 发表于 2020-4-29 14:43 | 显示全部楼层
9、触摸按键点播六首曲子

[mw_shl_code=arduino,true]#MicroPython动手做(17)——掌控板之触摸引脚
# 触摸按键点播六首曲子

from mpython import *
import time
import music
from machine import Timer

def on_button_a_down(_):
    time.sleep_ms(10)
    if button_a.value() == 1: return
    music.stop()

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
        touchPad_P.read();pass
    except:
        return
    if touchPad_P.read() < 400:
        if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
        if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
        if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
        if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
        if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
        if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_P_pressed():
    music.play(music.DONG_FANG_HONG, wait=False, loop=False)

def on_touchpad_Y_pressed():
    music.play(music.BIRTHDAY, wait=False, loop=False)

def on_touchpad_T_pressed():
    music.play(music.MO_LI_HUA, wait=False, loop=False)

def on_touchpad_H_pressed():
    music.play(music.ODE, wait=False, loop=False)

def on_touchpad_O_pressed():
    music.play(music.PRELUDE, wait=False, loop=False)

def on_touchpad_N_pressed():
    music.play(music.CAI_YUN_ZHUI_YUE, wait=False, loop=False)

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)


while True:
    oled.fill(0)
    oled.DispChar("A键:停止", 35, 0, 1)
    oled.DispChar("P:东方红  Y:生日快乐", 6, 20, 1)
    oled.DispChar("T:茉莉花  H:欢乐颂", 11, 35, 1)
    oled.DispChar("O:婚宴 N:彩云追月", 13, 50, 1)
    oled.show()[/mw_shl_code]

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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-27 09:32 , Processed in 0.119620 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表