Question about using a separte file or making a library

Hi I designed about 6 functions for setting and retrieving values from the eeprom. Should I declare them external or make a library for them. I'm going to possible reuse the code in a similar project. Is there any rules or conventions on this? I need to store and retrieve a long variable which I divided by 100,000 10,000 1000 100 10 1 to put the result in each byte of eeprom. I'm looking for your thoughts and ideas. Thanks Don

I need to store and retrieve a long variable which I divided by 100,000 10,000 1000 100 10 1 to put the result in each byte of eeprom.

So, you are using 7 bytes of EEPROM to store a 4 byte variable.

I don't suggest making a library, yet. There are already templates for storing anything in EEPROM. What motivated you to make a less efficient version?

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

Hi Paul
I will read that over I never saw that before.
I was using 6 bytes to store that number.
This is the function to retrieve the number.
The maximum amount of numbers stored is 31.
The numbers are coded for National Weather Service locations.
PSSCCC the size of the number must take up 6 bytes or I can’t look it up
correctly…

P is part of county
SS is the state number here in the USA
CCC is the county number for that state in the USA.

long getNumber(byte index)
{
long code = 0;
byte value = 0;
byte address = 0;
if (index < 32)
{
address = 6 * (1 + index);
value = eeprom.read(address+0);
code += value * 100000UL; //P
value = eeprom.read(address+1);
code += value * 10000UL; //S
value = eeprom.read(address+2);
code += value * 1000UL; //S
value = eeprom.read(address+3);
code += value * 100UL; //C
value = eeprom.read(address+4);
code += value * 10UL; //C
value = eeprom.read(address+5);
code += value * 1UL; //C
}
return code;
}

Don

PSSCCC the size of the number must take up 6 bytes or I can’t look it up
correctly…

That is not true. The value that getNumber returns could be stored in 4 bytes, by storing the value as a long, not converting the value to a string and then converting each character back to a number, which is what you are, in effect, doing.

If you don’t want to use EEPROM_writeAnything and EEPROM_readAnything, then use a union:

union stuff
{
   byte b[4];
   long a;
}
stuff save;
save.a = 123456;
for(int i=0; i<4; i++)
{
   eeprom.write(addr+i, stuff.b[i]);
}
stuff read;
for(int i=0; i<4; i++)
{
   read.b[i] = eeprom.read(addr+i);
}
long val = read.a;