Go Down

Topic: EEPROM hold more then 255 as a value? (Read 1 time) previous topic - next topic

Starfire

Is there a way to save a value larger then 255 into the eeprom?  all I see is that is the max, I don't care about using more space as I'm not writing a lot of data but values up to 1500 or so.

halley

#1
Apr 13, 2009, 04:41 am Last Edit: Apr 13, 2009, 04:42 am by halley Reason: 1
A byte is eight bits.  It can hold any number from 0 to 255.

On Arduino, an int is two bytes, and can hold -32768 to 32767.

On Arduino, a long is four bytes, and can hold -2147483648 to 2147483647.

If you are using a variable that is bigger than a byte, then you'll have to store the value in more than one byte.

Arduino's EEPROM is addressed by the byte, so you can use up consecutive addresses to store values in other variable types.

http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Starfire

Thanks for the link, but will this work for writing 8 values?  Sorry I'm a noob trying to learn.

FusiveResonance

Quote
Thanks for the link, but will this work for writing 8 values?  Sorry I'm a noob trying to learn.


Yes, using that function you can access any memory location in the eeprom. The eeprom is capable of holding 512 bytes, hence has 512 memory addresses ranging from 0 - 511.

Quote
I'm not writing a lot of data but values up to 1500 or so.


So let's say that you were using ints to store these values. Let's assume that you only have 3 values that you wish to write, a, b and c.

Code: [Select]
#include <EEPROM.h>

int a = 1500, b = 1600, c = 1700;

void setup()
{
     int eeprom_address = 0; //We will start writing at the first memory address
     
     eeprom_address += EEPROM_writeAnything(eeprom_address, a); //write a to memory and then updates eeprom address
     eeprom_address += EEPROM_writeAnything(eeprom_address, b);
     eeprom_address += EEPROM_writeAnything(eeprom_address, c);
}

void loop()
{
     //nothing to loop as we only store these values into eeprom once when the board boots up.
}


EEPROM_writeAnything returns the number of bytes that were transferred. Now, for an int we know that only 2 bytes will be used so I could have written the following:

Code: [Select]

EEPROM_writeAnything(0, b);
EEPROM_writeAnything(2, b);
EEPROM_writeAnything(4, c);


I'd rather not hardcode the memory addresses being used, (and neither should you) and perhaps in the future you will decide to write different data types which will use a different number of bytes. By having a variable known as eeprom_address that updates each time something is written to eeprom, we are not hard coding anything, hence leading to an efficient implementation.

P.S - Halley please correct if I've posted anything incorrect. This is the first time I'm using your function. Good job on the template though.




Starfire

Thanks so much I understand now, couldn't figure out how to read it back in   ;D :D

halley

#5
Apr 13, 2009, 04:34 pm Last Edit: Apr 13, 2009, 04:35 pm by halley Reason: 1
FusiveResonance, you got it exactly right.  Thanks for explaining the return value, and showing that you don't need to use it, but it's quite useful.

Starfire, EEPROM_readAnything() is exactly the same, it just fills the variables you name, instead of storing the values in the variables.

Starfire

Yup works like a charm, much thanks!

Go Up