Hello Guys,
So I have a simple game I am creating based on the arduino Keypad code. Basically, I have three color buttons (red, blue, green). Red corresponds to the #1 on key pad, blue is #2, and green is #3. SO basically when you press, red, blue, green, red a relay activates and an electronic lock is activated. The matrix looks something like this: char keys[rows][cols] = {
{'1'},
{'2'},
{'3'},
};
I got the code to work. But all I need to do now is to automatically reset the code after say 5 seconds, or reset after a incorrect entry. I am positive my circuit works fine because relay is activate after I manual reset arduino and enter code. Any advice?
#include <Keypad.h>
char* secretCode = "1231";
int position = 0;
const byte rows = 3;
const byte cols = 1;
char keys[rows][cols] = {
{'1'},
{'2'},
{'3'},
};
byte rowPins[rows] = {2, 6, 7};
byte colPins[cols] = {3};
Keypad keypad = Keypad(makeKeymap(keys),
rowPins, colPins,
rows, cols);
int relay1 = 13;
void setup()
{
pinMode(relay1, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key == '22' ) {
position = 0;
setLocked(true);
delay(100);
}
if (key == secretCode[position]) {
position++;
}
if (position == 4) {
setLocked(false);
}
delay(100);
}
void setLocked(int locked)
{
if (locked) {
digitalWrite(relay1, LOW);
}
else {
digitalWrite(relay1, HIGH);
delay(5000);
}
digitalWrite(relay1, LOW);
}