“SKU:RB-02S159 模拟键盘模块”的版本间的差异
来自ALSROBOT WiKi
(→产品概述) |
(→资料下载) |
||
(未显示1个用户的4个中间版本) | |||
第5行: | 第5行: | ||
==产品参数== | ==产品参数== | ||
===基本参数=== | ===基本参数=== | ||
− | + | #品名:模拟按键模块 | |
+ | #货号:RB-02S159 | ||
+ | #尺寸:55*40mm | ||
+ | #固定孔:M3*4 | ||
===电气性能=== | ===电气性能=== | ||
+ | 1.工作电压:3.3V~5V<br/> | ||
+ | 2.工作温度限值:-20℃ ~ +70℃<br/> | ||
+ | 3.信号类型:模拟信号<br/> | ||
+ | 4.接口类型:3P 防插反接口<br/> | ||
+ | 5.工作电流:小于等于20mA,5V 供电下<br/> | ||
+ | 6.按键及指示灯:S1 ~ S5 五个按键旁分别配有指示灯,按下时亮起<br/> | ||
+ | 7.按键模拟值:<br/> | ||
+ | :S1=380±40 | ||
+ | :S2=470±40 | ||
+ | :S3=560±40 | ||
+ | :S4=680±40 | ||
+ | :S5=840±40 | ||
+ | 8.引脚定义:<br/> | ||
+ | :+:电源正极 | ||
+ | :-:电源负极 | ||
+ | :S:信号 | ||
==使用方法== | ==使用方法== | ||
第213行: | 第232行: | ||
* 示例程序 | * 示例程序 | ||
− | 接收端<br/> | + | '''接收端'''<br/> |
+ | <pre style='color:blue'> | ||
+ | #define PWA 3 | ||
+ | #define PWB 11 | ||
+ | #define DIRA 12 | ||
+ | #define DIRB 13 | ||
+ | #define BRAKEA 9 | ||
+ | #define BRAKEB 8 | ||
+ | void setup(){ | ||
+ | Serial.begin(9600); | ||
+ | pinMode(PWA,OUTPUT); | ||
+ | pinMode(PWB,OUTPUT); | ||
+ | pinMode(DIRA,OUTPUT); | ||
+ | pinMode(DIRB,OUTPUT); | ||
+ | pinMode(BRAKEA,OUTPUT); | ||
+ | pinMode(BRAKEB,OUTPUT); | ||
+ | } | ||
− | + | void loop(){ | |
+ | if(Serial.available()>0) | ||
+ | { | ||
+ | char val = Serial.read(); | ||
+ | Serial.println(val); | ||
+ | if(val == 'r') | ||
+ | right(); | ||
+ | if(val == 'l') | ||
+ | left(); | ||
+ | if(val == 'h') | ||
+ | head(); | ||
+ | if(val == 'b') | ||
+ | back(); | ||
+ | if(val == 's') | ||
+ | stopt(); | ||
+ | } | ||
+ | } | ||
+ | void head(){ | ||
+ | analogWrite(PWA,180); | ||
+ | analogWrite(PWB,180); | ||
+ | digitalWrite(DIRA,HIGH); | ||
+ | digitalWrite(BRAKEA,LOW); | ||
+ | digitalWrite(DIRB,HIGH); | ||
+ | digitalWrite(BRAKEB,LOW); | ||
+ | } | ||
+ | |||
+ | void back(){ | ||
+ | analogWrite(PWA,180); | ||
+ | analogWrite(PWB,180); | ||
+ | digitalWrite(DIRA,LOW); | ||
+ | digitalWrite(BRAKEA,LOW); | ||
+ | digitalWrite(DIRB,LOW); | ||
+ | digitalWrite(BRAKEB,LOW); | ||
+ | } | ||
+ | |||
+ | void left(){ | ||
+ | analogWrite(PWA,180); | ||
+ | analogWrite(PWB,180); | ||
+ | digitalWrite(DIRA,HIGH); | ||
+ | digitalWrite(BRAKEA,LOW); | ||
+ | digitalWrite(DIRB,LOW); | ||
+ | digitalWrite(BRAKEB,LOW); | ||
+ | } | ||
+ | |||
+ | void right(){ | ||
+ | analogWrite(PWA,180); | ||
+ | analogWrite(PWB,180); | ||
+ | digitalWrite(DIRA,LOW); | ||
+ | digitalWrite(BRAKEA,LOW); | ||
+ | digitalWrite(DIRB,HIGH); | ||
+ | digitalWrite(BRAKEB,LOW); | ||
+ | } | ||
+ | |||
+ | void stopt(){ | ||
+ | analogWrite(PWA,0); | ||
+ | analogWrite(PWB,0); | ||
+ | digitalWrite(DIRA,LOW); | ||
+ | digitalWrite(BRAKEA,HIGH); | ||
+ | digitalWrite(DIRB,LOW); | ||
+ | digitalWrite(BRAKEB,HIGH); | ||
+ | } | ||
+ | </pre> | ||
+ | '''发射端'''<br/> | ||
+ | <pre style='color:blue'> | ||
+ | int adc_key_val[5] ={400,470,560,700,900 }; | ||
+ | int NUM_KEYS = 5; | ||
+ | int adc_key_in; | ||
+ | int key=-1; | ||
+ | int get_key(unsigned int input) | ||
+ | { | ||
+ | int k; | ||
+ | for (k = 0; k < NUM_KEYS; k++) | ||
+ | { | ||
+ | while (input < adc_key_val[k]) | ||
+ | { | ||
+ | return k; | ||
+ | } | ||
+ | } | ||
+ | if (k >= NUM_KEYS)k = -1; // No valid key pressed | ||
+ | return k; | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); // 9600 bps | ||
+ | } | ||
+ | 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 ==0) Serial.println("r"); | ||
+ | if (key ==1) Serial.println("l"); | ||
+ | if (key ==2) Serial.println("h"); | ||
+ | if (key ==3) Serial.println("b"); | ||
+ | if (key ==4) Serial.println("s"); | ||
+ | } | ||
+ | |||
+ | </pre> | ||
* 程序效果 | * 程序效果 | ||
:S3 -- 前进 | :S3 -- 前进 | ||
− | : | + | :S4 -- 后退 |
− | : | + | :S2 -- 左转 |
:S1 -- 右转 | :S1 -- 右转 | ||
:S5 -- 停止 | :S5 -- 停止 | ||
− | == | + | ==资料下载== |
[[文件:erweima.png|230px|无框|右]] | [[文件:erweima.png|230px|无框|右]] | ||
* 模拟按键示例程序下载 | * 模拟按键示例程序下载 | ||
− | 链接:https://pan.baidu.com/s/ | + | 链接:https://pan.baidu.com/s/1i2nLla1HmTKN5EYAHERvKw |
+ | 提取码:2u42 | ||
* 产品购买链接:http://www.alsrobot.cn/goods-873.html | * 产品购买链接:http://www.alsrobot.cn/goods-873.html | ||
+ | * 奥松机器人技术论坛:http://www.makerspace.cn |
2021年12月10日 (五) 10:41的最后版本
目录 |
产品概述
奥松机器人新推出的模拟键盘模块,我们在设计按键输入电路时,采用独立IO连接按键或者矩阵连接的方式,矩阵连接相对于独立连接的方式来说占用IO相对较少,但有没有一种更好的方式呢,模拟键盘模块就很好的解决了这个问题,它能让你用一个模拟IO口,实现五个按键状态的读取,真正实现了电路的简化,并且在按下按键时,按键旁的LED会随之亮起。
产品参数
基本参数
- 品名:模拟按键模块
- 货号:RB-02S159
- 尺寸:55*40mm
- 固定孔:M3*4
电气性能
1.工作电压:3.3V~5V
2.工作温度限值:-20℃ ~ +70℃
3.信号类型:模拟信号
4.接口类型:3P 防插反接口
5.工作电流:小于等于20mA,5V 供电下
6.按键及指示灯:S1 ~ S5 五个按键旁分别配有指示灯,按下时亮起
7.按键模拟值:
- S1=380±40
- S2=470±40
- S3=560±40
- S4=680±40
- S5=840±40
8.引脚定义:
- +:电源正极
- -:电源负极
- S:信号
使用方法
example1_Arduino
- 主要硬件
- Arduino UNO 控制器
- 传感器扩展板 V5.0
- 模拟按键模块
- 单头防插反 3P 传感器连接线
- USB 数据线
- 硬件连接
- 示例程序
int adc_key_val[5] ={400,470,560,700,900 }; int NUM_KEYS = 5; int adc_key_in; int key=-1; int oldkey=-1; void setup() { pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat Serial.begin(9600); // 9600 bps } void loop() { adc_key_in = analogRead(0); // read the value from the sensor digitalWrite(13,LOW); key = get_key(adc_key_in); // convert into key press //Serial.println(adc_key_in); 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){ digitalWrite(13,HIGH); switch(key) { case 0:Serial.println("S1 OK"); break; case 1:Serial.println("S2 OK"); break; case 2:Serial.println("S3 OK"); break; case 3:Serial.println("S4 OK"); break; case 4:Serial.println("S5 OK"); break; } } } } delay(100); } // 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; }
- 程序效果
example2_Arduino
- 主要硬件
- Arduino UNO 控制器
- 传感器扩展板 V5.0
- 模拟按键模块
- 单头防插反 3P 传感器连接线
- USB 数据线
- 全彩 LED 发光模块
- 硬件连接
- 示例程序
#include <MeRGBLed.h> MeRGBLed led(PORT_3); int adc_key_val[5] ={400,470,560,700,900 }; int NUM_KEYS = 5; int adc_key_in; int key=-1; int oldkey=-1; 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; } void color_r() { led.setColor(255, 0, 0); led.show(); } void color_g() { led.setColor(0, 255, 0); led.show(); } void color_b() { led.setColor(0, 0, 255); led.show(); } void color_y() { led.setColor(255, 255, 0); led.show(); } void color_w() { led.setColor(255, 250, 250); led.show(); } void setup() { pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat Serial.begin(9600); // 9600 bps } void loop() { adc_key_in = analogRead(0); // read the value from the sensor digitalWrite(13,LOW); 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) { if(key==0) { color_y(); } if(key==1) { color_b(); } if(key==2) { color_r(); } if(key==3) { color_g(); } if(key==4) { color_w(); } } } } delay(100); }
- 程序效果
按下相应按键,全彩LED会显示与按键帽相同的颜色。
example3_Arduino
- 主要硬件
- Arduino UNO 控制器
- 传感器扩展板 V5.0
- 模拟按键模块
- 单头防插反 3P 传感器连接线
- USB 数据线
- 4WD 移动平台
- APC220 无线数传模块
- MotorDriver Shield 电机驱动板
- 示例程序
接收端
#define PWA 3 #define PWB 11 #define DIRA 12 #define DIRB 13 #define BRAKEA 9 #define BRAKEB 8 void setup(){ Serial.begin(9600); pinMode(PWA,OUTPUT); pinMode(PWB,OUTPUT); pinMode(DIRA,OUTPUT); pinMode(DIRB,OUTPUT); pinMode(BRAKEA,OUTPUT); pinMode(BRAKEB,OUTPUT); } void loop(){ if(Serial.available()>0) { char val = Serial.read(); Serial.println(val); if(val == 'r') right(); if(val == 'l') left(); if(val == 'h') head(); if(val == 'b') back(); if(val == 's') stopt(); } } void head(){ analogWrite(PWA,180); analogWrite(PWB,180); digitalWrite(DIRA,HIGH); digitalWrite(BRAKEA,LOW); digitalWrite(DIRB,HIGH); digitalWrite(BRAKEB,LOW); } void back(){ analogWrite(PWA,180); analogWrite(PWB,180); digitalWrite(DIRA,LOW); digitalWrite(BRAKEA,LOW); digitalWrite(DIRB,LOW); digitalWrite(BRAKEB,LOW); } void left(){ analogWrite(PWA,180); analogWrite(PWB,180); digitalWrite(DIRA,HIGH); digitalWrite(BRAKEA,LOW); digitalWrite(DIRB,LOW); digitalWrite(BRAKEB,LOW); } void right(){ analogWrite(PWA,180); analogWrite(PWB,180); digitalWrite(DIRA,LOW); digitalWrite(BRAKEA,LOW); digitalWrite(DIRB,HIGH); digitalWrite(BRAKEB,LOW); } void stopt(){ analogWrite(PWA,0); analogWrite(PWB,0); digitalWrite(DIRA,LOW); digitalWrite(BRAKEA,HIGH); digitalWrite(DIRB,LOW); digitalWrite(BRAKEB,HIGH); }
发射端
int adc_key_val[5] ={400,470,560,700,900 }; int NUM_KEYS = 5; int adc_key_in; int key=-1; int get_key(unsigned int input) { int k; for (k = 0; k < NUM_KEYS; k++) { while (input < adc_key_val[k]) { return k; } } if (k >= NUM_KEYS)k = -1; // No valid key pressed return k; } void setup() { Serial.begin(9600); // 9600 bps } 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 ==0) Serial.println("r"); if (key ==1) Serial.println("l"); if (key ==2) Serial.println("h"); if (key ==3) Serial.println("b"); if (key ==4) Serial.println("s"); }
- 程序效果
- S3 -- 前进
- S4 -- 后退
- S2 -- 左转
- S1 -- 右转
- S5 -- 停止
资料下载
- 模拟按键示例程序下载
链接:https://pan.baidu.com/s/1i2nLla1HmTKN5EYAHERvKw 提取码:2u42
- 产品购买链接:http://www.alsrobot.cn/goods-873.html
- 奥松机器人技术论坛:http://www.makerspace.cn