Pages: [1]   Go Down
Author Topic: EEPROM hold more then 255 as a value?  (Read 1401 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 12, 2009, 09:42:35 pm by halley » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 175
---
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 13, 2009, 09:35:01 am by halley » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup works like a charm, much thanks!
Logged

Pages: [1]   Go Up
Jump to: