万马奔腾 发表于 2013-8-10 21:29:07

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

Password password = Password( "1234" );
以上是例程里面的代码,密码是事先设定好了的
我想将上述密码设置成一个变量,可以在线更改密码
String comdata = "1234";
Password password = Password(comdata);
这个代码编译不能通过,求高人~~~~就是矩阵键盘的密码怎么用变量赋值?

smching 发表于 2013-8-10 23:26:28

用password.set(newPassword);

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

String newPasswordString; //hold the new password
char newPassword; //charater string of newPasswordString

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

byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

//Define the keymap
char keys = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

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

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

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

void setup(){
   Serial.begin(9600);
}

void loop(){
   char key = keypad.getKey();
   if (key != NO_KEY){
      delay(60);
      switch (key){
      case 'A': break;
      case 'B': break;
      case 'C': break;
      case 'D': changePassword(); break;
      case '#': checkPassword(); break;
      case '*': resetPassword(); break;
      default: processNumberKey(key);
      }
   }
}

void processNumberKey(char key) {
   Serial.print(key);
   currentPasswordLength++;
   password.append(key);
   if (currentPasswordLength == maxPasswordLength) {
      checkPassword();
   }
}

void checkPassword() {
   if (password.evaluate()){
      Serial.println(" OK.");
   } else {
      Serial.println(" Wrong passwowrd!");
   }
   resetPassword();
}

void resetPassword() {
   password.reset();
   currentPasswordLength = 0;
}

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

void resetPassword() {
   //do something to resetPassword
}
页: [1]
查看完整版本: 矩阵键盘密码能否用变量代替,实现在线更改密码~~