SKU:RB-02S159 模擬鍵盤模塊

來自ALSROBOT WiKi
2018年9月12日 (三) 16:19Arduino77討論 | 貢獻(xiàn)的版本

跳轉(zhuǎn)至: 導(dǎo)航、 搜索
02S15900.png

目錄

產(chǎn)品概述

產(chǎn)品參數(shù)

基本參數(shù)

電氣性能

使用方法

example1_Arduino

  • 主要硬件
Arduino UNO 控制器
傳感器擴(kuò)展板 V5.0
模擬按鍵模塊
單頭防插反 3P 傳感器連接線
USB 數(shù)據(jù)線
  • 硬件連接
02S15901.png
  • 示例程序
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;
}
  • 程序效果
02S15902.png

example2_Arduino

  • 主要硬件
Arduino UNO 控制器
傳感器擴(kuò)展板 V5.0
模擬按鍵模塊
單頭防插反 3P 傳感器連接線
USB 數(shù)據(jù)線
全彩 LED 發(fā)光模塊
  • 硬件連接
02S15903.png
  • 示例程序
#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);
}

  • 程序效果

按下相應(yīng)按鍵,全彩LED會(huì)顯示與按鍵帽相同的顏色。

example3_Arduino

  • 主要硬件
Arduino UNO 控制器
傳感器擴(kuò)展板 V5.0
模擬按鍵模塊
單頭防插反 3P 傳感器連接線
USB 數(shù)據(jù)線
4WD 移動(dòng)平臺(tái)
APC220 無線數(shù)傳模塊
MotorDriver Shield 電機(jī)驅(qū)動(dòng)板
  • 示例程序

接收端

發(fā)射端

  • 程序效果
S3 -- 前進(jìn)
S2 -- 后退
S4 -- 左轉(zhuǎn)
S1 -- 右轉(zhuǎn)
S5 -- 停止

相關(guān)資料

Erweima.png
  • 模擬按鍵示例程序下載

鏈接:https://pan.baidu.com/s/1QReHaInd2T2wP7-yzmiSwQ 密碼:g914