要店4位數的值,這樣做可能是使用尺寸4.數組假設keypad.getKey
返回一個int,你可以這樣做的最簡單和原始的方法這個:int input[4] = {0};
。
您將需要一個遊標變量知道到該陣列的插槽,你需要寫的時候按下一個鍵,所以你可以做一些類型的循環是這樣的:
int input[4] = {0};
for (unsigned cursor = 0; cursor < 4; ++cursor) {
input[cursor] = keypad.getKey();
}
如果你想使用終止符(可以說你的鍵盤有0-9和AF鍵,我們可以說,F是終止鍵),代碼修改爲類似:
bool checkPassword() {
static const int expected[4] = {4,8,6,7}; // our password
int input[4] = {0};
// Get the next 4 key presses
for (unsigned cursor = 0; cursor < 4; ++cursor) {
int key = keypad.getKey();
// if F is pressed too early, then it fails
if (key == 15) {
return false;
}
// store the keypress value in our input array
input[cursor] = key;
}
// If the key pressed here isn't F (terminating key), it fails
if (keypad.getKey() != 15)
return false;
// Check if input equals expected
for (unsigned i = 0; i < 4; ++i) {
// If it doesn't, it fails
if (expected[i] != input[i]) {
return false;
}
}
// If we manage to get here the password is right :)
return true;
}
現在你可以使用checkPassword功能,在主功能如下:
int main() {
while (true) {
if (checkPassword())
//unlock the thing
}
return 0;
}
NB:使用定時器聽起來也是可能的(也可以與終止字符選項組合使用,它們不是唯一的)。這樣做的方法是設置一個計時器到您選擇的時間段,當它結束時,您將光標變量重置爲0.
(我從來沒有在arduino上編程,也不知道它的鍵盤庫,但邏輯在這裏,它取決於你現在)
這是相同的當AI電話運營商告訴你「_Enter你的電話號碼,然後綁定KEY_」這樣劃分累加器
value
什麼阻止你這樣做? –我不知道該怎麼做。你能推薦一個解釋它的網站嗎? –
這很簡單。試想一下:通過繼續調用getkey()來填充你的數字容器。每次你得到一個密鑰,檢查它,它是終止密鑰然後停止,否則再次調用getkey()來獲得一個新的密鑰。 –