EEPROM memory problems

How do I map the arduino eeprom memory, through the Serial.print ()? I am having problems storing data, the truth not is if my arduino is keeping certain data or not, or is this by overriding

(traslate google)

Have a look at the examples at File > Examples > EEPROM. If you can't find the solution to your problem then post your code here USING CODE TAGS(</> button on the toolbar).

The Arduino forum has language specific sections for most languages. You might find it easier to use the section for your native language.

I'm doing an access system, will be implemented in real life, I am sufficiently problems retrieving an arrangement in the eeprom.

Not posting the code since it is very long, they are more 2000 lines

the array declaration:

int password[] = {1,2,3,4,5};     //The system default password
 int password_user[] = {6,7,8,4,5}; // These numbers at some point in my program are entered by the user
  int password_eeprom[]; // Serves to retrieve the password from the eeprom

To change password:

for (int i = 0; i < sizeof(password_user); i++){ //The user enters the password and is stored in the array, followed in the eeprom

                EEPROM.put(i, password_user [i]);
        }

and also keep a variable indicating the system is to change the password:

int estado = 1;

EEPROM.write(0+DISPLACEMENT, Estado); //This what I found, and if kept in you eeprom

DISPLACEMENT is a movement to save the variable in that position, that due to that I have other types of stored data

define DISPLACEMENT 100;

After saving you should retrieve the password from the eeprom

  Estado=EEPROM.read(0+DISPLACEMENT); 

 for (int b = 0; b < sizeof(password_eeprom); b++){
    EEPROM.get(b, password_eeprom[b]);
 }

At the time that the user wants to enter the menu, enter your password

Followed it is compared, to see if it exists in the system

if((strcmp (password, password_user)==0) && (Estado == 0)){ 
          //entering the menu
        }
       
        else if ((strcmp (password_eeprom, password_user)==0) && (Estado == 1)){
          //entering the menu
         }

I think I am having problems with this condition:

for (int i = 0; i < sizeof(password_user); i++){ //The user enters the password and is stored in the array, followed in the eeprom

                EEPROM.put(i, password_user [i]);

}


 for (int b = 0; b < sizeof(password_eeprom); b++){
    EEPROM.get(b, password_eeprom[b]);
 }

Because through the serial.print () saw that I will keep "State"

And as a result I can not enter the menu, when I change password

Please help. My project is almost 99% finished, up to who gave me this problem

Each eeprom address is a byte but an int is more than a byte. You need to allocate enough EEPROM for each entry. The amount needed is the size of each element of the password_user array:

sizeof(password_user[0])

You could use any valid subscript but it's safest to use 0 because any useful array will have at least one element.

You are incorrectly calculating the number of elements in password_user:

sizeof(password_user)

is the number of bytes in the array but since an int is more than one byte that is not the same as the number of elements. The correct way to calculate this is:

sizeof(password_user) / sizeof(password_user[0])

Which is the number of bytes in the array divided by the number of bytes per element.

Try this:

for (int i = 0; i < sizeof(password_user) / sizeof(password_user[0]); i++){ //The user enters the password and is stored in the array, followed in the eeprom
                EEPROM.put(i * sizeof(password_user[0]), password_user [i]);

Note that you shouldn't use larger variable types than necessary. If the values of your array elements will always be 0-255 then there's no point in wasting memory using an int when a byte will work. I don't know if that applies to your application but it's something to consider.

You need to learn the use of Serial.print() as a debugging tool. When you run into problems start adding in a bunch of debugging output until you find out where things went wrong. This would have shown you that your calculations of the number of array elements was wrong.

You are using int in your array. An int is two bytes.

sizeof returns the size (in bytes) of the array. As you use int for 5 digits in your array, the array is 10 bytes. and your for loops count far beyond your array limit.

I think changing your ints for byte is a good start to fix your problem.

pert:
Each eeprom address is a byte but an int is more than a byte. You need to allocate enough EEPROM for each entry. The amount needed is the size of each element of the password_user array:

sizeof(password_user[0])

Yes, sorry, had put "int" instead of "char" to the array, to keep serious asi:

char password_user[5]={1,2,3,4,5}
 for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.get(b*sizeof(password_user[0]), password_user[b]);
 }

 for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.put(b*sizeof(password_user[0]), password_user[b]);
 }

or this way?

 for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.get(b, password_user[b]);
 }

 for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.put(b, password_user[b]);
 }

This saving 3 array.

1- dimensional
2-dimensional

The first is one-dimensional I keep going in the top of the eeprom.

The second is two-dimensional going after the previous one.

The third is unidimensional will last.

The last two we add a shift at the time of save

ej:

#define TAG_DESPLAZAMIENTO_ADM 20
char TagAdm[5];
char password[5];
read:

for (int b = 0; b < sizeof(password[5]); b++){  /
      EEPROM.get(b, TagAdm[b]);
  }

 for (int a = 0; a < sizeof(TagAdm); a++){
      EEPROM.get(a*sizeof(TagAdm)+ TAG_DESPLAZAMIENTO_ADM, TagAdm[a]);
  }

save:

for (int b = 0; b < sizeof(password[5]); b++){                      (/The first I keep it in position 0 eeprom
      EEPROM.put(b, TagAdm[b]);
  }

 for (int a = 0; a < sizeof(TagAdm); a++){                        // The second shifted 20 positions to save
      EEPROM.put(a*sizeof(TagAdm)+ TAG_DESPLAZAMIENTO_ADM, TagAdm[a]);
  }

I do not know the truth, because in real life with my arduino, does not save the first array but if the other

In my simulation with Proteus 8 if it works well

My arduino keeps everything except that array

for (int b = 0; b < sizeof(password[5]);What is the size of a single array element?

In my simulation with Proteus 8 if it works well

This suggests to me that Proteus is broken.

new99:

 for (int b = 0; b < sizeof(password_user[0]); b++){

EEPROM.get(b*sizeof(password_user[0]), password_user[b]);
}

for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.put(b*sizeof(password_user[0]), password_user[b]);
}





or this way?



for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.get(b, password_user[b]);
}

for (int b = 0; b < sizeof(password_user[0]); b++){
    EEPROM.put(b, password_user[b]);
}

Neither one. Try doing this:

Serial.println(sizeof(password_user[0]));

I already told you how you can calculate the number of elements in any array, read what I said:

pert:
The correct way to calculate this is:

sizeof(password_user) / sizeof(password_user[0])

Which is the number of bytes in the array divided by the number of bytes per element.

If you've switched the array type to char and char is one byte then you could get rid of the divisor but it can be nice to have code that works correctly for any type.

new99:
This saving 3 array.

1- dimensional
2-dimensional

I don't see any 2 dimensional arrays in your code. You are trying to cobble together some 2000 line sketch without understanding the basics of working with arrays and EEPROM and doing no testing along the way. That's not the right way to do this stuff. You need to start with simple test sketches with serial output for each of the parts of your project and get each of them working perfectly. Then start slowly combining them, testing all along the way. If you run into a problem start adding in debug output to find out exactly where the problem is and then do research and testing until you can resolve that issue before moving on.

new99:

char password[5];
for (int b = 0; b < sizeof(password[5]); b++){  /

Why are you using password[5]? That's an invalid subscript value because password only has 5 elements and they are zero indexed. Read what I wrote:

pert:
You could use any valid subscript but it's safest to use 0 because any useful array will have at least one element.

Again, you need to learn how to correctly work with arrays before attempting this project.

Thank you very much everyone for your advice, solved the problem.

pert:
Why are you using password[5]? That's an invalid subscript value because password only has 5 elements and they are zero indexed.

In the context of a "sizeof", the subscript is irrelevant. You could just as well have used -1 as a subscript.