Robofun 機器人論壇

標題: 關於-Arduino LCD i2c + 3*4鍵盤 結合 [打印本頁]

作者: twst911302    時間: 2014-6-19 11:16
標題: 關於-Arduino LCD i2c + 3*4鍵盤 結合
如題Arduino LCD i2c 1602 + 3*4鍵盤 結合
當我用3*4鍵盤輸入 數字,要顯示於LCD裡,
有範例教學嗎??
作者: avril90    時間: 2015-7-22 14:25
雖然一年後才回覆此主題,希望依舊能幫助到有需要的人
p.s.有問題請留言給我

#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);
  }
}
作者: xa5566888    時間: 2015-12-15 16:03
請教樓上大大
我想要做一個門禁系統,我分別寫了兩個程式
第一個:RFID配合繼電器啟動電子門鎖
第二個:利用4*4鍵盤輸入密碼,密碼正確後啟動電子門鎖
目前第一個已經成功寫出來了
第二個部分還差在 寫入密碼的程式還沒想出來..
想請教大大,如何利用4*4鍵盤寫出密碼程式、以及讓兩種程式結合再一起?
作者: xa5566888    時間: 2015-12-15 16:04
這是4*4鍵盤及LCD螢幕顯示器的程式

#include <Keypad.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>  

//#define LCMIIC1
//#define LCMIIC2
#define LCMIIC3

/*-----( Declare Constants )-----*/
#if defined(LCMIIC1)
#define I2C_ADDR    0x20  // Define I2C Address
#define BACKLIGHT_PIN  7
#define En_pin  4
#define Rw_pin  5
#define Rs_pin  6
#define D4_pin  0
#define D5_pin  1
#define D6_pin  2
#define D7_pin  3
#define BACKLIGHT_FLAG  NEGATIVE
#elif defined(LCMIIC2)
#define I2C_ADDR    0x27  // Define I2C Address
#define BACKLIGHT_PIN  3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
#define BACKLIGHT_FLAG  POSITIVE
#elif defined(LCMIIC3)
#define I2C_ADDR    0x20  // Define I2C Address
#define BACKLIGHT_PIN  3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
#define BACKLIGHT_FLAG  POSITIVE
#else // error
#error LCM not defined
#endif

#define  LED_OFF  0
#define  LED_ON  1

// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// 3x4 Keypad



const byte ROWS = 4; // 4 Rows
const byte COLS = 4; // 3 Columns

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

// 定義 Keypad 連到 Arduino 的接腳
byte rowPins[ROWS] = {2, 3,4, 5}; // 連到 Keypad 的 4 個 Rows: Row0, Row1, Row2, Row3
byte colPins[COLS] = {6, 7,8, 9};   // 連到 Keypad 的 3 個 Columns: Column0, Column1, Column2

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


void setup()   
{
Serial.begin(9600);
  lcd.begin (16,2);  
   
  lcd.print("password:");
  lcd.setBacklightPin(BACKLIGHT_PIN,BACKLIGHT_FLAG);
  
  lcd.setBacklight(LED_ON);

  lcd.setCursor(0, 1);
  lcd.cursor(); // 顯示游標
  lcd.blink(); // 讓游標閃爍
}


void loop() {

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

  // NO_KEY 代表沒有按鍵被按下
  if (key != NO_KEY){
     Serial.println(key);
            lcd.print(key);
  }
     if(key == '*'){
        lcd.clear();
          lcd.print("Password:");
            lcd.setCursor(0, 1);
         
}
  }




歡迎光臨 Robofun 機器人論壇 (https://www.robofun.net/forum/) Powered by Discuz! X3.2