Go Down

Topic: Extract data from EEPROM to an array (Read 2 times) previous topic - next topic

PaulS

Quote
By doing "EEPROM.write(u*5," you write one number i one byte?

The value in u is multiplied by 5, so that the address being written to is 0, 5, 10, 15, etc.

It should, of course, be u*5 + b, so that the inner loop is writing to 0, 1, 2, 3, and 4 when u is 0, and to 5, 6, 7, 8, and 9, when u is 1.

And, of course, there is a ) missing on the end...

Quote
I made it like this (example for 3 users):

That should work, if the data is written correctly. So, add Serial.print() statements to see what is being read, what is being assigned to user1,  etc. Don't forget that strings need to be NULL terminated, and yours are not (yet), if you are using them as strings, rather than just arrays of chars.

dgelectron

Thanks so much Paul. I made it like this and it works!! It reads from the EEPROM  and assign as I want.
Code: [Select]

  for(i=0; i<5; i++){
    user1[x]=EEPROM.read(i);
      x++;}
      x=0;
    for(i=5; i<10; i++){
    user2[x]=EEPROM.read(i);
      x++;}
      x=0;
    for(i=10; i<15; i++){
    user3[x]=EEPROM.read(i);
      x++;}
      x=0;
}

Yes, this method for 100 user is going to be crazy but at least it works. Next step is to receive sms and modify the EEPROM data if it is neccesary. Then I'll proceed to optimize the code, is insane right now!

Thanks!!

PaulS

Quote
Yes, this method for 100 user is going to be crazy but at least it works.

Yes, it is, and unnecessary. If you are using:
Code: [Select]
for(byte u=0; u<userCount; u++)
{
  for(byte b=0; b<5; b++)
  {
     EEPROM.write(u*5+b, user[u][b]);
  }
}

to store the data, you can use a similar set of loops to read the data.

I'd suggest that you also store the user count in EEPROM, so you have a way of knowing how many users there are. Perhaps write that in address 0, and add a +1 to all the EEPROM.write() addresses, to not overwrite that value.

dgelectron

I'll do it for sure. There is part of the code where I need to save array elements to EEPROM that don't know at the begining cause it arrive to my arduino via sms. For example an sms that sends me a text with the code "67202" I want to save the first element in byte 10, second one in byte 11... I tried like the next code show but it prints me in the serial "93" instead of 7. Do you know what i'm doing wrong?
Code: [Select]

char intento1[]="67202";

EEPROM.write(10, 'intento1[1]'); // also tried EEPROM.write(10, intento1[1]);

Serial.println(EEPROM.read(10));



PaulS

Quote
EEPROM.write(10, 'intento1[1]'); // also tried EEPROM.write(10, intento1[1]);

The first byte, the 6, is in intento1[0].

The code (snippet) does something. "It doesn't work" only means that what it does is not what you want, without defining either what it does OR what you want.

You need to store all 5 characters, don't you?

Knowing where to store them is important. That's why I suggested storing the number of users in 0, so that you could read and update that number if a new user needs to be added. You could also use the number of users to determine the next available address to write to.

Go Up