“四按键”的版本间的差异

来自Labplus盛思维基百科
跳转至: 导航搜索
图形化示例
第52行: 第52行:
 
}
 
}
 
</pre>
 
</pre>
=== MicroPython示例 ===
+
=== Python示例 ===
 
<pre style="color:blue">
 
<pre style="color:blue">
//程序功能:按下不同按键,播放不同音调//
 
 
from microbit import *
 
from microbit import *
import music
 
 
bool get4ButtonVal(int index, int pin):
 
  int val = analogRead(pin)
 
  int btn = 0
 
  if(val < 101) btn = 1
 
  else if(val > 199 && val < 301) btn = 2
 
  else if(val > 449 && val < 551) btn = 3
 
  else if(val > 699 && val < 801) btn = 4
 
  return btn == index
 
  
 +
def get4ButtonVal(pin):
 +
    val=pin.read_analog()
 +
    btn=None
 +
    if val<51:
 +
        btn='A'
 +
    elif val>199 and val<301:
 +
        btn='B'
 +
    elif val>449 and val<551:
 +
        btn='C'
 +
    elif val>699 and val<801:
 +
        btn='D'
 +
    return btn
  
 
while True:
 
while True:
  if get4ButtonVal(2, 0):
+
     print(get4ButtonVal(pin0))
    music.pitch(200, 1000, pin0)
 
  if get4ButtonVal(3, 0):
 
     music.pitch(400, 1000, pin0)
 
  if get4ButtonVal(4, 0):
 
    music.pitch(600, 1000, pin0)
 
  if get4ButtonVal(1, 0):
 
music.pitch(800, 1000, pin0)
 
 
</pre>
 
</pre>
  

2018年5月25日 (五) 08:28的版本

黑色传感器最终版12.20-14.png

概述

集成4个按压式按键,只需1个引脚资源便可获取4个按键状态,节省主控I/O资源。当某个按键按下时,AO输出相应的模拟量。

技术参数

  • 工作电压:VCC 3.3-5V
  • 集成4个按键,只需1引脚资源
  • 模块尺寸:24x46x7.5mm

引脚定义

VCC 电源
NC 空脚
AO 模拟输出
GND

使用教程

AO输出模拟量0~1023,可通过analog Read,检测4按键状态。

按键状态 AO模拟量
4个按键都不按 >1000
按下A按键 0-50
按下B按键 200-300
按下C按键 450-550
按下D按键 700-800

Arduino示例

//程序功能:读取四按键当前的采样值//
int fourbottonPin =A0;
int val;
void setup() {
  Serial.begin(9600);  
  pinMode(fourbottonPin, INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  val=analogRead(fourbottonPin);
  Serial.println(val);
}

Python示例

from microbit import *

def get4ButtonVal(pin):
    val=pin.read_analog()
    btn=None
    if val<51:
        btn='A'
    elif val>199 and val<301:
        btn='B'
    elif val>449 and val<551:
        btn='C'
    elif val>699 and val<801:
        btn='D'
    return btn 

while True:
    print(get4ButtonVal(pin0))

图形化示例

程序功能:按下A键时LED1会亮,按下B键LED1灭;按下C键时LED2亮,按下D键时LED2会灭
四位键 1.png

版本历史记录

Version Date Note [+]新增[-]删除[^]修复
V2.0