Trying to concatenate keypad entries?

Hey Guys,

Trying to get something simple done, but can't seem to get it to work right.

Basically, just want a user to enter four numbers into a keypad, and then print those four numbers to the serial monitor as 'xxxx'.

Here's my code, what am I doing wrong?

void setup() {
Serial.begin(9600);
Serial.println("Press a Key...");
}

void loop() {
String keyOne = keypad.waitForKey();
String keyTwo = keypad.waitForKey();
String keyThree = keypad.waitForKey();
String keyFour = keypad.waitForKey();

String pwd = keyOne + keyTwo + keyThree + keyFour;

Serial.println(pwd);
delay(2000);

}

what am I doing wrong?

Posting incomplete code.
Using String instances to hold single characters.
Posting code that does something without telling us what it actually does.
Failing to post a link to the library you are using.
Using a blocking function when a non-blocking one would be better.

I think that about sums it up.

Sorry...here's the full code...

#include <Keypad.h>

//Build Keypad Layout
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows] = {5,4,3,2};
byte colPins[cols] = {9,8,7,6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols);

// void checkIfFirstUse() {} //Checks to see if password is set to '9999', if it is, asks user for default password and let's them set new password

// void firstUse() {} //Set Password For First Use - Default pwd is '9999'

void setup() {
Serial.begin(9600);
Serial.println("Press a Key...");
}

void loop() {
char keyOne = keypad.waitForKey();
char keyTwo = keypad.waitForKey();
char keyThree = keypad.waitForKey();
char keyFour = keypad.waitForKey();

char pwd = 'keyOne + keyTwo + keyThree + keyFour';

Serial.println(pwd);
delay(2000);

}

Link to library: Arduino Playground - HomePage

Sorry...here's the full code...

Still posted incorrectly.

5 issues. One partly addressed. That's a 20% - a flunking grade.

Well...since I'm new to this code, I can't actually tell you what the difference is between a blocking and nonblocking function is. I tried to put what I was trying to accomplish in the first post. I added the library after I went back and saw what you were asking for - so can you now give me an example of the best way to do what I'm trying to accomplish?

I can't actually tell you what the difference is between a blocking and nonblocking function is.

A blocking function, like waitForKey() is one that does not return until some action has happened. A non-blocking function, like getKey() will return immediately, but may not have any data.

If you want to do nothing until the user has entered a password, use blocking functions. If you want to do other things while you wait for a password, don't.

so can you now give me an example of the best way to do what I'm trying to accomplish?

char pass[5];

pass[0] = keypad.waitForKey();
pass[1] = keypad.waitForKey();
pass[2] = keypad.waitForKey();
pass[3] = keypad.waitForKey();
pass[4] = '\0';
  char pwd = 'keyOne + keyTwo + keyThree + keyFour';

Which ONE key did you press to get the ONE character in the single quotes there? I don't have that key on my keyboard.

'Key One'. Ha ha. Get a good laugh at the noobs expense. :smiley:

Let me ask you this, when you build your array, should you put that inside the loop function or outside? Is the arduino code similar to javascript, where even if you put a function at the end, the program can be evaluate the function...or do the functions have to be loaded in order of use?

The biggest thing that puzzles me is what should, and what shouldn't be put in the loop function. Obviously, if you put the println expression in there, it'll print a bazillion times to your serial monitor...right?

PS...code works :smiley:

Another question. I know that arduino has a password library. Do you think I should use that to verify a password entered into the keypad, or would simple if/then statements be sufficent? I just don't know if there are any advantages to using the password library....

Let me ask you this, when you build your array, should you put that inside the loop function or outside?

That depends on what you are trying to accomplish. I think that your code should be getting the password in setup(), not in loop(), since it is using blocking code.

Is the arduino code similar to javascript, where even if you put a function at the end, the program can be evaluate the function...or do the functions have to be loaded in order of use?

Functions need to be defined, or prototyped, before they are used. The IDE takes care of creating the prototypes for any functions that do not have them, putting them at the top of the sketch.

The biggest thing that puzzles me is what should, and what shouldn't be put in the loop function. Obviously, if you put the println expression in there, it'll print a bazillion times to your serial monitor...right?

Yes, it will. So, that should give you a clue where to put code. If it is to execute over and over, like checking to see if a key on the keypad is pressed, it goes in loop(). If it should be executed only once, it goes in setup().

Do you think I should use that to verify a password entered into the keypad, or would simple if/then statements be sufficent?

Do you want to be able to deal with multiple passwords? Do you want to be able to change the password? Do you want to be able to deal with variable length passwords? If the answer to all of these is no, then you don't need the library.

PaulS:
Do you want to be able to deal with multiple passwords? Do you want to be able to change the password? Do you want to be able to deal with variable length passwords? If the answer to all of these is no, then you don't need the library.

Hello, if there's a necessity to be able to change the password, but there's a single password, and not variable length passwords, should the Library be downloaded?

should the Library be downloaded?

If you understand what the Password library does, then you should be able to replicate the functionality. Then, the only thing that the library does is provide ready-to-use code, which is not a bad thing.

If you just plan to use code you don't understand, then that is not a good thing.

You have to decide for yourself whether to use a library, or not.