\\Arduino\\KeyPad\\KeyPad.ino: In function 'void setup()':
\\Arduino\\KeyPad\\KeyPad.ino:24:9: error: expected unqualified-id before '.' token
Keypad.tick();
^
\\Arduino\\KeyPad\\KeyPad.ino: In function 'void loop()':
\\Arduino\\KeyPad\\KeyPad.ino:29:9: error: expected unqualified-id before '.' token
Keypad.begin();
^
\\Arduino\\KeyPad\\KeyPad.ino:34:16: error: expected primary-expression before '.' token
while (Keypad.available()) {
^
\\Arduino\\KeyPad\\KeyPad.ino:35:5: error: 'keypadEvent' was not declared in this scope
keypadEvent e = Keypad.read();
^\~\~\~\~\~\~\~\~\~\~
\\Arduino\\KeyPad\\KeyPad.ino:35:5: note: suggested alternative: 'KeypadEvent'
keypadEvent e = Keypad.read();
^\~\~\~\~\~\~\~\~\~\~
KeypadEvent
\\Arduino\\KeyPad\\KeyPad.ino:36:24: error: 'e' was not declared in this scope
Serial.print((char)e.bit.KEY);
^
\\Arduino\\KeyPad\\KeyPad.ino:37:24: error: 'KEY_JUST_PRESSED' was not declared in this scope
if (e.bit.EVENT == KEY_JUST_PRESSED) Serial.println(" pressed");
^\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~
\\Arduino\\KeyPad\\KeyPad.ino:38:29: error: 'KEY_JUST_RELEASED' was not declared in this scope
else if (e.bit.EVENT == KEY_JUST_RELEASED) Serial.println(" released");
^\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~
exit status 1
Compilation error: expected unqualified-id before '.' token
代码
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte colPins[ROWS] = { 41, 43, 45, 47 }; //connect to the row pinouts of the keypad
byte rowPins[COLS] = { 33, 35, 37, 39 }; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const String password = "1234"; // change your password here
String input_password;
void setup() {
Serial.begin(9600);
Keypad.tick();
input_password.reserve(32); // maximum input characters is 33, change if needed
}
void loop() {
Keypad.begin();
// put your main code here, to run repeatedly:
while (Keypad.available()) {
keypadEvent e = Keypad.read();
Serial.print((char)e.bit.KEY);
if (e.bit.EVENT == KEY_JUST_PRESSED) Serial.println(" pressed");
else if (e.bit.EVENT == KEY_JUST_RELEASED) Serial.println(" released");
}
delay(10);
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = ""; // clear input password
} else if (key == '#') {
if (password == input_password) {
Serial.println("password is correct");
// DO YOUR WORK HERE
} else {
Serial.println("password is incorrect, try again");
}
input_password = ""; // clear input password
} else {
input_password += key; // append new character to input password string
}
}
}
我查找错误消息并修复它,并得到相同的错误消息。
Keypad.tick();
应该是
keypad.tick();
(小写字母很重要!)
然后将其应用于键盘的每个对象实例(不是 Keypad 键盘,第一个是类/类型!)
基本上 Arduino IDE 抱怨的每一行都是这样的:
\Arduino\KeyPad\KeyPad.ino:29:9: 错误: 之前预期的不合格 ID '.'令牌Keypad.begin(); ^
您应该在“K/键盘”一词中输入小写的 k 而不是大写的 K。
您在代码中包含标准 Arduino 库时,错误地使用了 Adafruit Keypad 库的接口/功能。
导入 Adafruit Keypad 库并使用您拥有的代码,或者更改您的代码/基于这些示例的代码:
https://github.com/Chris--A/Keypad/tree/master/examples