简体   繁体   English

(Arduino Mega2560) 键盘密码检查

[英](Arduino Mega2560) Keypad password check

I am working on a school project, basically I have to create a lock system that opens a fictive gate when the correct code is entered.我正在做一个学校项目,基本上我必须创建一个锁定系统,当输入正确的代码时,它会打开一个虚拟的门。 We have been asked to simulate our system before actually building it.我们被要求在实际构建之前模拟我们的系统。 So, I made the following circuit in Proteus(Labcenter Electronics' simulation software): Keypad circuit因此,我在 Proteus(Labcenter Electronics 的仿真软件)中制作了以下电路:键盘电路

Sorry, I can't post images.对不起,我不能发图片。

Here's what my system should do: A variable contains the correct code and the user must type a code on the keypad, and if it is correct, a green LED turns on and the LCD screen displays "Acces Granted!"这是我的系统应该做的事情:一个变量包含正确的代码,用户必须在键盘上输入一个代码,如果正确,绿色 LED 亮起,LCD 屏幕显示“已授予访问权限!” if the code is wrong, the screen will display "Access refused!".如果密码错误,屏幕将显示“拒绝访问!”。 It's a basic system, but I'm trying to make it as simple/short as possible(I have to explain it in an oral presentation, so the simpler the better) and for some reason, I can't get my code to work.这是一个基本系统,但我试图让它尽可能简单/简短(我必须在口头演示中解释它,所以越简单越好)并且出于某种原因,我无法让我的代码工作. Ideally, I'd like to have the LCD display a * for each character typed, liked on a real security system, but I haven't managed to do that either.理想情况下,我想让 LCD 为每个键入的字符显示一个 *,这在真正的安全系统上很受欢迎,但我也没有设法做到这一点。 I've been working on it for hours, trying different variants and etc.. but nothing seems to work the way I want to.我一直在研究它几个小时,尝试不同的变体等等……但似乎没有什么能像我想要的那样工作。 Btw, my circuit works perfectly fine and the initialisation part of my code is therefore correct aswell.顺便说一句,我的电路工作得很好,因此我的代码的初始化部分也是正确的。 Can anyone help me figure out what's wrong with my code and how I can make it better?谁能帮我弄清楚我的代码有什么问题以及如何使它变得更好?

Here's my code so far:到目前为止,这是我的代码:

#include <LiquidCrystal.h>
#include <Keypad.h>

//define LED pins
#define redLED 11
#define greenLED 10

String codeSerrure = "87362";   //correct code that opens the imaginative gate
String enteredCode = "";
int keyPressed;

const byte rows = 4;
const byte cols = 3;

char touches_digicode [rows] [cols] = {

  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins [rows] = {25, 26, 27, 28};
byte colPins [cols] = {24, 23, 22};

Keypad leDigicode = Keypad( makeKeymap(touches_digicode), rowPins, colPins, rows, cols);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


void setup(){
  lcd.begin(16, 2);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, HIGH);
}

void loop(){
  lcd.setCursor(0,0);
  lcd.print(" Entrez le code");

  keyPressed = leDigicode.getKey();
  enteredCode += String(keyPressed);
  if(enteredCode.length() >= 5){
    if(enteredCode == codeSerrure){
      digitalWrite(greenLED, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("  Acces autorise!");
      delay(4000);
      digitalWrite(greenLED, LOW);
      digitalWrite(redLED, HIGH);
      enteredCode = "";
    }
    else{
      digitalWrite(redLED, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("  Acces refuse!");
      delay(4000);
      enteredCode = "";
    }
  }
  if(keyPressed == "#"){
    lcd.clear();
    enteredCode = "";
  }
}

The problem is that when I run this code in the simulation software, the LCD displays "Acces refuse!"问题是,当我在模拟软件中运行这段代码时,LCD 显示“访问拒绝!” and the red LED is on.并且红色 LED 亮起。 No matter what keys I press (on the keypad) nothing happens.无论我按什么键(在小键盘上)都没有任何反应。 So the problem is that my code jumps straight to the else statement, infering that the password typed is wrong (although no password was actually typed).所以问题是我的代码直接跳转到 else 语句,推断输入的密码是错误的(虽然实际上没有输入密码)。 I think the error is in here:我认为错误在这里:

keyPressed = leDigicode.getKey();
  enteredCode += String(keyPressed);
  if(enteredCode.length() >= 5){
    if(enteredCode == codeSerrure){

Btw, forgive the french words, I am french.顺便说一句,原谅法语单词,我是法国人。 Also, I forgot to say, the # key should, clear the entered code.另外,我忘了说,# 键应该清除输入的代码。 Any explainations, code samples or links would help.任何解释、代码示例或链接都会有所帮助。 Thank you!谢谢!

The method getKey() returns a char; getKey() 方法返回一个字符; so it ALWAYS returns a char !所以它总是返回一个字符! For instance if no key has been pressed, it returns NO_KEY (= '\\0').例如,如果没有按下任何键,则返回 NO_KEY (= '\\0')。

The way you wrote your code, you quickly fill enteredCode string with "\\0\\0\\0\\0\\0".按照您编写代码的方式,您可以用“\\0\\0\\0\\0\\0”快速填充enteredCode字符串。 Change your code with:更改您的代码:

keyPressed = leDigicode.getKey();
if (keyPressed)   {
    enteredCode += String(keyPressed);
    // following is for debug
    // lcd.print(keyPressed);   delay(300);
}
if(enteredCode.length() >= 5) {   ...  

Thanks to all you guys' help I finally managed to code all the features I wanted with no glitches and the code is much cleaner.感谢你们所有人的帮助,我终于设法编写了我想要的所有功能,没有出现任何故障,并且代码更清晰。 I'm aware that my code is not perfect and can probably be improved, so if anyone still interested, any modification ideas are welcome.我知道我的代码并不完美,可能可以改进,所以如果有人仍然感兴趣,欢迎任何修改想法。 Anyway, here's my final code for those interested:无论如何,这是我为感兴趣的人提供的最终代码:

#include <LiquidCrystal.h>
#include <Keypad.h>

//define LED pins
#define redLED 11
#define greenLED 10

int contrast = 25;
String codeSerrure = "87362";
char keyPressed;

const byte rows = 4;
const byte cols = 3;

char touches_digicode [rows] [cols] = {

  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins [rows] = {25, 26, 27, 28};
byte colPins [cols] = {24, 23, 22};

Keypad leDigicode = Keypad( makeKeymap(touches_digicode), rowPins, colPins, rows, cols);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

byte e_aigu[8] = {
  B00010,
  B01100,
  B00000,
  B01110,
  B10001,
  B11111,
  B10000,
  B01110
};
byte e_grave[8] = {
  B01000,
  B00110,
  B00000,
  B01110,
  B10001,
  B11111,
  B10000,
  B01110
};

void setup(){
  analogWrite(8, contrast);
  lcd.createChar(0, e_aigu);
  lcd.createChar(1, e_grave);
  lcd.begin(16, 2);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, HIGH);
}

void loop(){
  String enteredCode = "";
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" Entrez le code:");

  while(enteredCode.length() < 5){
    keyPressed = leDigicode.getKey();
    if(keyPressed){
      if(keyPressed == 0x23 || keyPressed == 0x2A){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" Entrez le code:");
        enteredCode = "";
      }
      else
      {
        enteredCode += keyPressed;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" Entrez le code:");
        lcd.setCursor(5, 1);
        for(int i=0; i < enteredCode.length(); i++){
          lcd.print("*");
        }
      }
    }
  }

  delay(390);

  if(enteredCode == codeSerrure){
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, HIGH);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" Acc");
    lcd.write((uint8_t)1);
    lcd.print("s autoris");
    lcd.write((uint8_t)0);
    lcd.print("!");
    delay(4000);
    digitalWrite(greenLED, LOW);
    digitalWrite(redLED, HIGH);
  }
  else
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("  Acc");
    lcd.write((uint8_t)1);
    lcd.print("s refus");
    lcd.write((uint8_t)0);
    lcd.print("!");
    delay(4000);
  }
}

As you can see I changed many things.正如你所看到的,我改变了很多东西。 I rewrote the password check function with a while loop and integrated a function that displays a * on the LCD each time a key is pressed.我用 while 循环重写了密码检查功能,并集成了每次按下键时在 LCD 上显示 * 的功能。 I have also added the erase function for the * and # keys.我还为 * 和 # 键添加了擦除功能。 And since my LCD doesn't support characters such as é and è (I needed them for french words, why do we have to make everything more complicated), I created my own.由于我的 LCD 不支持诸如 é 和 è 之类的字符(我需要它们来表示法语单词,为什么我们必须使一切变得更复杂),我创建了自己的。 I also made a small modification to my circuit, I removed the potentiometer and assigned the contrast value of the LCD in my code instead.我还对我的电路做了一个小的修改,我去掉了电位器,并在我的代码中分配了 LCD 的对比度值。 Here's the finished schematics:这是完成的原理图:

Can't post pictures yet还不能发图片

Thanks to everyone who helped me!感谢所有帮助过我的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM