Extract data from EEPROM to an array

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!!

#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");    
}

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

Hi PaulS,

Any suggestion of what i have to do instead??

Thanks!

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.

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

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!!

#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");    
}
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.

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... :fearful:)

  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:

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

And this the second:

    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:

  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);
    }

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.

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];
  }
}

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.

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(u5, user;
** }**
}[/quote]
[u]__By doing "EEPROM.write(u
5," you write one number i one byte?? I mean, byte0 = '6', byte1='7', byte2 = '2'...??__[/u]
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):
**[/u] [u]**for(i=0; i<5; i++){    user1[i]=EEPROM.read(i);    user2[i]=EEPROM.read(i+5);    user3[i]=EEPROM.read(i+10); }**[/u] [u]**
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
```
[u]**  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);
   }**[/u]

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

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

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.

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

  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!!

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:

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.

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?

char intento1[]="67202";

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

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

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.

The code does something that's true. With "doesn't work" I just mean that it isn't what I want, you are right.

EEPROM.write(10, intento1[0]);
EEPROM.write(11, intento1[1]);
EEPROM.write(12, intento1[2]);
EEPROM.write(13, intento1[3]);
EEPROM.write(14, intento1[4]);

Serial.println(EEPROM.read(10));
Serial.println(EEPROM.read(11));
Serial.println(EEPROM.read(12));
Serial.println(EEPROM.read(13));
Serial.println(EEPROM.read(14));

Whits this code the serial prints:

54
55
50
48
50

I already have byte 0 of EEPROM to store users number, that was a good idea!

I guess is something relative to the command "Serial.println" cause even it prints 55, 54, 50... the keypad recognizes it as '6', '7', '2'... So the problem seems to be over!

#include<EEPROM.h>
#include <PN532.h>
#include <SPI.h>
int led = 13;
int EEsize = 1;
int taha;
String input = "";
#define PN532_CS 10
PN532 nfc(PN532_CS);
#define NFC_DEMO_DEBUG 1
void setup(void) {
#ifdef NFC_DEMO_DEBUG
Serial.begin(9600);
Serial.println("Hello!");
#endif
nfc.begin();
pinMode(led, OUTPUT);
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");

while (1); // halt
}
#ifdef NFC_DEMO_DEBUG
Serial.print("Found chip PN5");
#endif
nfc.SAMConfig();
}
void loop(void) {
uint32_t id;long randNumber;
id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
if (id==2999603015)
{
Serial.println("ACCESS GRANTED");
Serial.println(" The unique number is sent to your registered mobile");

for (int i = 0; i < EEsize; i++)
{
randNumber=random(255);
Serial.println(randNumber);
EEPROM.write(i, randNumber);
}
Serial.println();
for (int a=0; a<EEsize; a++)
{
taha = EEPROM.read(a);
Serial.print("EEPROM position: ");
Serial.print(a);
Serial.print(" contains ");
Serial.println(taha);
delay(2000);

//Serial.print("\r");
//delay(1000);
//Serial.println("AT+CMGF=1\r");
//delay(1000);
//Serial.println("AT+CMGS="+91944xxxxxx8"\r"); //Number to which you want to send the sms
//delay(1000);
//Serial.println(" randNumber \r"); //The text of the message to be sent
//delay(1000);
//Serial.write(0x1A);
//delay(1000);
//
if(Serial.available()){
input += (int)Serial.read();
delay(30);
//if(input =="taha");
Serial.println("The unique number you entered is correct");
}

}
}

else if(id!=0)
{
Serial.println("ACCESS DENIED");
delay(2000);
digitalWrite(led, HIGH);
}
else
{
Serial.println(" PLEASE DISPLAY YOUR MOBILE");
delay(2000);
}
}
hey!how can i compare a data stored in eeprom using serial port monitor..... i am using serial functions to read and then compairng with eeeprom value "taha" but it is not comparing...can u help me out

@abdulmajidtaha: Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

http://arduino.cc/forum/index.php/topic,158767.0.html

  • Moderator