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?