Pages: [1]   Go Down
Author Topic: User defined password  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
||  Simple Password Entry Using Matrix Keypad
||  4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
||
*/


//* is to validate password   
//# is to reset password attempt

/////////////////////////////////////////////////////////////////

#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <EEPROM.h>
#include <Servo.h>
Servo myservo;
Password password = Password( "1234" );


//////password stuff
byte ee1=0;
byte ee2=0;
byte ee3=0;
byte ee4=0;

byte EEsize = 5;
byte zz;

int password2 =0;

byte pass1=1;
byte pass2=2;
byte pass3=6;
byte pass4=4;

byte in1=0;
byte in2=0;
byte in3=0;
byte in4=0;

byte Stop =0;

byte temp1=0;
byte temp2=0;
byte temp3=0;
byte temp4=0;

byte prevkey1=0;
byte prevkey2=0;
byte prevkey3=0;
byte prevkey4=0;
byte k =0;

byte eKey2=3;


//////////////////////////////////////


const int ledPin =  13;
int ledState = LOW;
long interval = 200;
long interval2 = 2000;
unsigned long currentMillis = 0;
long previousMillis = 0;
long previousMillis2 = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 3; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = { 4,5,7,9 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 3,6,8 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  myservo.attach(10);
  myservo.write(1800);
  Serial.begin(9600);
  //////////password write (only needed once )
   // EEPROM.write(1, pass1);
   // EEPROM.write(2, pass2);
  //  EEPROM.write(3, pass3);
   // EEPROM.write(4, pass4);
  ///////////////Password read
    ee1 = EEPROM.read(1);
    ee2 = EEPROM.read(2);
    ee3 = EEPROM.read(3);
    ee4 = EEPROM.read(4);
 ////////////password calculation   
    password2=((ee1*1000)+(ee2*100)+(ee3*10)+(ee4));
 ////////////password print
Serial.println("password");
Serial.println(password2);
//////////////////////////////////////////////////////////////////

  pinMode(ledPin, OUTPUT);
 
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  k=++k;
  switch (keypad.getState()){
    case HOLD:
    digitalWrite(ledPin,HIGH);
    prevkey1=eKey-'0';
    Serial.println(prevkey1);
  }
  switch (keypad.getState()){
    case RELEASED:
    digitalWrite(ledPin,LOW);
    EEPROM.write(1, prevkey1);
  }
  switch (keypad.getState()){
    case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
       
//temp1=eKey-'0';
//Serial.println(eKey);
//Serial.println(eKey2);
//Serial.println(prevkey);

 // if (Stop==0){
//EEPROM.write(3, temp1);
//digitalWrite(ledPin,HIGH);
//Stop=1;
//}
//eKey2=eKey;
//if ((prevkey=1)&&(eKey2=0)){
  //digitalWrite(13,HIGH);}
 
 
 
 
 
 
 
 
 
 

switch (eKey){
  case '*': checkPassword(); break;
  case '#': password.reset(); break;
          case '0': passwordwrite(); //break;
  default: password.append(eKey);
     }
  }
}

void checkPassword(){
  if (password.evaluate()){
    Serial.println("Success");
    myservo.write(1000);
    digitalWrite(ledPin,HIGH);
    delay(900);
    digitalWrite(ledPin,LOW);
    //myservo.write(2000);
   
   
  }else{
    Serial.println("Wrong");
    myservo.write(2100);
   
    //add code to run if it did not work
  }
}

void passwordwrite(){
  Serial.println("please enter new password");
 
    digitalWrite(ledPin,HIGH);
    password.set("2345");
 



}



Ok,
I posted what is basically this code a while ago asking for help on another topic,
I have a numberpad, with a hardcoded password. which when entered correctly it moves a servo and lights the led on 13.
when incorrect it shuts the servo, I will also decide on a way to shut the servo after another input later in the project.

For now I really want to add a user changeable password into it. I have practiced writing single keystrokes to EEPROM but I havent been able to save multiple keystrokes.

I need to somehow interrupt the code with a button press so that it goes into programming mode, it then needs to record the following four keystrokes, and save them to eeprom so that it can be used for the following password.

Can someone help ? I have already spent a few hours googling it and found an example but you have to pay 10 dollars to subscribe to the site before they show you the code smiley-sad
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Password password = Password( "1234" );

Code:
    ee1 = EEPROM.read(1);
    ee2 = EEPROM.read(2);
    ee3 = EEPROM.read(3);
    ee4 = EEPROM.read(4);
 ////////////password calculation   
    password2=((ee1*1000)+(ee2*100)+(ee3*10)+(ee4));
There is a disconnect right here. The Password library is expecting a string. You are reading and computing a number.

Code:
  switch (keypad.getState()){
    case HOLD:
    digitalWrite(ledPin,HIGH);
    prevkey1=eKey-'0';
    Serial.println(prevkey1);
  }
It doesn't make sense to use a switch statement for ONE case.

Code:
  switch (keypad.getState()){
    case RELEASED:
    digitalWrite(ledPin,LOW);
    EEPROM.write(1, prevkey1);
  }
It doesn't make sense to keep calling the same function. The function returned a value. Select which action to perform based on that value. Don't keep asking for a new value.

Writing the value assigned to prvkey1, which happens only when the key is held, to EEPROM, in the first position, doesn't make sense.

Quote
I need to somehow interrupt the code with a button press so that it goes into programming mode
"Interrupt" isn't the correct term. You need, when some input arrives, to change to "reset password" mode. What input are you planning to use? Wiring an additional switch seems like the best solution.

