Later I'll post my code with some changes!! XD
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
correctpassword();
cleardata();
break;
}
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
correctpassword();
cleardata();
break;
}
}
Now I would like to know how I can turn on some leds when I press some key of keypad only the password is correct for example, using one new pins:
#define LED1 = 10
Later read that password is correct I press for example *so turn on the LED1
Are you saying if the password is correct and after you press *, to turn on LED1? So the password is acting as a lock, and if you put a correct password in, you what to access more functions.
If this is the case, then you will need to add another while loop at the beginning of the Loop() function and a new Boolean variable in if(address == 4...).
void loop()
{
While( pass_not_found == false)
{
// rest of code
}
// password is good, new functions available
}
Could you show me one example with the before code, put in last code for turn on one new LED.
then I try to do with some more leds...
#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library
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] = {
2,3,4,5};
/*5, 4, 3, 2};*/
byte colPins[COLS] = {
10,9,8 /*7, 6*/};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
volatile unsigned long delaytime = 1000; // made volatile but I don't think it matters.
boolean timeout = false, PassFound = false;
char datatest[number_of_passwords][length_of_password]={
{
'1','2','2','1' }
,
{
'1','2','2','3' }
,
{
'1','1','1','1' }
,
{
'9','9','9','9' }
};
#define ledPin 13
#define AlarmWrong 10
int i = 0; //variable to count number of key pressed
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);
for(street = 0; street < number_of_passwords * length_of_password; street += 4)
{
for( address =0; address < length_of_password; address++)
{
EEPROM.write(address + street, datatest[street/4][address]);
}
}
address = 0; // clear address
street = 0; // clear street
}
void loop(){
while(PassFound == false){
timeout = false;
while(num != 4) // get four numbers
{
char mykey = kpd.getKey(); // Check the first keypress
if(mykey){ // This will check to see if a key was pressed
current_time = millis(); // and if so, this will also store the current time.
key[num] = mykey; // This will add the keypress to the array
Serial.print(key[num]);
num++; // This will increment the array for the next keys
while(time < hang_time){ // This will allow the program to get the rest of the
time = millis() - current_time;// of the number within the time limit of 5 seconds
char mykey = kpd.getKey(); // Check keypress again
if(mykey){
key[num] = mykey;
mykey = 0; // The key must be set to zero, otherwise the code will not work correctly
Serial.print(key[num]);
num++;
}
if(num == 4)
{
time = 0; // reset variable "time" for timer to start over
break;
}
}
if(time >= 5000)
{
Serial.println(" Timed out");
cleardata(); // Clear everything
timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
time = 0;
break;
}
}
}
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
//WHY IS THIS NEEDED, bolded so it stands out?
// digitalWrite(ledPin, HIGH);
// delay(1000);
// digitalWrite(ledPin, LOW);
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
Serial.print(" User ");
Serial.println(user);
correctpassword();
cleardata();
break;
}
}
if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
{
Serial.println(" Password not found");
digitalWrite(AlarmWrong, HIGH);
delay(500);
digitalWrite(AlarmWrong, LOW);
cleardata(); // clear variables
}
}
//PASSWORD WAS FOUND, BROKEN OUT OF GETPASSWORD LOOP
if(millis() - current_time > getTime()){ NOTE: This is not 100% responsive, its just an example to show what you can do
current_time = millis();
digitalWrite(ledPin, !digitalRead(ledPin)); // SIMPLE BLINK METHOD
}
}//End of loop()
unsigned long getTime() // CHANGE BLINK RATE
{
if(kpd.getKey() == '*'){
current_time = millis();
delaytime = 100;
}
else if(kpd.getKey() == '#') {
current_time = millis();
delaytime = 500;
}
return delaytime;
}
void cleardata()
{
timeout = false;
address = 0;
street = 0;
user = 1;
num = 0;
return;
}
void correctpassword()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
PassFound = true;
cleardata(); //clears the input array
}
Hello,
I achieve to do one code, then when Password is correct if I press a key *, 0 or # it will turn on one LED, but now I want to know how I can reset this code, because I want later password is correct press the key but later I press just on of this key it must reset for me press again the password...and next...next...
#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library
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] = {
5, 4, 3, 2};
byte colPins[COLS] = {
8, 7, 6};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
boolean timeout = false;
char myKey;
char datatest[number_of_passwords][length_of_password]={
{
'1','2','2','1' }
, // Murillo Passaword
{
'1','2','2','3' }
, // Jeferson Password
{
'1','1','1','1' }
, // Messias Password
{
'9','9','9','9' } // New costume
};
#define ledPin 9
#define AlarmWrong 10
#define LED 11
#define LED2 12
#define LED3 13
//int i = 0; //variable to count number of key pressed
void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
for(street = 0; street < number_of_passwords * length_of_password; street += 4)
{
for( address =0; address < length_of_password; address++)
{
EEPROM.write(address + street, datatest[street/4][address]);
}
}
address = 0; // clear address
street = 0; // clear street
}
void loop(){
timeout = false;
while(num != 4) // get four numbers
{
myKey = kpd.getKey(); // Check the first keypress
if(myKey){ // This will check to see if a key was pressed
current_time = millis(); // and if so, this will also store the current time.
key[num] = myKey; // This will add the keypress to the array
num++; // This will increment the array for the next keys
while(time < hang_time){ // This will allow the program to get the rest of the
time = millis() - current_time;// of the numbers within the time limit of 5 seconds
myKey = kpd.getKey(); // Check keypress again
if(myKey){
key[num] = myKey;
myKey = 0; // The key must be set to zero, otherwise the code will not work correctly
num++;
}
if(num == 4)
{
time = 0; // reset variable "time" for timer to start over
break;
}
}
if(time >= 5000) // If hang time has reached 5 seconds, display timeout message
{
readAlarmWrong();
cleardata(); // Clear everything
timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
time = 0;
break;
}
}
}
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
digitalWrite(ledPin, HIGH); //correct password
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
cleardata(); //clears the input array
kpd.addEventListener(PositionsON); //when I press on key later password is correct turn on one LED
cleardata(); // clear variables
break;
}
}
if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
{
readAlarmWrong();
cleardata(); // clear variables
}
}
void cleardata()
{
timeout = false;
address = 0;
street = 0;
user = 1;
num = 0;
return;
}
void readAlarmWrong()
{
digitalWrite(AlarmWrong, HIGH);
delay(500);
digitalWrite(AlarmWrong, LOW);
}
void PositionsON(KeypadEvent key){
switch (kpd.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(LED, HIGH); // Turn ON LED1
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
if (key == '*') {
digitalWrite(LED2, HIGH); // Turn ON LED2
delay(500);
digitalWrite(LED2, LOW);
delay(500);
}
if (key == '0'){
digitalWrite(LED3, HIGH); // Turn ON LED3
delay(500);
digitalWrite(LED3, LOW);
delay(500);
}
break;
cleardata(); // clear variables
}
}
Sorry, Im having a hard time trying to understand what your saying.
Your current code will do this,
enter 4 digit password + # = LED1 on/off, repeat
--------------------- + * = LED2 on/off, repeat
--------------------- + 0 = LED3 on/off, repeat
The code I gave you will lock out the "enter password loop" and allow you to access your other functions. Like a security system, you enter a correct password, and the door unlocks.
What are you trying do exactly. When you say reset, do you mean you want to relock the code so the person has to enter the password again?
Sorry,
I want do say that for me access the " void PositionsON(KeypadEvent key) " the password need to stay correct.
When I say RESET:
I want to say...Then I need to read again the password for me get " void PositionsON(KeypadEvent key) " again.
But remenber that: On " void PositionsON(KeypadEvent key) "...I have some functions, turn on LED, LED2 and LED3.
but for each access I want access just one function for each time.
Some functions for me access:
void PositionsON(KeypadEvent key){
if(clearAlarm = 2){
switch (kpd.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(LED, HIGH); // Turn ON LED1
delay(500);
digitalWrite(LED, LOW);
delay(500);
clearAlarm = 0;}
if (key == '*') {
digitalWrite(LED2, HIGH); // Turn ON LED2
delay(500);
digitalWrite(LED2, LOW);
delay(500);
clearAlarm = 0;
}
if (key == '0'){
digitalWrite(LED3, HIGH); // Turn ON LED3
delay(500);
digitalWrite(LED3, LOW);
delay(500);
clearAlarm = 0;}
break;
cleardata(); // clear variables
clearAlarm = 1;
}
}}
but I need to access just on for turn...wherever functions for example: Now I want to turn on LED1, but later I want to turn on LED2...and next
Thank's
Ahh Ok.
So you put the password in, code checks to see its correct. Once it is correct you then want it to access the PositionsON function and stay there until a choice is made. From that point you press a key, say #, on LED1 is on. Now repeat enter password, go to PositionON funct.
Now at this point, I can code it that, so that you can't access the same key as before. So once you pick one, you have a choice of the remaining two, the next turn. Once you press a new key, the previous key will again be available. This just like having radio buttons, where only one is on at a time and if you pick a new button, it is turned on, and all the others stay off.
Try this.
#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library
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] = {
5, 4, 3, 2};
byte colPins[COLS] = {
8, 7, 6};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
boolean timeout = false;
boolean clearAlarm = false; //ADDED NEW BOOLEAN
char lastKey = ' '; //ADDED NEW CHAR
char checkKey;//ADDED NEW CHAR
char myKey;
char datatest[number_of_passwords][length_of_password]={
{
'1','2','2','1'}
, // Murillo Passaword
{
'1','2','2','3'}
, // Jeferson Password
{
'1','1','1','1'}
, // Messias Password
{
'9','9','9','9'} // New costume
};
#define ledPin 9
#define AlarmWrong 10
#define LED 11
#define LED2 12
#define LED3 13
//int i = 0; //variable to count number of key pressed
void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
for(street = 0; street < number_of_passwords * length_of_password; street += 4)
{
for( address =0; address < length_of_password; address++)
{
EEPROM.write(address + street, datatest[street/4][address]);
}
}
address = 0; // clear address
street = 0; // clear street
}
void loop(){
timeout = false;
while(num != 4) // get four numbers
{
myKey = kpd.getKey(); // Check the first keypress
if(myKey){ // This will check to see if a key was pressed
current_time = millis(); // and if so, this will also store the current time.
key[num] = myKey; // This will add the keypress to the array
num++; // This will increment the array for the next keys
while(time < hang_time){ // This will allow the program to get the rest of the
time = millis() - current_time;// of the numbers within the time limit of 5 seconds
myKey = kpd.getKey(); // Check keypress again
if(myKey){
key[num] = myKey;
myKey = 0; // The key must be set to zero, otherwise the code will not work correctly
num++;
}
if(num == 4)
{
time = 0; // reset variable "time" for timer to start over
break;
}
}
if(time >= 5000) // If hang time has reached 5 seconds, display timeout message
{
readAlarmWrong();
cleardata(); // Clear everything
timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
time = 0;
break;
}
}
}
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
digitalWrite(ledPin, HIGH); //correct password
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
cleardata(); //clears the input array
clearAlarm= false;
while(!clearAlarm){
kpd.addEventListener(PositionsON); //when I press on key later password is correct turn on one LED
}
cleardata(); // clear variables
break;
}
}
if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
{
readAlarmWrong();
cleardata(); // clear variables
}
}
void cleardata()
{
timeout = false;
address = 0;
street = 0;
user = 1;
num = 0;
return;
}
void readAlarmWrong()
{
digitalWrite(AlarmWrong, HIGH);
delay(500);
digitalWrite(AlarmWrong, LOW);
}
void PositionsON(KeypadEvent key){
checkKey = kpd.getKey(); // check key press
if(checkKey != lastKey){ // compare it lastKey to see if it is a new key.
switch (checkKey){
case '#':
{
digitalWrite(LED, HIGH); // Turn ON LED1
delay(500);
digitalWrite(LED, LOW);
delay(500);
clearAlarm = true;
lastKey = checkKey;
cleardata();
break;
}
case '*':
{
digitalWrite(LED2, HIGH); // Turn ON LED2
delay(500);
digitalWrite(LED2, LOW);
delay(500);
clearAlarm = true;
lastKey = checkKey;
cleardata();
break;
}
case '0':
{
digitalWrite(LED3, HIGH); // Turn ON LED3
delay(500);
digitalWrite(LED3, LOW);
delay(500);
clearAlarm = true;
lastKey = checkKey;
cleardata();
break;
}
default:
{
cleardata();
} // clear variables
}
}
}
You are sure, it is that I want to do!!
Sorry,
I tryed but it hasn' t worked...but I made one New code and I would like if can help me - in this code when the password is correct the "ledPin" will to state HIGH and I access to use the functions (turn on: LED, LED2 and LED3). but now I need to do when one of this code is used set the "ledPin" for the state LOW again...beacuse the functions is true only the "ledPin" is HIGH...
/* It have been building by Messias Silva and some parts with help on Arduino.CC forum
Post on Forum Arduino CC: HOW TO USE KEYPAD FOR MAKE A CONTROL ACCESS SISTEM
messias.gja.sp@hotmail.com */
#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library
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] = {
5, 4, 3, 2};
byte colPins[COLS] = {
8, 7, 6};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
boolean timeout = false;
char myKey;
char datatest[number_of_passwords][length_of_password]={
{
'1','2','2','1' }
, // Murillo Passaword
{
'1','2','2','3' }
, // Jeferson Password
{
'1','1','1','1' }
, // Messias Password
{
'9','9','9','9' } // New costume
};
#define ledPin 9
#define AlarmWrong 10
#define LED 11
#define LED2 12
#define LED3 13
int clearAlarm = 0;
//int i = 0; //variable to count number of key pressed
void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
for(street = 0; street < number_of_passwords * length_of_password; street += 4)
{
for( address =0; address < length_of_password; address++)
{
EEPROM.write(address + street, datatest[street/4][address]);
}
}
address = 0; // clear address
street = 0; // clear street
}
void loop(){
timeout = false;
while(num != 4) // get four numbers
{
myKey = kpd.getKey(); // Check the first keypress
if(myKey){ // This will check to see if a key was pressed
current_time = millis(); // and if so, this will also store the current time.
key[num] = myKey; // This will add the keypress to the array
num++; // This will increment the array for the next keys
while(time < hang_time){ // This will allow the program to get the rest of the
time = millis() - current_time;// of the numbers within the time limit of 5 seconds
myKey = kpd.getKey(); // Check keypress again
if(myKey){
key[num] = myKey;
myKey = 0; // The key must be set to zero, otherwise the code will not work correctly
num++;
}
if(num == 4)
{
time = 0; // reset variable "time" for timer to start over
break;
}
}
if(time >= 5000) // If hang time has reached 5 seconds, display timeout message
{
readAlarmWrong();
cleardata(); // Clear everything
timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
time = 0;
break;
}
}
}
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
digitalWrite(ledPin, HIGH); //correct password
delay(100);
cleardata(); //clears the input array
char key = kpd.getKey();
switch (kpd.getState())// FUNCTIONS
{
kpd.setDebounceTime(50);
if(ledPin == HIGH){
case PRESSED:
if (key == '#') {
digitalWrite(LED, HIGH); // Turn ON LED
delay(500);
digitalWrite(LED, LOW);
delay(500);
cleardata();
break;
}
if(key == '*'){
digitalWrite(LED2, HIGH);//turn ON LED2
delay(500);
digitalWrite(LED2, LOW);
delay(500);
cleardata();
break;
}
if(key == '0'){
digitalWrite(LED3, HIGH);// Turn ON LED3
delay(500);
digitalWrite(LED3, LOW);
delay(500);
cleardata();
break;
}
}
cleardata(); // clear variables
break;
}
}
}
if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
{
readAlarmWrong();
cleardata(); // clear variables
}
}
void cleardata()
{
timeout = false;
address = 0;
street = 0;
user = 1;
num = 0;
return;
}
void readAlarmWrong()
{
digitalWrite(AlarmWrong, HIGH);
delay(500);
digitalWrite(AlarmWrong, LOW);
cleardata();
}
Something in the code is causing it to not work anymore.
Thank's
I got this code, I tryed and treyd many ways then now I got it.
If you want to see I post here XD
It's all for hour, if I need more I'll post here...
Yes post it.
It' is my code XD
/* It have been building by Messias Silva and some parts with help on Arduino.CC forum
Post on Forum Arduino CC: HOW TO USE KEYPAD FOR MAKE A CONTROL ACCESS SISTEM
messias.gja.sp@hotmail.com */
#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library
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] = {
5, 4, 3, 2};
byte colPins[COLS] = {
8, 7, 6};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
boolean timeout = false;
boolean breaking = false;
char myKey;
char datatest[number_of_passwords][length_of_password]={
{
'1','2','2','1' }
, // Murillo Passaword
{
'1','2','2','3' }
, // Jeferson Password
{
'1','1','1','1' }
, // Messias Password
{
'9','9','9','9' } // New costume
};
#define ledPin 9
#define AlarmWrong 10
#define LED 11
#define LED2 12
#define LED3 13
int clearAlarm = 0;
//int i = 0; //variable to count number of key pressed
void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
for(street = 0; street < number_of_passwords * length_of_password; street += 4)
{
for( address =0; address < length_of_password; address++)
{
EEPROM.write(address + street, datatest[street/4][address]);
}
}
address = 0; // clear address
street = 0; // clear street
}
void loop(){
timeout = false;
while(num != 4) // get four numbers
{
myKey = kpd.getKey(); // Check the first keypress
if(myKey){ // This will check to see if a key was pressed
current_time = millis(); // and if so, this will also store the current time.
key[num] = myKey; // This will add the keypress to the array
num++; // This will increment the array for the next keys
while(time < hang_time){ // This will allow the program to get the rest of the
time = millis() - current_time;// of the numbers within the time limit of 5 seconds
myKey = kpd.getKey(); // Check keypress again
if(myKey){
key[num] = myKey;
myKey = 0; // The key must be set to zero, otherwise the code will not work correctly
num++;
}
if(num == 4)
{
time = 0; // reset variable "time" for timer to start over
break;
}
}
if(time >= 5000) // If hang time has reached 5 seconds, display timeout message
{
readAlarmWrong();
cleardata(); // Clear everything
timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
time = 0;
break;
}
}
}
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
digitalWrite(ledPin, HIGH); //correct password
delay(100);
cleardata(); //clears the input array
char key = kpd.getKey();
switch (kpd.getState())// FUNCTIONS
{
kpd.setDebounceTime(50);
if(ledPin == HIGH){
case PRESSED:
if (key == '#') {
digitalWrite(LED, HIGH); // Turn ON LED
delay(1000);
digitalWrite(LED, LOW);
delay(500);
breaking == true;
cleardata();
readbreakfunctions();
break;
}
if(key == '*'){
digitalWrite(LED2, HIGH);//turn ON LED2
delay(1000);
digitalWrite(LED2, LOW);
delay(500);
breaking == true;
cleardata();
readbreakfunctions();
break;
}
if(key == '0'){
digitalWrite(LED3, HIGH);// Turn ON LED3
delay(1000);
digitalWrite(LED3, LOW);
delay(500);
breaking == true;
cleardata();
readbreakfunctions();
break;
}
}
cleardata(); // clear variables
break;
}
}
}
if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
{
readAlarmWrong();
cleardata(); // clear variables
}
}
void cleardata()
{
timeout = false;
address = 0;
street = 0;
user = 1;
num = 0;
return;
}
void readAlarmWrong()
{
digitalWrite(AlarmWrong, HIGH);
delay(500);
digitalWrite(AlarmWrong, LOW);
cleardata();
}
void readbreakfunctions(){
breaking = false;
street = number_of_passwords * length_of_password;
digitalWrite(ledPin, LOW);
delay(1000);
return;
}
Now I would like to know if for me instead use EEPROM I use SD-card...is it to hard?
Because for complement my project I would like to know it...
thank's
I wouldn't be able to test anything, my SD card broke and I never bothered to get a new one. Sorry
OK,
Did you see my code...? And do you like?
I'm very gratefull for your help
XD!!
Code looks good, except "if(ledPin == HIGH){" is not needed, its always true.
Messi_Silva:
It' is my code XD/* It have been building by Messias Silva and some parts with help on Arduino.CC forum
Post on Forum Arduino CC: HOW TO USE KEYPAD FOR MAKE A CONTROL ACCESS SISTEM
#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library
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] = {
5, 4, 3, 2};
byte colPins[COLS] = {
8, 7, 6};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad
const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
boolean timeout = false;
boolean breaking = false;
char myKey;
char datatest[number_of_passwords][length_of_password]={
{
'1','2','2','1' }
, // Murillo Passaword
{
'1','2','2','3' }
, // Jeferson Password
{
'1','1','1','1' }
, // Messias Password
{
'9','9','9','9' } // New costume
};
#define ledPin 9
#define AlarmWrong 10
#define LED 11
#define LED2 12
#define LED3 13
int clearAlarm = 0;
//int i = 0; //variable to count number of key pressed
void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
for(street = 0; street < number_of_passwords * length_of_password; street += 4)
{
for( address =0; address < length_of_password; address++)
{
EEPROM.write(address + street, datatest[street/4][address]);
}
}
address = 0; // clear address
street = 0; // clear street
}
void loop(){
timeout = false;
while(num != 4) // get four numbers
{
myKey = kpd.getKey(); // Check the first keypress
if(myKey){ // This will check to see if a key was pressed
current_time = millis(); // and if so, this will also store the current time.
key[num] = myKey; // This will add the keypress to the array
num++; // This will increment the array for the next keys
while(time < hang_time){ // This will allow the program to get the rest of the
time = millis() - current_time;// of the numbers within the time limit of 5 seconds
myKey = kpd.getKey(); // Check keypress again
if(myKey){
key[num] = myKey;
myKey = 0; // The key must be set to zero, otherwise the code will not work correctly
num++;
}
if(num == 4)
{
time = 0; // reset variable "time" for timer to start over
break;
}
}
if(time >= 5000) // If hang time has reached 5 seconds, display timeout message
{
readAlarmWrong();
cleardata(); // Clear everything
timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
time = 0;
break;
}
}
}
while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
{
if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
{
address++; // if the first number matches, get the next.
}
else // no match was found, try next password
{
address = 0; // set address back to zero and start over
user++; // keep track of users
if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
}
if(address == 4 && !timeout){ // if address equals 4, password was found for said user
digitalWrite(ledPin, HIGH); //correct password
delay(100);
cleardata(); //clears the input array
char key = kpd.getKey();
switch (kpd.getState())// FUNCTIONS
{
kpd.setDebounceTime(50);
if(ledPin == HIGH){
case PRESSED:
if (key == '#') {
digitalWrite(LED, HIGH); // Turn ON LED
delay(1000);
digitalWrite(LED, LOW);
delay(500);
breaking == true;
cleardata();
readbreakfunctions();
break;
}
if(key == '*'){
digitalWrite(LED2, HIGH);//turn ON LED2
delay(1000);
digitalWrite(LED2, LOW);
delay(500);
breaking == true;
cleardata();
readbreakfunctions();
break;
}
if(key == '0'){
digitalWrite(LED3, HIGH);// Turn ON LED3
delay(1000);
digitalWrite(LED3, LOW);
delay(500);
breaking == true;
cleardata();
readbreakfunctions();
break;
}
}
cleardata(); // clear variables
break;
}
}
}
if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
{
readAlarmWrong();
cleardata(); // clear variables
}
}
void cleardata()
{
timeout = false;
address = 0;
street = 0;
user = 1;
num = 0;
return;
}
void readAlarmWrong()
{
digitalWrite(AlarmWrong, HIGH);
delay(500);
digitalWrite(AlarmWrong, LOW);
cleardata();
}
void readbreakfunctions(){
breaking = false;
street = number_of_passwords * length_of_password;
digitalWrite(ledPin, LOW);
delay(1000);
return;
}
Hi Messi,
so the password are hard coded and they cannot change it?
They can be changed, those are just initial passwords. You can use the same method like how it is done in the beginning, to change them later in the code. You will need a way to wait for keypress then one by one store them in the EEPROM. Make sure you set a limit otherwise it will give you problems and may write over other passwords.
I am unable to provide an example at the moment but someone else may be able to.