Hi,
This is my first arduino project. It's a simple keypad lock using a 3x4 matrix keypad. As this is a learning experience for me (c++ and electronics) I decided not to use the keypad or password libraries because copying pasting and editing a few lines of code is not a good way to learn in my opinion. The code is messy and uncommented but it works perfectly. I plan to tidy up the code a bit and add comments soon. Also I am going to move the passkey into the EEPROM and add some way of setting it without burning a new sketch.
/*
* Zabouth Keypad Lock 2010
* Pin Color
* 10 | Green
* 9 | Yellow
* 8 | Pink
* 7 | White
* 6 | Purple
* 5 | Blue
* 4 | Brown
*/
long lastDebounceTime = 0;
long debounceDelay = 50;
int debouce = 0;
int colpress = 0;
int rowpress = 0;
int ledPin = 13;
char PassCode[5] = "1234";
char EntCode[5] = "AAAA";
const byte ROWS = 4;
const byte COLS = 3;
byte rowPins[ROWS] = {7,6,5,4};
byte colPins[COLS] = {8,9,10};
int PressCount = 0;
char customKey;
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
customKey = keypress();
if (customKey){
Serial.println(customKey);
if (customKey == 42) {
resetcode();
}
else{
EntCode[PressCount] = customKey;
PressCount = PressCount + 1;
}
if (PressCount == 4 && EntCode[0] == PassCode[0] && EntCode[1] == PassCode[1] && EntCode[2] == PassCode[2] && EntCode[3] == PassCode[3]){
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
resetcode();
}
if (PressCount == 4 && EntCode[0] != PassCode[0] || PressCount == 4 && EntCode[1] != PassCode[1] || PressCount == 4 && EntCode[2] != PassCode[2] || PressCount == 4 && EntCode[3] != PassCode[3]){
resetcode();
}
}
}
int keypress()
{
int r,c,x = 0;
char ret;
r = ScanRow();
if (r) { rowpress = r; }
c = ScanCol ();
if (c) { colpress = c; }
if (c && r)
{
c = c - 1;
r = r - 1;
if (debouce == 0 && (millis() - lastDebounceTime) > debounceDelay){
debouce = 1;
return hexaKeys[r][c];
}else{ return 0; }
}else{
debouce = 0;
lastDebounceTime = millis();
return 0;
}
}
int ScanRow() {
pinMode(7, INPUT);
digitalWrite(7, HIGH);
pinMode(6, INPUT);
digitalWrite(6, HIGH);
pinMode(5, INPUT);
digitalWrite(5, HIGH);
pinMode(4, INPUT);
digitalWrite(4, HIGH);
pinMode(8,OUTPUT);
digitalWrite(8, LOW);
pinMode(9,OUTPUT);
digitalWrite(9, LOW);
pinMode(10,OUTPUT);
digitalWrite(10, LOW);
if (digitalRead(7) == LOW)
{
return 1;
}
else if (digitalRead(6) == LOW)
{
return 2;
}
else if (digitalRead(5) == LOW)
{
return 3;
}
else if (digitalRead(4) == LOW)
{
return 4;
}
else
{
return 0;
}
}
int ScanCol () {
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
pinMode(8,INPUT);
digitalWrite(8, HIGH);
pinMode(9,INPUT);
digitalWrite(9, HIGH);
pinMode(10,INPUT);
digitalWrite(10, HIGH);
if (digitalRead(8) == LOW)
{
return 1;
}
else if (digitalRead(9) == LOW)
{
return 2;
}
else if (digitalRead(10) == LOW)
{
return 3;
}
else
{
return 0;
}
}
void resetcode()
{
Serial.println("reset");
PressCount = 0;
EntCode[0] = 32;
EntCode[1] = 32;
EntCode[2] = 32;
EntCode[3] = 32;
}
