When writing to EEPROM, remember that even uploading a new sketch will not clear the
EEPROM, so you may have leftover values in there from a previous project. Sketch 8-03
resets all the contents of EEPROM to zeros:
Also be aware that you can write to an EEPROM location only about 100,000 times
before it will become unreliable. So only write a value back to EEPROM when you really
need to. EEPROM is also quite slow, taking about 3 milliseconds to write a byte.
Compression
When saving data to EEPROM or when using
PROGMEM
, you will sometimes find that
you have more to save than you have room to save it. When this happens,
it is worth
finding the most efficient way of representing the data.
Range Compression
You may have a value for which on the face of it you need an
int
or a
float
that are both
16-bit. For example, to represent a temperature in degrees Celsius, you might use a
float
value such as 20.25. When you
are storing that into EEPROM,
life would be so much
easier if you could
fit it into a single byte, and you could store twice as much as if you
used a
float
.
One way that you can do this is to change the data before you store it. Remember that a
byte will allow you to store a positive number between 0 and 255. So if you only cared
about the temperature
to the nearest degree Celsius, then
you could simply convert the
float
to an
int
and discard the part after the decimal point. The following example shows
how to do this:
The variable
tempFloat
contains the floating point value. The
(int)
command is called
a
type cast
and is used to convert a variable from one type to another compatible type. In
this case, the type cast converts the
Dostları ilə paylaş: