i'm trying test program write , read 3 values.
it compiles ok.
i'm getting strange numbers back.
instead of 1500, 1600 , 1700, seeing 16604, -23488 , 1700
what missing??
here's code:
here code eepromanything.h
moderator: added code tags
it compiles ok.
i'm getting strange numbers back.
instead of 1500, 1600 , 1700, seeing 16604, -23488 , 1700
what missing??
here's code:
code: [select]
#include <eeprom.h>
#include <wprogram.h>
#include "eepromanything.h"
int = 1500;
int b = 1600;
int c = 1700;
int val;
void setup()
{
serial.begin(9600);
serial.println("start");
// write 0 3 addresses of eeprom
eeprom_writeanything(0, 0);
eeprom_readanything(0, val);
serial.println(val);
eeprom_writeanything(1, 0);
eeprom_readanything(1, val);
serial.println(val);
eeprom_writeanything(2, 0);
eeprom_readanything(2, val);
serial.println(val);
// write values 3 addresses
eeprom_writeanything(0, a);
eeprom_writeanything(1, b);
eeprom_writeanything(2, c);
eeprom_readanything(0, val);
serial.println(val); // getting 16604
eeprom_readanything(1, val);
serial.println(val); // getting -23488
eeprom_readanything(2, val);
serial.println(val); // getting 1700
serial.println("done");
}
void loop()
{
}
here code eepromanything.h
code: [select]
#include <eeprom.h>
#include <wprogram.h> // type definitions
template <class t> int eeprom_writeanything(int ee, const t& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
(i = 0; < sizeof(value); i++)
eeprom.write(ee++, *p++);
return i;
}
template <class t> int eeprom_readanything(int ee, t& value)
{
byte* p = (byte*)(void*)&value;
unsigned int i;
(i = 0; < sizeof(value); i++)
*p++ = eeprom.read(ee++);
return i;
}
moderator: added code tags
an int has 2 bytes , using addresses 0 1 , 2
so values overlap ?
try using address 0 2 4
so values overlap ?
try using address 0 2 4
Arduino Forum > Using Arduino > Storage > EEPROM_writeAnything not working quite right
arduino
Comments
Post a Comment