Hey. I am working on a project for fun with different types of sensors and stuff. It's sort of like a mini smarthome box to test stuff with. I am currently working on the password system, and my change password system is not working.
When you click the change password button, it checks if the password you have entered currently is correct. If it is, it enters password set mode, where you can enter the new password and press the same button. It then should change the password and the old one stops working. Instead, every password seems to be correct now. If I print the guess and the target in the serial monitor, it seems like the target is changing to the guess.
#include <IRremote.hpp>
#define IR_RECEIVE_PIN A2
#include <LiquidCrystal_I2C.h>
#include <Password.h>
LiquidCrystal_I2C mylcd(0x27, 16, 2);
Password password = Password( "1234" );
// Variable declarations
bool setpass;
volatile int gas;
void setup()
{
Serial.begin(9600);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
mylcd.init();
mylcd.backlight();
mylcd.setCursor(1 - 1, 1 - 1);
mylcd.print("Password:");
}
void loop() {
if (IrReceiver.decode()) {
bool num;
switch (IrReceiver.decodedIRData.decodedRawData) {
case 3860463360:
password.append('0');
num = true;
break;
case 3125149440:
password.append('1');
num = true;
break;
case 3108437760:
password.append('2');
num = true;
break;
case 3091726080:
password.append('3');
num = true;
break;
case 3141861120:
password.append('4');
num = true;
break;
case 3208707840:
password.append('5');
num = true;
break;
case 3158572800:
password.append('6');
num = true;
break;
case 4161273600:
password.append('7');
num = true;
break;
case 3927310080:
password.append('8');
num = true;
break;
case 4127850240:
password.append('9');
num = true;
break;
case 3810328320:
checkPass();
break;
case 3910598400:
resetPass(true);
break;
case 4061003520:
setPass();
break;
}
if (num) {
mylcd.setCursor(1 - 1, 2 - 1);
mylcd.print(password.guess);
}
delay(300);
IrReceiver.resume();
}
}
// Password functions
void checkPass() {
Serial.println(password.target);
Serial.println(password.guess);
if (password.evaluate()) {
mylcd.setCursor(1 - 1, 2 - 1);
mylcd.clear();
mylcd.print("Correct!");
} else {
mylcd.setCursor(1 - 1, 2 - 1);
mylcd.clear();
mylcd.setCursor(1 - 1, 1 - 1);
mylcd.print("Wrong");
delay(1000);
resetPass(true);
}
}
void resetPass(bool again) {
password.reset();
mylcd.setCursor(1 - 1, 2 - 1);
mylcd.print("Reset");
delay(1000);
mylcd.clear();
if (again) {
mylcd.setCursor(1 - 1, 1 - 1);
mylcd.clear();
mylcd.print("Password:");
}
}
void setPass() {
if (setpass) {
setpass = false;
password.target = password.guess;
mylcd.setCursor(1 - 1, 1 - 1);
mylcd.clear();
mylcd.print("Password Set");
delay(1000);
resetPass(true);
return 0;
}
if (password.evaluate()) {
setpass = true;
password.reset();
mylcd.setCursor(1 - 1, 1 - 1);
mylcd.clear();
mylcd.setCursor(1 - 1, 1 - 2);
mylcd.clear();
mylcd.print("Set Password:");
} else {
mylcd.setCursor(1 - 1, 2 - 1);
mylcd.clear();
mylcd.print("Wrong");
delay(1000);
resetPass(true);
}
}
This is using a well known password management library that can be found here: Arduino Playground - Password Library
Serial Monitor (after pass change to 4321):
6789 (target password)
6789 (guess)
The target password should be 4321, and the guess should be 6789, which are not equal, and it should be marked as wrong, but they are equal.
Yes. I set the password to 4321 using the change password function and it still says all password are correct. If I log the target password right after I change it, it outputs the correct thing, it’s just when it evaluates it or something in that function.
The newly entered password guess is set as the target password. But
target is just a pointer, which now points to the buffer used for the guess. From now on, evaluate always returns true -- unless the buffer is full, due to an unrelated bug in evaluate, in which case it always returns false, even if you never touched target and the 19-character password matches.
To add your desired feature, you can create your own target buffer
I've done some experimenting and added more logging. It seems like password.target is blank after I change it using password.target = password.guess. password.guess returns the correct value, but password.target is blank when I set it to password.guess.