Hello, I want to write a string array in to the EEPROM and read it again. Can anyone help me on this.
The array looks like as follows. It consist of RFID tag numbers.
String myTags[] ={"37376B34","7AA29B1A"};
Hello, I want to write a string array in to the EEPROM and read it again. Can anyone help me on this.
The array looks like as follows. It consist of RFID tag numbers.
String myTags[] ={"37376B34","7AA29B1A"};
I would start by not using Strings and use arrays of chars instead.
Then I would use the EEPROM.put() function to save it and load it back with the EEPROM.get() function
#include <EEPROM.h>
char * myTags[] = {"37376B34", "7AA29B1A"};
char * reloadedTags[2];
void setup()
{
Serial.begin(115200);
while (!Serial);
EEPROM.put(0, myTags);
EEPROM.get(0, reloadedTags); //to confirm that it works
for (int tag = 0; tag < 2; tag++)
{
Serial.println(reloadedTags[tag]);
}
}
void loop()
{
}
UKHeliBob:
I would start by not using Strings and use arrays of chars instead.
Then I would use the EEPROM.put() function to save it and load it back with the EEPROM.get() function#include <EEPROM.h>
char * myTags[] = {"37376B34", "7AA29B1A"};
char * reloadedTags[2];
void setup()
{
Serial.begin(115200);
while (!Serial);
EEPROM.put(0, myTags);
EEPROM.get(0, reloadedTags); //to confirm that it works
for (int tag = 0; tag < 2; tag++)
{
Serial.println(reloadedTags[tag]);
}
}
void loop()
{
}
That won't work, myTags
should be an array of pointer to const char, and you're only storing the pointers to EEPROM, you need to copy the strings themselves as well.
Pieter
Are you saying that the code does not work ?
In any case, if the tags are const then why save them to EEPROM in the first place ?
UKHeliBob:
Are you saying that the code does not work ?
The code might work, but the data in EEPROM is useless, it's just two pointers. The actual strings aren't stored, and the stored pointers are meaningless as soon as you change anything about the program.
UKHeliBob:
In any case, if the tags are const then why save them to EEPROM in the first place ?
String literals are always const. You can only access them through a read-only pointer, and the compiler will tell you that:
warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char * myTags[] = {"37376B34", "7AA29B1A"};
^
In OP's program, the Strings aren't const, but in your code they are string literals, so you have to use pointers to const.
The code might work, but the data in EEPROM is useless, it's just two pointers
Now I think about you are, of course, correct
How about
#include <EEPROM.h>
char myTags[][9] = {"37376B34", "7AA29B1A"};
char reloadedTags[2][9];
void setup()
{
Serial.begin(115200);
while (!Serial);
EEPROM.put(0, myTags);
EEPROM.get(0, reloadedTags); //to confirm that it works
for (int tag = 0; tag < 2; tag++)
{
for (int c = 0; c < 9; c++)
{
Serial.print(reloadedTags[tag][c]);
}
Serial.println();
}
}
void loop()
{
}
As to the data types and what needs to be saved, I await the OPs reply
Hey UKHeliBob, I tried your code. But it is not working properly. It doesn't print the array values back.
UKHeliBob:
Now I think about you are, of course, correctHow about
#include <EEPROM.h>
char myTags[][9] = {"37376B34", "7AA29B1A"};
char reloadedTags[2][9];
void setup()
{
Serial.begin(115200);
while (!Serial);
EEPROM.put(0, myTags);
EEPROM.get(0, reloadedTags); //to confirm that it works
for (int tag = 0; tag < 2; tag++)
{
for (int c = 0; c < 9; c++)
{
Serial.print(reloadedTags[tag][c]);
}
Serial.println();
}
}
void loop()
{
}
As to the data types and what needs to be saved, I await the OPs reply
chamodmelaka:
Hey UKHeliBob, I tried your code. But it is not working properly. It doesn't print the array values back.
You've probably done something wrong
It doesn't print the array values back.
What baud rate do you have the Serial monitor set to ?
UKHeliBob:
What baud rate do you have the Serial monitor set to ?
Sorry UKHeliBob. Its my mistake. I used the wrong baud rate. The code is working properly. Thank you very much.
Hey another help please. If I want to check whether a RFID tag number which is read from the RFID reader is in the EEPROM and if it is there take a action, How can I do that.
This code is for the array which I had defined in the code. Here StrUID is the variable which the read tag number is stored.
how to check whether it is in the EEPROM.
void checkAccess (String StrUID) { //Function to check if an identified tag is registered to allow access
boolean granted = false;
for (int i=0; i <= (myTagsSize-1); i++){ //Runs through all tag ID numbers registered in the array
if(myTags[i] == StrUID){
open();
granted = true;
}
}
if (granted == false){ //If the tag is not found
// Serial.println ("Access Denied");
}
}
chamodmelaka:
If I want to check whether a RFID tag number which is read from the RFID reader is in the EEPROM and if it is there take a action, How can I do that.
You cannot take any action on EEPROM until it is read into RAM.
Having tested in UNO, it is found that your sketch of Post#1 and sketch of Post#5 are showing identical output on the Serial Monitor. Which sketch is flawless and reliable?
The code in post #1 has a fatal flaw in it because it is the pointers that are being saved, not the data, hence post #5 which saves the actual data
GolamMostafa:
@UKHeliBobHaving tested in UNO, it is found that your sketch of Post#1 and sketch of Post#5 are showing identical output on the Serial Monitor. Which sketch is flawless and reliable?
Hey, It is correct. Both of the codes work. which code is most suitable. Isn't that the code the post#1 saves the tag numbers to the EEPROM. because I will need to store the RFID tag numbers automatically in to EEPROM if it is mot in the EEPROM. Which will be most suitable code for this.
Hey, It is correct. Both of the codes work. which code is most suitable. Isn't that the code the post#1 saves the tag numbers to the EEPROM. because I will need to store the RFID tag numbers automatically in to EEPROM if it is mot in the EEPROM. Which will be most suitable code for this.
Post #5.
The best way to test EEPROM code is to run your reading code after a power cycle from the writing code. If you do this, you will see the difference between #1 and #5.
Without a powercycle, the pointers read from the eeprom will still point to the correct data.
After a power cycle, you will see that the actual data was not stored.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.