Hi I am using a keypad to unlock an electric door strike.

I have the door strike circuit working and the keypad is displaying the current button press by serial.

The problem is I need to save each keypress to an array up to 5 keypress's long, once it has 5 press's is check to see if the code is correct. I just cant work out how to do the array.

any help would be very appriciated.

Thanks

Ben

Without knowing how you’re reading the keypad, etc., here is what I’d do in pseudo code:

``````int i = 0; (variable to keep track of number of keys pressed);
int key[5] = {0, 0, 0, 0, 0};
int correct[5] = {1, 2, 3, 4, 5}; (the correct combo)
boolean correctCombo = false; (make a boolean to store whether the correct code is entered)

void setup() {
initialize things you may need such as pins, etc.
}

void loop() {
if (correctCombo == false) { (if you haven't entered the correct code)
check to see if a key is pressed
if a key is pressed {
determine what key it is
store the key in key[i]
i++;
if ( i == 5) { (check if you've entered 5 numbers)
for (int i = 0; i < 5; i++) {
if (key[i] != combo[i]) { (compare the key to the corect combo
i = 5; (terminate the for loop)
correctCombo = false; (state that the combo isn't correct)
}
else {
do the things that you want the correct code to do
}
}
i = 0;
}
}
}
``````

I know that’s sloppy, and I probably missed a few brackets, etc., but that should give you an idea of how I’d do it.

thanks heaps i will give it a go ;D

this is what I have done but still doesn’t seem to work could you have a look TchnclFl…

Thanks

``````#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
int i = 0; //(variable to keep track of number of keys pressed);
int key[5] = {0, 0, 0, 0, 0};
int correct[5] = {1, 2, 3, 4, 5}; //(the correct combo)
boolean correctCombo = false; //(make a boolean to store whether the correct code is entered)

// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

#define ledpin 13

void setup()
{
digitalWrite(ledpin, HIGH);
Serial.begin(9600);

}

void loop()
{
if (correctCombo == false) {
if(key)  // same as if(key != NO_KEY)
key[i] = kpd.getKey();
i++;
if ( i == 5) { //(check if you've entered 5 numbers)
for (int i = 0; i < 5; i++) {
if (key[i] != correct[i]) { //(compare the key to the corect combo
i = 5; //(terminate the for loop)
correctCombo = false; //(state that the combo isn't correct)
Serial.println("wrong");

}
else {
Serial.println("correct");
}
}
}

i = 0;
}

}
``````

this is what I have done but still doesn't seem to work could you have a look TchnclFl..

I don't think I can be trusted with code right now. It's 1.20 AM here and I'm exhausted. Going to bed now. I'll try to remember to look at it tomorrow if someone doesn't overnight.

Good luck :).

PS: If you could clarify what "doesn't work" about it, that'd be great. Also, try adding Serial.println()'s along the way to see what's being stored in each of the variables, etc.

PSS: Taking a look back at it, it looks like we're setting i back to zero at the wrong place. It's hard to tell because of all the parentheses, but it should be after we've determined whether or not the entered code is right (inside of " if ( i == 5) {}").

thanks for your help at such a late time!!!

I will keep playing

talk to you later

I have come up with the following code but it still says wrong code…

``````#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
int i = 0; //(variable to keep track of number of keys pressed);
char code[5] = {0, 0, 0, 0, 0};
char correct[5] = {1, 2, 3, 4, 5};
boolean correctCombo = false;

// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

#define ledpin 13

void setup()
{
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}

void loop()
{
char key = kpd.getKey();
if(key)  // same as if(key != NO_KEY)
{
code[i] = key;
//Serial.println(key);
if (i == 5){
check();

}
else{

Serial.println(code[i]);

i++;
}

}
}

void check()
{

for (int i = 0; i < 5; i++) {
if (code[i] != correct[i]){
i = 5;
correctCombo = false;
Serial.println("Wrong code");
break;
}
else
{
Serial.println("Correct code");

}

}
}
``````

Thanks

Ben

ok I have found the problem

I need to join variables together highlighted below in the foowing code

``````#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
int i = 0;
char code[5] = {
0, 0, 0, 0, 0};
char correct[5] = {
1, 2, 3, 4, 5};
boolean correctCombo = false;
char fcode;
int ccode = (12345);
int score;

// Define the Keymap
char keys[ROWS][COLS] = {
{
'1','2','3'      }
,
{
'4','5','6'      }
,
{
'7','8','9'      }
,
{
'#','0','*'      }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {
9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {
12, 11, 10 };

#define ledpin 13

void setup()
{
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}

void loop()
{
char key = kpd.getKey();
if(key)  // same as if(key != NO_KEY)
{
code[i] = key;
//Serial.println(key);
if (i == 5){
for (int p = 0; p < 5; p++) {
[glow]fcode = fcode & code[p];[/glow]
}
Serial.println(fcode);

if (fcode == ccode){

Serial.println("Correct code");
fcode = 0;
i = 0;
}
else
{
Serial.println("Wrong code");
fcode = 0;
i = 0;
}
}
else{
Serial.println(code[i]);
i++;
}
}
}
``````

thanks

Ben

Ok I Finally go the code working…just 1 little bug

once you enter the correct code
then next code you enter has additional garbage with the code

code below

then serial output

``````#include <Keypad.h>
#define VERSION "V0.5 Jan2010"

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
int i = 0;
char code[5] = {0, 0, 0, 0, 0};
boolean correctCombo = false;

// Define the Keymap
char keys[ROWS][COLS] = {
{
'1','2','3'      }
,
{
'4','5','6'      }
,
{
'7','8','9'      }
,
{
'#','0','*'      }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {12, 11, 10 };

#define ledpin 13

void setup()
{

digitalWrite(ledpin, HIGH);
Serial.begin(9600);
Serial.println("DOOR SECURITY ONLINE");
Serial.println(VERSION);
}

void loop()
{

if (i == 5){
check();
}
char key = kpd.getKey();
if(key)
{
code[i] = key;
i++;
}
}

void check()
{
Serial.println(code);
if (strcmp(code, "12345") == 0){
Serial.println("Correct code");
correctCombo = true;
i = 0;

}
else
{
Serial.println("Wrong code");
correctCombo = false;
i = 0;
}
}
``````

12345
Correct code
12345Uae4
Wrong code

``````  char key = kpd.getKey();
if(key)
{
code[i] = key;
i++;
[glow]    code[i] = '\0';[/glow]
}
``````

The string needs to be null terminated. On the first pass, the string IS null terminated (since all the characters are null).

You should also change the size of code to 6 characters (so there is room for the terminating NULL.