The time/place to save data in EEPROM is when checkPassword() is called WHEN IN PROGRAMMING mode.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
switch (keypad.getState()){
    case RELEASED:
    digitalWrite(ledPin,LOW);
    EEPROM.write(1, prevkey1);
  }
It doesn't make sense to keep calling the same function. The function returned a value. Select which action to perform based on that value. Don't keep asking for a new value.

This was purely for testing the eeprom writing,

The plan will be to use an external switch for this in the future, Right now I will concentrate on the mechanical part of the project as I currently have a password, albeit hardcoded and non changeable, and I have the servo reacting as I want,

The physical part has now taken priority, as with all things, looks are more important smiley

thanks for your help
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Password password = Password( "1234" );

Code:
    ee1 = EEPROM.read(1);
    ee2 = EEPROM.read(2);
    ee3 = EEPROM.read(3);
    ee4 = EEPROM.read(4);
 ////////////password calculation   
    password2=((ee1*1000)+(ee2*100)+(ee3*10)+(ee4));
There is a disconnect right here. The Password library is expecting a string. You are reading and computing a number.

Hi again,
So I have succesfully got the project to open and shut a mechanism when the correct "password" is entered,
but that password remains hard coded and I would like to be able to change it and store it in the eeprom.
I suppose that the way of doing that is to strore the new string in eeprom and then read it out and set the password as that,

has anyone got any experience with doing this using the password library ? any pointers in the right direction ?
thanks,

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
has anyone got any experience with doing this using the password library ?
"this" would be storing a string in EEPROM, and then reading that string from EEPROM and calling setPassword() with the string?

No, but I know plenty about storing characters in EEPROM, and reading from EEPROM. Using the characters in a call to setPassword() seems pretty simple, then.

I see that you have some steps that need to be implemented.
1) Determine that you are in password-define mode vs. password-entry mode.
2) When the "enter" key is pressed to define the end of the password, you need to check which mode you are in.
3) Store the string in EEPROM. Since each character takes one address, a for loop makes this trivial, regardless of the length of the password string.
4) Read the password from EEPROM (in setup(), most likely). The only trick here is knowing how long the string is. Make the length the 1st value stored, or store a NULL at the end, and read until you find the NULL.
5) Call password.setPassword() with the string read from EEPROM.

Note that setPassword() does NOT copy the password into a field in the Password class. It simply defines a pointer to where the password is stored (somewhere on the heap in the snippet you showed). So, the area where the password is stored must have program lifetime (like a global variable) rather than function lifetime (such as while setup() is running).
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul ,
thanks for the pointers, I will practice saving a string to eeprom, that seems to be the hardest part for me,

Right now I'm working on the physical side of the project, as the appearance of a set password (albeit hard coded) is good enough for the proof of concept and there are many other small hurdles to get over with other aspects of it,

thanks
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
i'm back with the same question again , I have made the physical part of the project acceptable, and added a lot mroe functionality to it, once again a user programmable password is needed,

I have added a "programming mode" via a button push on a second arduino that communicates ( albeit rather weirdly) through a radio modem with the arduino I need to password to be set on.

I can save button pushes to eeprom when in this mode, and I can read them out and compile an integer from them with the line that is mentioned above.

Code:
Password password = Password( "1234" );

I have tried the following line in an attempt to turn the int generated into a string

Code:
Password2=String(password2);

This print what I expect it to when serial.print is used. 
The question is:

is Password2 a string? and can I set the password to Password2?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I can save button pushes to eeprom when in this mode, and I can read them out and compile an integer from them with the line that is mentioned above.
Why do you want to turn the string of characters into an int, when it is the string that you need?

Quote
The question is:

is Password2 a string?
No, it is not. It is a String, which is NOT the same as a string.

Quote
and can I set the password to Password2?
No, because the function that sets the password does not take a String. It takes a string.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I can save button pushes to eeprom when in this mode, and I can read them out and compile an integer from them with the line that is mentioned above.
Why do you want to turn the string of characters into an int, when it is the string that you need?

How do I go about changing the password to those characters then ? say I have one character in eeprom 1, 2, 3 and 4.  How do I put them all into one string and use it ?

Do you have a link to something  that explains the difference bwtween String and string ?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I go about changing the password to those characters then ? say I have one character in eeprom 1, 2, 3 and 4.  How do I put them all into one string and use it ?
Code:
char pass[5]; // Define the array
for(byte n=0; n<4; n++)
{
   pass[n] = eeprom_read(n+1); // Read eeprom to populate the array
}
pass[4] = '\0'; NULL terminate the array

password.set(pass);

Quote
Do you have a link to something  that explains the difference bwtween String and string ?
Do yourself a favor and quit using Strings. Then, you don't need to know the difference between a bloated class ill-suited to the Arduino and a NULL terminated array of chars.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I go about changing the password to those characters then ? say I have one character in eeprom 1, 2, 3 and 4.  How do I put them all into one string and use it ?
Code:
char pass[5]; // Define the array
for(byte n=0; n<4; n++)
{
   pass[n] = eeprom_read(n+1); // Read eeprom to populate the array
}
pass[4] = '\0'; NULL terminate the array

password.set(pass);

Quote
Do you have a link to something  that explains the difference bwtween String and string ?
Do yourself a favor and quit using Strings. Then, you don't need to know the difference between a bloated class ill-suited to the Arduino and a NULL terminated array of chars.

So one is a bloated class that inst well suited to the arduino and one is an array of chars that is null terminated, I guess thats a good enough description,

thanks for the code snippet, I will try it out ,
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,
It appeares to work, there were a couple on typos, but its working well,

It also highlighted another problem in my sketch which I am now fixing,

cheers
Logged

Pages: [1]   Go Up
Jump to: