Setting up a PIN pad

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;
}

Why not just comment out these two lines

It won't print, but if you push the wrong digit it will automatically return "false," the light automatically turns red and it gives a sound indicating that it was the wrong digit and exits the loop. So if I press "1," and the first digit is "0," it lights up and I have to start over.

If it gives instant feedback that the button was incorrect, then that password can be brute forced. For an Arduino project, that’s not a big deal but if it was for an actual security scenario, that’s a big problem.

You should wait until the user presses a “Confirm” button first before analyzing the password. Less chance it can be cracked in a few dozen tries.

For reference:

Verify your wiring matches your own code

The code I liked to works (Post #5) works.

Your wiring is wrong. The "false" is returned when the code receives the "#" or "*" which are not digits.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.