“(SKU:ELB030639)AD Keyboard 模拟键盘模块”的版本间的差异
来自YwRobot Studio Wiki
YWrobot WB1(讨论 | 贡献) |
(→样例代码) |
||
(未显示另一用户的1个中间版本) | |||
第22行: | 第22行: | ||
==使用教程== | ==使用教程== | ||
[[File:AD Keyboard 模拟键盘模块LINE.jpg|500px|]] | [[File:AD Keyboard 模拟键盘模块LINE.jpg|500px|]] | ||
− | + | [[File:AD Keyboard 模拟键盘模块2LINE.jpg|500px|]] | |
===样例代码=== | ===样例代码=== | ||
<pre style="color:blue"> | <pre style="color:blue"> | ||
+ | #define LEDR 9 | ||
+ | #define LEDG 10 | ||
+ | #define LEDB 11 | ||
+ | |||
+ | int adc_key_val[5] = {30, 150, 360, 535, 760 }; | ||
+ | int NUM_KEYS = 5; | ||
+ | int adc_key_in; | ||
+ | int key = -1; | ||
+ | int oldkey = -1; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | pinMode(LEDG, OUTPUT); | ||
+ | pinMode(LEDB, OUTPUT); | ||
+ | pinMode(LEDR, OUTPUT); | ||
+ | } | ||
+ | void loop() { | ||
+ | adc_key_in = analogRead(0); // read the value from the sensor | ||
+ | key = get_key(adc_key_in); // convert into key press | ||
+ | if (key != oldkey) { // if keypress is detected | ||
+ | delay(50); // wait for debounce time | ||
+ | adc_key_in = analogRead(0); // read the value from the sensor | ||
+ | key = get_key(adc_key_in); // convert into key press | ||
+ | if (key != oldkey) { | ||
+ | oldkey = key; | ||
+ | if (key >= 0) { | ||
+ | analogWrite(LEDR, 0); analogWrite(LEDG, 0); analogWrite(LEDB, 0); | ||
+ | switch (key) | ||
+ | { | ||
+ | case 0: { | ||
+ | analogWrite(LEDR, 100);analogWrite(LEDG, 100); | ||
+ | } | ||
+ | break; | ||
+ | case 1: { | ||
+ | analogWrite(LEDR, 100); | ||
+ | } | ||
+ | break; | ||
+ | case 2: { | ||
+ | analogWrite(LEDB, 100); | ||
+ | } | ||
+ | break; | ||
+ | case 3: { | ||
+ | analogWrite(LEDG, 100); | ||
+ | } | ||
+ | break; | ||
+ | case 4: { | ||
+ | analogWrite(LEDR, 100); analogWrite(LEDG, 100);analogWrite(LEDB, 100); | ||
+ | } | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | delay(10); | ||
+ | } | ||
+ | // Convert ADC value to key number | ||
+ | int get_key(unsigned int input) { | ||
+ | int k; | ||
+ | for (k = 0; k < NUM_KEYS; k++) { | ||
+ | if (input < adc_key_val[k]) { | ||
+ | return k; | ||
+ | } | ||
+ | } | ||
+ | if (k >= NUM_KEYS)k = -1; // No valid key pressed | ||
+ | return k; | ||
+ | } | ||
</pre> | </pre> |
2017年1月11日 (三) 17:16的最新版本
产品参数
- 尺寸:69*34mm
- 电压:5V
- 颜色:五色键帽,红黄绿蓝白各一个
- 输出:模拟量电压
- 接口:Arduino 3P接口 S V G
- 平台:Arduino
产品特性
- 5个按键仅占用一个模拟量接口,节省资源
- 模拟量输出,按下不同的按键会有不同的电压值输出,可通过Arduino转换识别
- 采用欧姆龙按键,可以方便更换其它样式的键帽
- 兼容Arduino接口,可直接配合传感器扩展板使用
使用教程
样例代码
#define LEDR 9 #define LEDG 10 #define LEDB 11 int adc_key_val[5] = {30, 150, 360, 535, 760 }; int NUM_KEYS = 5; int adc_key_in; int key = -1; int oldkey = -1; void setup() { pinMode(LEDG, OUTPUT); pinMode(LEDB, OUTPUT); pinMode(LEDR, OUTPUT); } void loop() { adc_key_in = analogRead(0); // read the value from the sensor key = get_key(adc_key_in); // convert into key press if (key != oldkey) { // if keypress is detected delay(50); // wait for debounce time adc_key_in = analogRead(0); // read the value from the sensor key = get_key(adc_key_in); // convert into key press if (key != oldkey) { oldkey = key; if (key >= 0) { analogWrite(LEDR, 0); analogWrite(LEDG, 0); analogWrite(LEDB, 0); switch (key) { case 0: { analogWrite(LEDR, 100);analogWrite(LEDG, 100); } break; case 1: { analogWrite(LEDR, 100); } break; case 2: { analogWrite(LEDB, 100); } break; case 3: { analogWrite(LEDG, 100); } break; case 4: { analogWrite(LEDR, 100); analogWrite(LEDG, 100);analogWrite(LEDB, 100); } break; } } } } delay(10); } // Convert ADC value to key number int get_key(unsigned int input) { int k; for (k = 0; k < NUM_KEYS; k++) { if (input < adc_key_val[k]) { return k; } } if (k >= NUM_KEYS)k = -1; // No valid key pressed return k; }
更多
[YWRobot产品资料下载]
购买 YWRobot商城购买链接 AD Keyboard 模拟键盘模块
YWRobot商城购买链接AD Keyboard5位按键模拟键盘模块