I am using this library: GitHub - jlesech/Eeprom24C01_02: Arduino library for 24C01/02 serial EEPROM
It allows the user to write & read a single byte or an array.
In my project I need to use a struct to store all my data and searching on the forum I found a topic in which a user points here: Arduino Playground - EEPROMWriteAnything
So I created an example code in which I try to use this 2 functions with the previous library but I did something wrong or I need to change something else because it is failing. Values I read from eeprom are not the ones I wrote before.
Tjis is my test sketch:
/**************************************************************************//**
* \brief EEPROM 24C01 / 24C02 library for Arduino - Demonstration program
* \author Copyright (C) 2012 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20120217
*
* This file is part of the EEPROM 24C01 / 24C02 library for Arduino.
*
* This library is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
******************************************************************************/
/**************************************************************************//**
* \file WriteReadByte.ino
******************************************************************************/
/******************************************************************************
* Header file inclusions.
******************************************************************************/
#include <Wire.h>
#include <Eeprom24C01_02.h>
/******************************************************************************
* Private macro definitions.
******************************************************************************/
/**************************************************************************//**
* \def EEPROM_ADDRESS
* \brief Address of EEPROM memory on TWI bus.
******************************************************************************/
#define EEPROM_ADDRESS 0x50
/******************************************************************************
* Private variable definitions.
******************************************************************************/
static Eeprom24C01_02 eeprom(EEPROM_ADDRESS);
/******************************************************************************
* Public function definitions.
******************************************************************************/
/**************************************************************************//**
* \fn void setup()
*
* \brief
******************************************************************************/
typedef struct
{
int a;
int b;
int c[2];
} test;
test t;
test tt;
void setup()
{
t.a = 1;
t.b = 2;
t.c[0] = 3;
t.c[1] = 4;
Serial.println("ORIG bytes:");
Serial.println(tt.a);
Serial.println(tt.b);
Serial.println(tt.c[0]);
Serial.println(tt.c[1]);
// Initialize serial communication.
Serial.begin(9600);
// Initialize EEPROM library.
eeprom.initialize();
const byte address = 0;
// Write a byte at address 0 in EEPROM memory.
Serial.println("Write byte to EEPROM memory...");
EEPROM_writeAnything(0, t);
// Write cycle time (tWR). See EEPROM memory datasheet for more details.
delay(10);
// Read a byte at address 0 in EEPROM memory.
Serial.println("Read byte from EEPROM memory...");
EEPROM_readAnything(0, tt);
Serial.println("Read bytes:");
Serial.println(tt.a);
Serial.println(tt.b);
Serial.println(tt.c[0]);
Serial.println(tt.c[1]);
}
/**************************************************************************//**
* \fn void loop()
*
* \brief
******************************************************************************/
void loop()
{
}
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
eeprom.writeByte(ee++, *p++);
return i;
}
template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
*p++ = eeprom.readByte(ee++);
return i;
}
And this is my output:
Write byte to EEPROM memory...
Read byte from EEPROM memory...
Read bytes:
4865
-1
-1
-1
Any idea about what is happening? It could be related with the size of data I want to write? ints taking 2 bytes....