极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10831|回复: 1

矩阵键盘密码能否用变量代替,实现在线更改密码~~

[复制链接]
发表于 2013-8-10 21:29:07 | 显示全部楼层 |阅读模式
Password password = Password( "1234" );
以上是例程里面的代码,密码是事先设定好了的
我想将上述密码设置成一个变量,可以在线更改密码
String comdata = "1234";
Password password = Password(comdata);
这个代码编译不能通过,求高人~~~~就是矩阵键盘的密码怎么用变量赋值?
回复

使用道具 举报

发表于 2013-8-10 23:26:28 | 显示全部楼层
用password.set(newPassword);

请参考
http://ediy.com.my/index.php/tutorials/item/69-password-access-with-arduino-using-keypad
  1. #include <Keypad.h>
  2. #include <Password.h>

  3. String newPasswordString; //hold the new password
  4. char newPassword[6]; //charater string of newPasswordString

  5. //initialize password to 1234
  6. //you can use password.set(newPassword) to overwrite it
  7. Password password = Password( "1234" );

  8. byte maxPasswordLength = 6;
  9. byte currentPasswordLength = 0;
  10. const byte ROWS = 4; // Four rows
  11. const byte COLS = 4; // Four columns

  12. //Define the keymap
  13. char keys[ROWS][COLS] = {
  14. {'1','2','3','A'},
  15. {'4','5','6','B'},
  16. {'7','8','9','C'},
  17. {'*','0','#','D'}
  18. };

  19. //// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  20. byte rowPins[ROWS] = {6,7,8,9}; //connect to row pinouts

  21. // Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
  22. byte colPins[COLS] = {2,3,4,5}; //connect to column pinouts

  23. // Create the Keypad
  24. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

  25. void setup(){
  26.    Serial.begin(9600);
  27. }

  28. void loop(){
  29.    char key = keypad.getKey();
  30.    if (key != NO_KEY){
  31.       delay(60);
  32.       switch (key){
  33.       case 'A': break;
  34.       case 'B': break;
  35.       case 'C': break;
  36.       case 'D': changePassword(); break;
  37.       case '#': checkPassword(); break;
  38.       case '*': resetPassword(); break;
  39.       default: processNumberKey(key);
  40.       }
  41.    }
  42. }

  43. void processNumberKey(char key) {
  44.    Serial.print(key);
  45.    currentPasswordLength++;
  46.    password.append(key);
  47.    if (currentPasswordLength == maxPasswordLength) {
  48.       checkPassword();
  49.    }
  50. }

  51. void checkPassword() {
  52.    if (password.evaluate()){
  53.       Serial.println(" OK.");
  54.    } else {
  55.       Serial.println(" Wrong passwowrd!");
  56.    }
  57.    resetPassword();
  58. }

  59. void resetPassword() {
  60.    password.reset();
  61.    currentPasswordLength = 0;
  62. }

  63. void changePassword() {
  64.    newPasswordString = "123";
  65.    newPasswordString.toCharArray(newPassword, newPasswordString.length()+1); //convert string to char array
  66. password.set(newPassword);
  67.    resetPassword();
  68.    Serial.print("Password changed to ");
  69.    Serial.println(newPasswordString);
  70. }

  71. void resetPassword() {
  72.    //do something to resetPassword
  73. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 13:56 , Processed in 0.051092 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表