Instantly share code, notes, and snippets.

Embed
What would you like to do?
//http://playground.arduino.cc/Main/KeypadTutorial
//http://playground.arduino.cc/Main/MultiUserKeypadPassword
//http://forum.arduino.cc/index.php?topic=43803.0
#include <Key.h>
#include <Keypad.h>
int i = 0;
int state = 1; //create state
char pressedNumbers[7];
char phoneNumber1 [7] = {'3', '4', '8', '5', '0', '5', '8'};
char phoneNumber2 [7] = {'3', '4', '8', '5', '0', '7', '3'};
char phoneNumber3 [7] = {'3', '4', '8', '4', '0', '9', '5'};
char phoneNumber4 [7] = {'9', '6', '6', '4', '8', '1', '0'};
//const int numCount = 8; //7 numbers plus null
const byte ROWS = 4; // 4 rows
const byte COLS = 3; //3 columns
//Define the Keymap
char keys [ROWS] [COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'#', '0', '*'}
};
//Connect keypad ROW0, ROW1, ROW2 & ROW3 to Arduino Pins
byte rowPins[ROWS] = {5, 6, 8, 9};
//connect keypad COL0, COL1, COL2 to Arduino Pins
byte colPins[COLS] = {2, 3, 4};
//Create the Keypad
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#define ledpin 13
void setup() {
//pinMode(ledpin, HIGH);
Serial.begin(9600);
}
void loop() {
char key = kpd.getKey();
//if key is pressed
if (key) {
pressedNumbers[i] = key; //store entry into array
i++;
Serial.println(key);
if (i == 7) //if there have been 7 presses
{
{
//match string to password
if (pressedNumbers[0] == phoneNumber1[0] &&
pressedNumbers[1] == phoneNumber1[1] &&
pressedNumbers[2] == phoneNumber1[2] &&
pressedNumbers[3] == phoneNumber1[3] &&
pressedNumbers[4] == phoneNumber1[4] &&
pressedNumbers[5] == phoneNumber1[5] &&
pressedNumbers[6] == phoneNumber1[6] )
{
Serial.println("SUCCESS Number 1");
Serial.println(1, DEC);
} else if (pressedNumbers[0] == phoneNumber2[0] &&
pressedNumbers[1] == phoneNumber2[1] &&
pressedNumbers[2] == phoneNumber2[2] &&
pressedNumbers[3] == phoneNumber2[3] &&
pressedNumbers[4] == phoneNumber2[4] &&
pressedNumbers[5] == phoneNumber2[5] &&
pressedNumbers[6] == phoneNumber2[6]) {
Serial.println("SUCCESS Number 2");
} else if (pressedNumbers[0] == phoneNumber3[0] &&
pressedNumbers[1] == phoneNumber3[1] &&
pressedNumbers[2] == phoneNumber3[2] &&
pressedNumbers[3] == phoneNumber3[3] &&
pressedNumbers[4] == phoneNumber3[4] &&
pressedNumbers[5] == phoneNumber3[5] &&
pressedNumbers[6] == phoneNumber3[6])
{
Serial.println("SUCCESS Number 3");
} else if (pressedNumbers[0] == phoneNumber4[0] &&
pressedNumbers[1] == phoneNumber4[1] &&
pressedNumbers[2] == phoneNumber4[2] &&
pressedNumbers[3] == phoneNumber4[3] &&
pressedNumbers[4] == phoneNumber4[4] &&
pressedNumbers[5] == phoneNumber4[5] &&
pressedNumbers[6] == phoneNumber4[6]) {
Serial.println("SUCCESS Number 4");
} else {
Serial.println("NOT MATCHED");
//state == 2;
}
{
i = 0;
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment