极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19765|回复: 1

3*4 keypad 如何做出 完成輸入鍵

[复制链接]
发表于 2015-7-23 12:45:01 | 显示全部楼层 |阅读模式
小弟近日接了一個3*4的keypad 想利用此鍵盤來修改程式裡pwm輸出的功率

現在遇到的問題是:
當我輸入數值後,我希望能按下#來完成輸入
現階段鍵盤只要輸入一個數,LCD就會馬上顯示出來
那麼我設定的代碼 ex. char keyin = keypad.getKey();
keyin 之值 永遠只會是個位數,無法是90、100

想請問各位大大,有些甚麼建議呢?

目前所用的程式碼如下
#include <LiquidCrystal.h>
#include <Keypad.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// 3x4 Keypad
const byte ROWS = 4; // 4 Rows
const byte COLS = 3; // 3 Columns

// 定義 Keypad 的按鍵
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

// 定義 Keypad 連到 Arduino 的接腳
byte rowPins[ROWS] = {32, 22, 24, 28}; // 連到 Keypad 的 4 個 Rows: Row0, Row1, Row2, Row3
byte colPins[COLS] = {30, 34, 26};   // 連到 Keypad 的 3 個 Columns: Column0, Column1, Column2

// 建立 Keypad 物件
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);


}

void loop() {
   // 讀取 Keypad 的輸入
  char key = keypad.getKey();

  // NO_KEY 代表沒有按鍵被按下
  if (key != NO_KEY){
    // 假如有按鍵被按下,就印出按鍵對應的字元
    Serial.println(key);
    lcd.print(key);
  }
}
回复

使用道具 举报

发表于 2015-7-23 17:06:06 | 显示全部楼层
先用一個 char[] 作 buffer, 把收到的 key 先記下.
直到接收到 "#" 的 key 就把已記下的一次過輸出並同時清空.
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 08:26 , Processed in 0.036405 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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