Help with keypad.getkey();?

Hi all

Introduction: If you are interested, otherwise please proceed to the “Question” section
First time user here. I am currently doing a little project using the keypad and password libraries. I have connected my keypad to the arduino and got the keypad inputs to show on the LCD without any major issue. So when I input the correct password, LCD shows Success and green LED flashes, otherwise LCD shows “Denied” and red LED flashes, simple enough, right?

Now, I am trying to include a function to allow the user to set their own password. The idea is to store the keypad input to an array and pass the values in the array to the Password password = Password( “” );

I have tried something very simple below and got an error, as expected, let me show you my code:

void loop(){
  for(int i = 0; i < 5; i++){
  int userPW[i] = keypad.getKey();
  }
}

Question
So, my question is: What exactly does the getKey() returns? I have read that it returns int value but I guess the integer value can’t be stored like I tried to using the code above?

Any helps or tips would be greatly appreciated ;D ;D ;D

int userPW[i] = keypad.getKey();

You can’t just invent storage like that.

int userPW= keypad.getKey();

Would compile, but would be pointless because of the limited scope of the variable.

userPW= keypad.getKey();would be far more sensible.

oh i see, i thought you should always declare the type of the array though?

Yes, you should always declare the type of any variable.

chengtheman:
oh i see, i thought you should always declare the type of the array though?

Yeah, when you declare it, but not each time you use it. When you include the type like that you are creating a new array. If you give it the same name as the old one, but in a different scope then things get really confusing.

Yep, I have confused myself for quite a number of times due to using the same name in different scope, but I was never aware that when you declare the type, you are actually creating a new ones.

Thanks for the feedback, appreciate it :slight_smile:

chengtheman:
So, my question is: What exactly does the getKey() returns?

It returns zero if no key was pressed. If a key was pressed it returns the value you assigned to that key on the keypad. Typically that would be a character constant like ‘0’ or ‘A’.