keypad lock problems

im working with this:

#include <Keypad.h>

char* secretCode = "1234";
int position = 0;

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

int redPin = 9;
int greenPin = 10;

void setup()                    
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  setLocked(true);  
}

void loop()                    
{
  char key = keypad.getKey();
  if (key == '*' || key == '#')
  {
    position = 0;
    setLocked(true);
  }
  if (key == secretCode[position])
  {
    position ++;
  }
    
  if (position == 4)
  {
    setLocked(false);  
  delay(2000);
  setLocked(true);
  }
}

void setLocked(int locked)
{
  if (locked)
  {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);  
  }
  else
  {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);      
  }
}

i got it online, and it works. i modified it a bit, to fit the pins mine's wired to, and to set the "open" to 2 seconds then lock again.

the problem is in entering the code (into the keypad). it is set at 1234, and right now, you can hit xxxxxxx1xxxxxxx2xxxxxxx3xxxxxxxx4 and open. im looking to modify it so that if an incorrect key is pressed it restarts, so that you MUST enter 1234 to open.

i tried adding - if (key != secretCode[position]) {position = 0;} iv put it in different positions, ive tried it with "else", but i cant get it to work. i am not receiving an error code, it compiles and loads fine, but when you enter 1234, nothing happens.

what am i doing wrong?

Why not just add an ELSE statement that resets position back to zero if the key does not equal the secretcode.

if (key == secretCode[position])
  {
    position ++;
  }
else position = 0; // You can also add a message that tells the user the password was reset.

dosent the else need a qualifier? like: else (key != ect...)

No, but if you use "else if" then you can have it look for a particular condition, like "key != secretcode[position]"

nope. tried the else position = 0 like you posted, and i also tried the else if != line as well. both of them "worked" as in i did not get an error message and they uploaded fine, but when i hit "1234" it does not unlock. for some reason anything i put in there is stopping it from working all together.

this is my very first arduino project ever, but this seems pretty straight forward. idk what im missing here.

  char key = keypad.getKey();

If no key is pressed, what will be in key? How are you handling that? Or, rather, why aren't you handling that?

void loop()                    
{
  char key = keypad.getKey();
  if(key)  // This blocks "NO_KEY" to be entered. Very important line your code needs to work correctly
  {
    
   // Rest of code here

  }
}
  if(key)  // This blocks the code from allowing NO_KEY to be entered. Very important line your code needs to work correctly

It does, but:

   if(key != NO_KEY)

makes the comment unnecessary, by clearly defining what is expected to happen in the if test.

PaulS:

  char key = keypad.getKey();

If no key is pressed, what will be in key? How are you handling that? Or, rather, why aren't you handling that?

this was along the lines of what i was thinking. that when the 1 was released, it was counting no key press as not fitting the pass code, therefore resetting it to position 1. as far as the code itself,

again, i got the sketch from the net, and the keypad stuff in my library came from here in the downloads section. all i did was a few modifications to it , and this is the last one i need to do, but i cant get it to happen.

is there possibly another sketch anyone knows of that might work better? id be open to that too, but id really like to know what im doing wrong here to help me in the future.

Answer in reply# 6

yes, i realize that, i was just saying that i had thought myself that the problem may be something like this.

I have some keypad sketches you can have, if you want them. I have a simple keypad calculator, I have one that controls the position of a stepper, and a few keypad password sketches.

Keypad_Password2.ino (3.71 KB)

Keypad_Calculator2.ino (2.21 KB)

Keypad_Stepper.ino (1.16 KB)

thanks! ill give it a shot.

alternatively, this is a very simple circuit hardware wise, requiring only a keypad, 2 resistors, and 2 led, and you could easily do away with the red led for test purposes. can anyone throw it together quick and confirm that adding one of those lines completely bricks it out? i cant see it being a problem on my end unless i have a bad arduino board, and since it works without the line, im doubting thats the case either.