setting new password and writing it to EEPROM

The intention is to read a 4 position password out of an EEPROM.

The EEPROM has :
• Position [0] : pincheck → if this is 1 then a password has been selected and stored on a previous run. If it contains 0 then a new password of 4 characters must be chosen and stored in the EEPROM positions 1 to 4
• Positions [1 to 4] : a password, it is factoryloaded with “0000” when the first position of the EEPROM [0] is “0”. After a password was entered in this run or before it contains the four digit password.

When a 4 digit password is entered followed by “*” the the function checkPasswordSTAR() runs.

If the first EEPROM position (pincheck) = 0 then I want the entry given via the keyboard used as the new set password.

Now I’m totally lost with this String?? Char?? Password…. I’m new to programming and this is still a bit “in the clouds” for me.
Thanks for helping.

My questions :

• How do I spilt up the new password to be able to write it in The EEPROM positions 1 to 4
• How do I use the password.set function to set the new password?

//http://www.arduino.cc/playground/uploads/Code/Password.zip
//http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <Password.h>
#include <Keypad.h>
#include <EEPROM.h>
Password password = Password( “0000” );
const byte ROWS = 4; const byte COLS = 4;
char keys[ROWS][COLS] = {{‘1’,‘2’,‘3’,‘A’},{‘4’,‘5’,‘6’,‘B’},{‘7’,‘8’,‘9’,‘C’},{’*’,‘0’,’#’,‘D’}};
byte rowPins[ROWS] = { 2,3,4,5 }; byte colPins[COLS] = { 6,7,8,9, };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
Serial.begin(9600);
//Read EEPROM
String pincheck = String (EEPROM.read(0));
String pin1 = String (EEPROM.read(1));
String pin2 = String (EEPROM.read(2));
String pin3 = String (EEPROM.read(3));
String pin4 = String (EEPROM.read(4));
keypad.addEventListener(keypadEvent); //event checker

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

void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
switch (eKey){
case ‘*’: checkPasswordSTAR(); break;
default: password.append(eKey);}}}

void checkPasswordSTAR(){

// I suppose I need to check here if the field pincheck = 0 here and then do the writing to the EEPROM.

if (password.evaluate()){
Serial.println(“password OK”);
else {
Serial.println("* password incorrect");
password.reset();}
}

1 Like

Now I’m totally lost with this String?? Char?? Password…. I’m new to programming and this is still a bit “in the clouds” for me.

Get rid of the String class. Forget that is exists. Learn to use NULL terminated arrays of chars, also known as strings.

There are approximately 14 million C books published that cover strings in great detail. There are on the order of 42 bazillion web sites that do, too.

  String pincheck = String (EEPROM.read(0));
  String pin1 = String (EEPROM.read(1));
  String pin2 = String (EEPROM.read(2));
  String pin3 = String (EEPROM.read(3));
  String pin4 = String (EEPROM.read(4));

Complete nonsense.

All those incorrectly typed variables, and all that wasted memory, go out of scope when setup() ends.

Read the first value as a byte; store it in a global variable. Read the next 4 and store them in a global char array of size 5.

The Password class has a method to set the password. The function name is a bit misleading, since what it really sets is a pointer to the new password’s location in memory. That location MUST be always accessible. That means that it must be global.

That global char array is where you should store the password that is entered as the new password. It is where you should store the password read from EEPROM.

Thanks for your update.
I'm new to programmering so I'll forget all concepten to String.
I'll try it out.

1 Like