I'm doing a "30 Days lost in space" kit. I'm on day 15. This project uses an RGB led, a speaker, and a keypad. It prompts you to push one button to sign in and another to change the PIN. It then gives you feedback in the form of different lights and sounds. The code was already provided for me, but it's set up in such a way that as soon as you hit a wrong number it gives you error feedback. That means you can just keep guessing numbers until you find the right one. My project is to change the code so that it won't give error feedback until you enter the whole pin. I decided to change the "bool validatePIN" function. There is an array for the PIN, so my idea was to create a second array, record the digits, and then compare the contents of the second array to the PIN. So I used a for loop to enter in the contents of the 2nd array, and then once all 4 digits are entered, I want it to compare the contents of the arrays and return true or false depending on if they match. Once I tested the code, I was able to get it to accept multiple numbers, but once 4 digits are entered it automatically says access granted. I would like some help in straightening this out.
Here is the function I'm working on. The arrays are declared at the very top of the code:
char password[PIN_LENGTH] = { '0', '0', '0', '0' }; // Initial password is four zeros.
char pin_input[PIN_LENGTH]={'0','0','0','0'};
bool validatePIN() {
Serial.println("Enter PIN to continue.");
for (int i = 0; i < PIN_LENGTH; i++) {
pin_input[i] = heroKeypad.waitForKey();
if (i < (PIN_LENGTH)){
giveInputFeedback();
displayColor(128, 80, 0);
Serial.print('*');
}
else if(i == PIN_LENGTH){
if(pin_input == password){
giveSuccessFeedback();
displayColor(0,128,0);
Serial.println();
Serial.println("Device successfully unlocked!");
return true;
}
else {
return false;
giveErrorFeedback();
Serial.println("Wrong PIN, Access denied");
}
}
}
}
For comparison, this is the original function:
bool validatePIN() {
Serial.println("Enter PIN to continue.");
for (int i = 0; i < PIN_LENGTH; i++) {
char button_character = heroKeypad.waitForKey();
if (password[i] != button_character) {
giveErrorFeedback(); // Error sound and red light
Serial.println(); // start next message on new line
Serial.print("WRONG PIN DIGIT: ");
Serial.println(button_character);
return false; // return false and exit function
}
// Give normal input feedback for all but the LAST character
if (i < (PIN_LENGTH - 1)) {
giveInputFeedback(); // Short beep and blue LED
}
Serial.print("*");
}
giveSuccessFeedback(); // PIN matched - TADA! sound with green LED
Serial.println(); // add new line after last asterisk so next message is on next line
Serial.println("Device Successfully Unlocked!");
return true;
}
