Pages: [1] 2   Go Down
Author Topic: Extract data from EEPROM to an array  (Read 2208 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm able to save data properly but when I compare "user1" with "intento" it doesn´t work...any idea of what i'm doing wrong??

Thanks!!
Code:
#include <EEPROM.h>
#include <String.h>

char intento[]="67202";
int i;
char user1[5];
char data1[5];
int data;

void setup()
{
  Serial.begin(115200); //Configura la velocidad del puerto serie   
 
  EEPROM.write(0, 6);
  EEPROM.write(1, 7);
  EEPROM.write(2, 2);
  EEPROM.write(3, 0);
  EEPROM.write(4, 2);
 
  Serial.println("Asignando...");
    for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    Serial.print(user1[i], DEC);
    }
}

void loop(){
  delay(1000);
  if(strcmp(user1,intento)==0){
    Serial.println("OK");
  }
  else
    Serial.println("Fail");   
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
any idea of what i'm doing wrong??
Sure. 6 != '6', 7 != '7', 2 != '2', and 0 != '0'.

You are storing 6, 7, 2, 0, and 2 in EEPROM, and then hoping the stored values will equal '6', '7', '2', '0', and '2'.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,

Any suggestion of what i have to do instead??

Thanks!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any suggestion of what i have to do instead??
Well, you could store '6', '7', '2', '0', and '2' in EEPROM, instead.

Or, you could multiply and add to make 6, 7, 2, 0, and 2 into 67202, stored in a long, and do a numerical comparison instead of a string comparison.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh at first i didnt' understand what did yo mean! ok I got it. Now it prints OK. Thanks so much Paul!
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, here I am again.

For one user it works perfect, but when I add more user it doesn´t work. I believe the problem is in the setup while reading the EEPROM the way I show in the code. Is it wrong to write "EEPROM.read(i+5);"?? Is the way I tought to write all my users codes in EEPROM. Anny suggestion of how to do it properly?

Thanks in advance!!

Code:
#include <EEPROM.h>

char user1[5],user2[5],user3[5],user4[5],user5[5];

char attempt1="67202";
char attempt2="62601";
char attempt3="60920";


EEPROM.write(0, '6'),(1, '7'),(2, '2'),(3, '0'),(4, '2');
EEPROM.write(5, '6'),(6, '2'),(7, '6'),(8, '0'),(9, '1');
EEPROM.write(10, '6'),(11, '0'),(12, '9'),(13, '2'),(14, '0');

void setup()
{
  Serial.begin(115200); //Configura la velocidad del puerto serie    
  
    Serial.println("Asignando...");
    for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    user2[i]=EEPROM.read(i+5);
    user3[i]=EEPROM.read(i+10);
    }
}
void loop(){
if(strcmp(attempt, user1) == 0 && strcmp(attempt2, user2) == 0 && strcmp(attempt3, user3) == 0);
Serial.println("OK");
  else
    Serial.println("Fail");    
}
« Last Edit: January 09, 2013, 04:48:27 am by dgelectron » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
EEPROM.write(0, '6'),(1, '7'),(2, '2'),(3, '0'),(4, '2');
I love the creative attempt to invent new ways of doing things.

What changes did you make to your compiler to support this?

You need to store data for multiple user just like you do for one user, except at different addresses.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, that was too much creative...I pressed the "Verify" buttom in the compiler and get no error so I thought that must be ok...

So right now I write in EEPROM like this for 2 users (my idea is to have 100...  smiley-eek-blue)
Code:
 EEPROM.write(0, '6');
  EEPROM.write(1, '7');
  EEPROM.write(2, '2');
  EEPROM.write(3, '0');
  EEPROM.write(4, '2');
  EEPROM.write(5, '6');
  EEPROM.write(6, '2');
  EEPROM.write(7, '6');
  EEPROM.write(8, '0');
  EEPROM.write(9, '1');
 
I try two different ways to read the EEPROM and assign values to my users arrays but neither works. This is the first one:
Code:
for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    user2[i]=EEPROM.read(i+5);
}
And this the second:
Code:
   for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    }
    for(z=0; z<5; z++){
    user2[z]=EEPROM.read(i);
    i++;
    }

But when I enter the code in my keypad it doesnt recognize it. What surprise me the most is that if I just write one user in EEPROM it works. Thsi code works perfect:
Code:
  EEPROM.write(0, '6');
  EEPROM.write(1, '7');
  EEPROM.write(2, '2');
  EEPROM.write(3, '0');
  EEPROM.write(4, '2');
 
    Serial.println("Asignando...");
    for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    }
« Last Edit: January 09, 2013, 07:47:34 am by dgelectron » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So right now I write in EEPROM like this for 2 users (my idea is to have 100...  smiley-eek-blue)
There are ways to shorten that.

Code:
char user[100][6] = { "67202", "35426", "52565", "44234", "84583", "51511", "81510", ... , "00239" };
for(byte u=0; u<userCount; u++)
{
  for(byte b=0; b<5; b++)
  {
     EEPROM.write(u*5, user[u][b];
  }
}

Quote
I try two different ways to read the EEPROM and assign values to my users arrays but neither works.
What is read? You need to do a better job of defining what "doesn't work" means.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
char user[100][6] = { "67202", "35426", "52565", "44234", "84583", "51511", "81510", ... , "00239" };
for(byte u=0; u<userCount; u++)
{
  for(byte b=0; b<5; b++)
  {
     EEPROM.write(u*5, user;
  }
}

By doing "EEPROM.write(u*5," you write one number i one byte?? I mean, byte0 = '6', byte1='7', byte2 = '2'...??

With "read" I mean extract data from EEPROM, read the values of the EEPROM and assign it to my users list.

The idea is to not declare the users code numbers in the flash memory (because the users can be modify by sms), just write it once in EEPROM (by the way you teach me) and then delete the part of the code that writes in EEPROM and load the program in the Arduino again. So each time the program runs the first thing to do is read the values of the EEPROM and assign it to my users list. I made it like this (example for 3 users):
Code:
for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    user2[i]=EEPROM.read(i+5);
    user3[i]=EEPROM.read(i+10);
}
With "doesn't work" I mean that the keypad did not recognize the codes. At first I tried just with one user and the keypad recognize it
Code:
 EEPROM.write(0, '6');
  EEPROM.write(1, '7');
  EEPROM.write(2, '2');
  EEPROM.write(3, '0');
  EEPROM.write(4, '2');

   for(i=0; i<5; i++){
    user1[i]=EEPROM.read(i);
    }

So I attempt to do the same with more users but "doesn't work". I really hope I explain my self a little better. Thanks for your time PaulS, thanks so much.
« Last Edit: January 10, 2013, 03:41:12 am by dgelectron » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much Paul. I made it like this and it works!! It reads from the EEPROM  and assign as I want.
Code:
  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!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char intento1[]="67202";

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

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: