EEPROM Password Help

I have 3 buttons (pull-up), each writes a 4 bit binary code to the EEPROM, the number of bits is recorded as passwordLength.

I then have 4th button (pull-up) which prints the password recorded.

What can I do to make this passcode work?
If my password is 1, 2, 3 (000100100011), how do I input 1, 2, 3 and compare it to the EEPROM data?

I am totally new to EEPROM and Arduino altogether.

If someone can point me in the right direction I would appreciate it.

#include <EEPROM.h>
int button_1 = 5;
int button_2 = 6;
int button_3 = 7;
int button_4 = 8;
int bit_1;
int bit_2;
int bit_3;
int bit_4;
int buttonState = 1;
int eepromAddress=1;
int passwordLength=0; 

//write 4 bit binary to the eeprom and record amount of bits as passwordLength
//write_Binary function specifies which button is pressed and writes the corresponding 4 bit data

void write_Binary(int button, int b1, int b2, int b3, int b4){
  
  int buttonState = digitalRead(button);
  while(buttonState == 0){

      EEPROM.write(eepromAddress, b1);
      Serial.print(EEPROM.read(eepromAddress));
      eepromAddress++;
      passwordLength++;
      
      EEPROM.write(eepromAddress, b2);
      Serial.print(EEPROM.read(eepromAddress));
      eepromAddress++;
      passwordLength++;
      
      EEPROM.write(eepromAddress, b3);
      Serial.print(EEPROM.read(eepromAddress));
      eepromAddress++;
      passwordLength++;

      EEPROM.write(eepromAddress, b4);
      Serial.print(EEPROM.read(eepromAddress));
      eepromAddress++;
      passwordLength++;

//total bit length is stored in EEPROM address 0
      EEPROM.write(0, passwordLength);
      
      Serial.print(" ");
      Serial.println("Password Length: ");
      Serial.println(passwordLength);
     
  
  while(buttonState == 0){
      buttonState = digitalRead(button);
      delay(10);
    }
  }
}

void setup() {
  
Serial.begin(9600);
pinMode(button_1, INPUT_PULLUP);
pinMode(button_2, INPUT_PULLUP);
pinMode(button_3, INPUT_PULLUP);
pinMode(button_4, INPUT_PULLUP);

}

void loop() {

//assigns each button a 4 bit binary code
write_Binary(button_1, 0, 0, 0, 1);
write_Binary(button_2, 0, 0, 1, 0);
write_Binary(button_3, 0, 0, 1, 1);

int i = 1;

int passwordLength=EEPROM.read(0);


//prints the password
  while(digitalRead(button_4) == 0){

    while(i <= passwordLength){
      Serial.print(EEPROM.read(i));
      i++;
    while(buttonState == 0){
      buttonState = digitalRead(button_4);
      Serial.println();
      delay(10);
    }
  }
}
}

The reason I am using 4 bit binary is because the password input will eventually be a DTMF encoder.

Your first problem is that you are trying to write int variables (2 bytes each) and EEPROM.write() only writes a single byte. Consider using EEPROM.put() and EEPROM.get() to overcome that problem. You will also need to increment the address by 2 (the size of an int).

Your second problem is that continuously writing to EEPROM will wear out the EEPROM; you have 100,000 writes at your possession for a single EEPROM cell, after that, a cell is no longer guaranteed to work properly. You should only write when there is a change; EEPROM.put does that for you.

Why do you write every single address? Ain't you supposed to store the data in the first N cells?

Lastly, I'm not 100% sure if I understand what you're trying to achieve. Can you rephrase your question?

Thanks for reply.

Sorry I was a bit unclear.

I’m trying to make a password program which saves password to the eeprom. I also want the password to be any desired length. (Within eeprom limits ofc)

The eeprom works like an array right? Should I use an array to test the code before doing anything to the eeprom?

I didn’t think about the ints using 2 bytes. Is there a data-type I can use to write only binary? I am going to be using DTMF to input the password so each key of a 4x4 keypad outputs 4 bits of binary.

Need to do some more research.
Thank you for your help.

All data types are binary, it is all a question of how your handle them. You could use a union data type to access individual bits in a byte like this:

union UNION_TYPE
{
	uint8_t as_byte;
	struct {
		uint8_t bit0 : 1;
		uint8_t bit1 : 1;
		uint8_t bit2 : 1;
		uint8_t bit3 : 1;
		uint8_t bit4 : 1;
		uint8_t bit5 : 1;
		uint8_t bit6 : 1;
		uint8_t bit7 : 1;
	} as_bits;
};

void setup()
{
  UNION_TYPE ut;
  ut.as_bits.bit0 = 1;
  ut.as_bits.bit2 = 1;
  Serial.println(ut.as_byte); //Prints 5 which is the decimal representation of the binary value 101
}

But I'm quite sure that there is a simpler sollution to your problem.

Your second problem is that continuously writing to EEPROM will wear out the EEPROM; you have 100,000 writes at your possession for a single EEPROM cell, after that, a cell is no longer guaranteed to work properly.

really a 100.000 writes is a lot, but i do agree you should not write just to compare a value, only to change it.

Deva_Rishi:
really a 100.000 writes is a lot, but i do agree you should not write just to compare a value, only to change it.

Just see how fast loop() executes :wink:

sterretje:
Just see how fast loop() executes :wink:

Yes but only 1 write per button-press as far as i can tell (there is a ‘block until released’ at the end of the function)

Deva_Rishi:
Yes but only 1 write per button-press as far as i can tell (there is a 'block until released' at the end of the function)

You're right, I ignored some of the reading of the button.

Why not separate the problems of EEPROM storage and password handling, and get each one working separately? With so few buttons, there is no need to "pack bits", just use a byte for each button and the code will be 10 times simpler.

aarg:
Why not separate the problems of EEPROM storage and password handling, and get each one working separately? With so few buttons, there is no need to "pack bits", just use a byte for each button and the code will be 10 times simpler.

Yeah I think that is the biggest problem. After I have a working password problem I will work on EEPROM.

Thank you everyone for the help.