EEPROM anything working example?

Hello friends,
I've been doing some projects using Arduino from some time now and have stumbled across the EEPROM.h and EEPROMAnything.h. I have a project whereby I have to update the distance in Kilometers onto the ROM. It is basically a Speedometer/Odometer/Tachometer to store trip distances and total distance ever rode on my motorbike. I read about PROGMEM and EEPROM, but due to lesser write/replace cycles for PROGMEM, I am wanting to use EEPROM.(10,000 cycles for PROGMEM and 1,00,000 for EEPROM).

I am successful in testing the sample EEPROM.h code, but it is limited to the number 255 for 1 byte on ROM, and hence I wanted to test the EEPROMAnything.h, but I'm not able to find the dotH files and sample code to test for long int and string values. I wanted to know how much write cycles are used to write/replace for example a long int (32,46,054) and how long can I use the same chip before replacement. Can anybody please help if you have already done something in this area? Also does anybody has a trick for how frequently should the KM values be written into the ROM, as I don't want to run out of write cycles soon, and don't want to bother pressing a button to write values from the RAM to ROM everytime I turn off my bike.

I read about PROGMEM and EEPROM, but due to lesser write/replace cycles for PROGMEM, I am wanting to use EEPROM.

Read/write cycles has nothing to do with it. PROGMEM is read-only.

but I'm not able to find the dotH files and sample code to test for long int and string values.

The functions take a pointer to the data to be stored. That data can be ANYTHING - a long, a string, an array of structs.
What have you tried?

...but it is limited to the number 255 for 1 byte...

Yes, the byte is limited, 0-255, everywhere.
If you want to read/write different size from/to EEPROM you can use AVR functions (..avr\eeprom.h) e.g. eeprom_read_block ...
EEPROM is good to use only for data wich you don't want to loose when chip is off or during a reset (e.g. some setup or the last data). The EEPROM is wearing after 100,000 cycles +/-. I think it is pretty enough for many application. Normally, program is working with data in RAM.
Flash memory (PROGMEM) is keeping program and data which are constant during program lifetime.

PaulS:

I read about PROGMEM and EEPROM, but due to lesser write/replace cycles for PROGMEM, I am wanting to use EEPROM.

Read/write cycles has nothing to do with it. PROGMEM is read-only.

Why do you say PROGMEM is read-only? Isn't PROGMEM where our sketches are stored? Here also it's written: http://arduino.cc/en/Reference/PROGMEM

but I'm not able to find the dotH files and sample code to test for long int and string values.

The functions take a pointer to the data to be stored. That data can be ANYTHING - a long, a string, an array of structs.
What have you tried?

I don't understand fully what you mean here.

Budvar10:
If you want to read/write different size from/to EEPROM you can use AVR functions (..avr\eeprom.h) e.g. eeprom_read_block ...
EEPROM is good to use only for data wich you don't want to loose when chip is off or during a reset (e.g. some setup or the last data). The EEPROM is wearing after 100,000 cycles +/-. I think it is pretty enough for many application. Normally, program is working with data in RAM.
Flash memory (PROGMEM) is keeping program and data which are constant during program lifetime.

I have tried the avr/eeprom.h, but I can't get it working. Do you have a sample working code?

I think 100,000 cycles are enough only if we optimize our coding and logic of data management. Here, I wish I could update in realtime the incremental values from my distance calculator and hence I'm not sure how do I base my writes to the ROM, as the distance will constantly keep on increasing. I had thought to update on the ROM based on when the speed sensor stops reading values for some time (when speed=0 for some time) but that would also include stopping on signals which is okay.

greatidea:
Why do you say PROGMEM is read-only? Isn’t PROGMEM where our sketches are stored? Here also it’s written:

The program memory flash can only be written by an external programmer or by the Arduino bootloader. Once your sketch is running, it cannot be changed, and so it is effectively read-only as far as a conventional Arduino sketch writer is concerned. It also has some other drawbacks compared to EEPROM, mainly that it can only be erased and written in large chunks, not byte by byte as is possible with EEPROM.

I have tried the avr/eeprom.h, but I can’t get it working. Do you have a sample working code?

#include <avr/eeprom.h>

int myValuePos = 12345;//int in sram
int myValueNeg = -12345;

EEMEM uint16_t saveMyValuePos; //integer in eeprom; avr will control address
EEMEM uint16_t saveMyValueNeg;

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

  eeprom_write_word( & saveMyValuePos , myValuePos);
  eeprom_write_word( & saveMyValueNeg , myValueNeg);

  int valPos =eeprom_read_word(& saveMyValuePos);
  int valNeg =eeprom_read_word(& saveMyValueNeg);

  Serial.println(valPos);
  Serial.println(valNeg);
}

void loop()
{
}
EEMEM uint16_t saveMyValueNeg;

Saving a negative value in an unsigned variable? How's that work for you?

PaulS:
Saving a negative value in an unsigned variable? How's that work for you?

Since he copies the value to an int before doing anything with it, it likely works just fine.

  int valNeg =eeprom_read_word(& saveMyValueNeg);
  Serial.println(valNeg);

Saving a negative value in an unsigned variable? How’s that work for you?

The example works perfectly. I think it is because the EEMEM declaration is just for memory allocation and the variable declaration is controlling.
Here’s another piece of code , from jack Christensen, I think which uses an unsigned EEMEM declaration for a negative number. It also works as intended

#include <avr/eeprom.h>

EEMEM unsigned long foo;    //a variable stored in EEPROM
long bar1, bar2;            //regular variables in SRAM

void setup(void)
{
    Serial.begin(9600);
    
    bar1 = -12345678;
    eeprom_write_dword( &foo, bar1 );
    bar2 = eeprom_read_dword( &foo );
    Serial.println(bar2);
}

void loop(void)
{
}

cattledog:

I have tried the avr/eeprom.h, but I can’t get it working. Do you have a sample working code?

#include <avr/eeprom.h>

int myValuePos = 12345;//int in sram
int myValueNeg = -12345;

EEMEM uint16_t saveMyValuePos; //integer in eeprom; avr will control address
EEMEM uint16_t saveMyValueNeg;

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

eeprom_write_word( & saveMyValuePos , myValuePos);
  eeprom_write_word( & saveMyValueNeg , myValueNeg);

int valPos =eeprom_read_word(& saveMyValuePos);
  int valNeg =eeprom_read_word(& saveMyValueNeg);

Serial.println(valPos);
  Serial.println(valNeg);
}

void loop()
{
}

Hi cattledog,

This simply works great. This was what I was looking for. Do you know how many write cycles does eeprom_write_word consume internally? I need to optimize my final sketch because I’m going to store values which will be updated couple of times per day and the Atmega chip once installed, will not be getting a replacement soon just because it’s eeprom won’t store values reliably after it’s 100,000 write cycles.
How do I modify this to add strings with long int?

You might take a peek at the examples presented here:
http://playground.arduino.cc/Code/EepromUtil

Do you know how many write cycles does eeprom_write_word consume internally?

I think that it is variable because of eeprom write speed is slow and function has a wait loop while the write ends.
There is also an update version of write functions (e.g. eeprom_update_byte) which can save some write cycles - write only if byte data differs. But once again, try to avoid of frequent writes to EEPROM.
Another solution for you should be a use of the SD card.

How do I modify this to add strings with long int?

Did you mean long int array? Read/write as block. For example use an union of long int array and byte array. Or simply cast to (byte *), but compiler will complain.