最近在玩4x4薄膜鍵盤,想做一個密碼鎖,密碼鎖事做出來了,但是我想增加一個新增密碼的功能,想到是用陣列方法,但不知道怎麼使用,只好上來尋求大家幫忙!!!!!
這是我的密碼鎖程式:
#include <Keypad.h> // 引用Keypad程式庫
#define KEY_ROWS 4 // 按鍵模組的列數
#define KEY_COLS 4 // 按鍵模組的行數
#define LEDG 5 // 綠LED--通過
#define LEDR 15 // 紅LED--禁止通行
/*-------按鍵模組基本設定-------*/
char keymap[KEY_ROWS][KEY_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte colPins[KEY_COLS] = {9, 8, 7, 6}; // 按鍵模組,行1~4接腳。
byte rowPins[KEY_ROWS] = {13, 12, 11, 10}; // 按鍵模組,列1~4接腳。
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, KEY_ROWS, KEY_COLS);
/*--------------------*/
String passcode ="4321"; //預設密碼
String inputCode =""; // 暫存用戶的按鍵字串
bool acceptKey = true; // 代表是否接受用戶按鍵輸入的變數,預設為「接受」
/*--------------------*/
// 比對用戶輸入的密碼
void checkPinCode() {
acceptKey = false; // 暫時不接受用戶按鍵輸入
// 比對密碼
if (inputCode == passcode) {///////-------
Serial.println("Welcome home!");
digitalWrite(LEDG,HIGH);
digitalWrite(LEDR,LOW);
} else {
Serial.println("***false word!!***");
digitalWrite(LEDR,LOW);
delay(500);
digitalWrite(LEDR,HIGH);
delay(500);
digitalWrite(LEDR,LOW);
delay(500);
digitalWrite(LEDR,HIGH);
acceptKey = true;
}
}
void setup(){
Serial.begin(9600);
pinMode(LEDG, OUTPUT);
pinMode(LEDR, OUTPUT);
digitalWrite(LEDG,LOW);
digitalWrite(LEDR,HIGH);
}
char buf[100];
int count;
boolean quit;
void loop(){
// 透過Keypad物件的getKey()方法讀取按鍵的字元
char key = myKeypad.getKey();
if (key != NO_KEY){
count = 0;
if(key != '#') {
Serial.println(key);
quit = false;
buf[count++] = key;
while (!quit) {
key = myKeypad.getKey();
if(key != NO_KEY) {
if(key == '#')
{
quit = true;
buf[count] = '\0';
} else {
buf[count++] = key;
}
inputCode = buf;
Serial.println(key);
}
}
checkPinCode();
Serial.print("Your input is ");
Serial.println(buf);
}
}
}
我想到的新增密碼的陣列方法如下:
void readLine(){//將收到的內容寫入char陣列之中
char p;
int i=0;
while(buf[count] = '\0'){//收到'\0'(換行)則表示接收完畢
str[i]=p;
i++;
delay(5);
}
}
不知道有人可以教我如何新增密碼!!! 感謝 |