How to use EEPROM write anything

Hi Guys,

I’m trying to write a string to the eeprom but I’m not having much success with my sketch, I’ve put it togeather form examples I’ve seen but I just wont work, what have I done wrong?

#include <EEPROM.h>
#include "EEPROMAnything.h"

char string1; //where EEPROM_readAnything will save data to

void setup()
{
  char* fox = "The quick brown fox"; //SEND TO EEPROM
  byte len = (byte) strlen (fox);
  EEPROM_writeAnything (0, len);
  EEPROM_writeAnything (1, fox);
}
void loop()
{
   if(EEPROM_readAnything(0, string1))
    Serial.println("marker"); //SEE if anything happens on SM
    Serial.println(string1);
}

Thanks
Cal

Hi Guys,

I have compose this sketch but it doesn’t seem to like the fact that I want to write data*,DEC to EEPROM*
Here is the sketch
```
*#include <EEPROM.h>

int address = 0;

void setup()
{
  Serial.begin(9600);
 
  String data = “Hello There!”;//string that needs to be on eeprom
 
  for(int i = 0; i < data.length(); i++)  {
      if (i > 0)  {
        long dd = data[i],DEC; //Copy string to long dd
        EEPROM.write(address, dd); //write string to
        address = address + 1;// move to next eeprom address
        }
      }
}

void loop()
{

}*
* *And here is the error code* *
*Arduino: 1.5.8 (Windows 7), Board: “Arduino Uno”

In file included from C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,
                from C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,
                from C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:223,
                from eeprom_write.ino:3:
eeprom_write.ino: In function ‘void setup()’:
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Print.h:29:13: error: expected unqualified-id before numeric constant
#define DEC 10
            ^
eeprom_write.ino:13:27: note: in expansion of macro ‘DEC’
Error compiling.

This report would have more information with
  “Show verbose output during compilation”
  enabled in File > Preferences.*
```
any one know how I can do this a different way that wont blow my brain cells lol (TGIF) :grin: :grin: :grin:

What is this supposed to do

long dd = data[i],DEC; //Copy string to long dd

How does “hello There!” get translated into a long value. And what relevance does “,DEC” have here?

Surely this would make more sense

#include <EEPROM.h>

int address = 0;

void setup()
{
  Serial.begin(9600);
  
  char data[] = "Hello There!";//string that needs to be on eeprom 
  for(int i = 0; i < strlen(data); i++)  
    {
    EEPROM.write(address, data[i]); //write string to 
    address = address + 1;// move to next eeprom address
    }
}

void loop()
{

}

The Original sketch I composed was this

void setup() {
  Serial.begin(9600);
  
  String data = "Trigger 1 has been set off!";
  
  for(int i =0; i < data.length(); i++)  {
    if (i > 0)  {
    Serial.println(data[i],DEC);
    }
  }
}

void loop() {


}

It converts Strings to ASCII

So Now I want to save the ASCII values to EEPROM (1 character : 1 address… right?)

        long dd = data[i],DEC; //Copy string to long dd

It looks like you are expecting this line to convert the char from the string into a long decimal value.

Firstly that is not how you would do it. You cannot just invent your own syntax for doing things.
Secondly a long value would not fit in a single byte EEPROM address
Thirdly the char already has a numeric value between 0 and 255 so there is no need to convert it before writing it.

Why are you not writing data[0] to the EEPROM ?
There is also a case for using C style strings (zero terminated arrays of chars) instead of Strings (objects created by the Strings library) in anything but trivial programs so you might as well get used to using them.

Serial.println is a function (hence the brackets) that can take various types of parameters.

long dd = is simply a value assignment. You can't just add extra parameters and expect them to mean something to the = operator.

KenF, your code worked like a charm, but please can you explain how you sketch converts String data to ASCII?

Sorry... I'm a NOOB of note! :'( :'( :fearful: :fearful:

senorcallum: KenF, your code worked like a charm, but please can you explain how you sketch converts String data to ASCII?

It doesn't have to. It's already stored that way. In computer memory terms there's no such thing as a character. EVERYTHING is a number. When you store a character you're actually storing it's ascii equivalent.

So by changing String to char it wrote the ASCII equivalents to eeprom without telling it to? how would I get it from EEPROM Read to char?

senorcallum: So by changing String to char it wrote the ASCII equivalents to eeprom without telling it to? how would I get it from EEPROM Read to char?

You're still not getting this are you. CHARS are just how they get displayed. What is stored is ALWAYS a number.

When you read it back, read it into a char or char array. When you print, the print function will see that you're trying to print characters and THAT'S when the numbers get converted to characters.

NO computer EVER stores characters. It stores numbers, whether they're on a hard disk, in EEPROM, in a variable running in your browser. It doesn't matter where they are, they're ALWAYS numbers.

It's only when they need to be displayed to human beings that they need to be converted into dots on a screen that represent characters of the alphabet.

I see the light :open_mouth: :open_mouth: :open_mouth:

char string1; //where EEPROM_readAnything will save data to

Pretty dumb name for something that is NOT a string.

   if(EEPROM_readAnything(0, string1))

You read the length that you stored as a byte into a char variable. Why do you expect that to do anything different from what it actually did?

By the way, "it didn't work" is just too lame for words. The code DID work. It just didn't do what you expected. In 99.999% of cases where that happens, it is your expectations that are wrong.

Just a demo

void setup()
{
Serial.begin(9600);  

char stringy[27];
byte numbers[27];
int n;
for (0 ;  n<27 ; n++)
  {
   stringy[n] = 'A' + n;
   numbers[n] = 'A' + n;    
  }
stringy[26]=0;

Serial.println(stringy);

for(n=0; n<26; n++)
  {
   Serial.print(stringy[n]);
   Serial.print(numbers[n]);
   if(numbers[n] == stringy[n])
     Serial.println(" Yes these are EXACTLY THE SAME");
   else
     Serial.println(" No These are different");
  }
}

void loop(){}

outputs

ABCDEFGHIJKLMNOPQRSTUVWXYZ
A65 Yes these are EXACTLY THE SAME
B66 Yes these are EXACTLY THE SAME
C67 Yes these are EXACTLY THE SAME
D68 Yes these are EXACTLY THE SAME
E69 Yes these are EXACTLY THE SAME
F70 Yes these are EXACTLY THE SAME
G71 Yes these are EXACTLY THE SAME
H72 Yes these are EXACTLY THE SAME
I73 Yes these are EXACTLY THE SAME
J74 Yes these are EXACTLY THE SAME
K75 Yes these are EXACTLY THE SAME
L76 Yes these are EXACTLY THE SAME
M77 Yes these are EXACTLY THE SAME
N78 Yes these are EXACTLY THE SAME
O79 Yes these are EXACTLY THE SAME
P80 Yes these are EXACTLY THE SAME
Q81 Yes these are EXACTLY THE SAME
R82 Yes these are EXACTLY THE SAME
S83 Yes these are EXACTLY THE SAME
T84 Yes these are EXACTLY THE SAME
U85 Yes these are EXACTLY THE SAME
V86 Yes these are EXACTLY THE SAME
W87 Yes these are EXACTLY THE SAME
X88 Yes these are EXACTLY THE SAME
Y89 Yes these are EXACTLY THE SAME
Z90 Yes these are EXACTLY THE SAME