我是 Arduino 和 tinkercad 的初学者,我不太确定问题出在哪里,但我正在尝试创建一个键盘和 LCD 锁。起初,它说 setlock 没有在范围内声明,但后来我添加了 'boolean setLocked' 但后来错误只是变成说 setLocked 不能用作函数。任何帮助将不胜感激,请与我交谈,就好像我很愚蠢一样,哈哈,我刚刚开始这门课程,没有任何先验知识。谢谢!
boolean setLocked;
#include <LiquidCrystal.h>
#include <Keypad.h>
#define redLED 10
#define greenLED 11
char* password="1212";
int positions = 0;
const byte rows = 4;
const byte columns = 4;
char keyMap [rows] [columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [rows] = {1, 2, 3, 4};
byte columnsPins [columns] = {5, 6, 7, 8};
Keypad myKeypad = Keypad (makeKeymap(keyMap), rowPins, columnsPins, rows, columns);
LiquidCrystal lcd (A0, A1, A2, A3, A4, A5);
void setup() {
lcd.begin(16, 2);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
setLocked(true);
}
void loop() {
char whichKey = myKeypad.getKey();
lcd.setCursor (0,0);
lcd.print ("Hello");
lcd.setCursor(0,1);
lcd.print("Please enter the password");
if(whichKey == '*' || whichKey =='#'|| whichKey =='A' || whichKey =='B'|| whichKey =='C' || whichKey =='D') {
positions=0;
setLocked(true);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("The password is incorrect");
delay(500);
lcd.clear();
}
if(whichKey == password [positions]) {
positions ++;
}
if(positions == 4){
setLocked(false);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!Correct!");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("End Arduino");
lcd.setCursor(0, 1);
lcd.print("Thank you for trying me out")
delay(5000);
lcd.clear();
}
delay(100);
}
void setLocked(int locked)
if(locked){
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
else{
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
}