EEPROM_writeAnything problem

Hi,

I'have made a test file but when i run it it gives 2 lines on the LCD display

1024 (correct)
0 (not correct should be 1024)

I don't get the EEPROM_reedAnything working. Can anyone look at my code and please help me solve it.
I use Arduino 0019.

#include <EEPROM.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;
}


template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ = EEPROM.read(ee++);
    return i;
}

long lengte;

struct config_t
{
    long lengte;
} cfg;

void setup(){

  lcd.begin(16, 2);
  lengte = 1024;
  EEPROM_writeAnything(0, cfg);
  delay(20);
    
  EEPROM_readAnything(0, cfg);
  delay(20);
  lcd.clear();
  lcd.print(lengte);
  lcd.setCursor(0,1);
  lcd.print(cfg.lengte);
  delay(500);
 
}

void loop(){

}

Did you mean

cfg.lengte = 1024;
  EEPROM_writeAnything(0, cfg);

or even

lengte = 1024;
  EEPROM_writeAnything(0, lengte);

?

Groove,

Thanks,

This one did the yob.

cfg.lengte = 1024;
EEPROM_writeAnything(0, cfg);

;D ;D ;D ;D ;D ;D

So i had to put cfg. before lengte when using it in the EEPROM_writeAnything.

In which case, your test proves nothing.
I think you need the second example.

Awol,

Tested the second one too and it works.

So, both codes are working but why is the second one the better one?

Think about the first case, and imagine that EEPROM_writeAnything works, but EEPROM_readAnything does not do anything

Okay, made 2 codes with both options mentioned above.
At the end there are 2 if statements, there is the diverence.

#include <EEPROM.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int analogInPin0 = 0;
const int analogInPin5 = 5;

int sensorValue0 = 0;    // value read from 5 pushputtons on analog 0
int sensorValue5 = 0;    // value read from the pot on analog 5
int outputValue = 0;     // 

long lengte;
long breedte;
long hoogte;

//---------------------
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
    EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
    *p++ = EEPROM.read(ee++);
    return i;
}
struct config_t
{
long lengte;
long breedte;
long hoogte;
} cfg;
//---------------------

void setup(){
 
  lcd.begin(16, 2);
}

void loop(){
  
 
  sensorValue0 = analogRead(analogInPin0);
  sensorValue5 = analogRead(analogInPin5);
  outputValue = map(sensorValue0, 0, 1023, 0, 255);
  
  
  lcd.setCursor(0,0);
  lcd.print("Sensor=       cm");
  lcd.setCursor(8,0);
  lcd.print(sensorValue5);
  
  EEPROM_readAnything(0, cfg);
  lcd.setCursor(0,1);
  lcd.print("L=   cm B=   cm");
  lcd.setCursor(2,1);
  lcd.print(cfg.lengte);
  lcd.setCursor(9,1);
  lcd.print(cfg.breedte);
 
  if (outputValue == 184){
     cfg.lengte = sensorValue5;
     EEPROM_writeAnything(0, cfg);
     delay (100);
     }
  if (outputValue == 0){
     cfg.breedte = sensorValue5;
     EEPROM_writeAnything(0, cfg);
     delay (100);
     }
  delay(20);
}
#include <EEPROM.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int analogInPin0 = 0;
const int analogInPin5 = 5;

int sensorValue0 = 0;    // value read from 5 pushputtons on analog 0
int sensorValue5 = 0;    // value read from the pot on analog 5
int outputValue = 0;     // 

long lengte;
long breedte;
long hoogte;

//---------------------
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
    EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
    *p++ = EEPROM.read(ee++);
    return i;
}
struct config_t
{
long lengte;
long breedte;
long hoogte;
} cfg;
//---------------------

void setup(){
 
  lcd.begin(16, 2);
}

void loop(){
  
 
  sensorValue0 = analogRead(analogInPin0);
  sensorValue5 = analogRead(analogInPin5);
  outputValue = map(sensorValue0, 0, 1023, 0, 255);
  
  
  lcd.setCursor(0,0);
  lcd.print("Sensor=       cm");
  lcd.setCursor(8,0);
  lcd.print(sensorValue5);
  
  EEPROM_readAnything(0, cfg);
  lcd.setCursor(0,1);
  lcd.print("L=   cm B=   cm");
  lcd.setCursor(2,1);
  lcd.print(cfg.lengte);
  lcd.setCursor(9,1);
  lcd.print(cfg.breedte);
 
  if (outputValue == 184){
     lengte = sensorValue5;
     EEPROM_writeAnything(0, lengte);
     delay (100);
     }
  if (outputValue == 0){
     breedte = sensorValue5;
     EEPROM_writeAnything(0, breedte);
     delay (100);
     }
  delay(20);
}

Both codes are working.

But i can't figure out why "EEPROM_readAnything does not do anything" in the first code.

I said "imagine" it does nothing - I didn't say that it didn't work.
I was trying to make the point that when you're testing something, use a method that makes the result as unambiguous as possible.

If you write a value out from a variable, reading back into the same variable may not prove anything, if the "read" isn't doing anything at all - the initial write would be non-destructive, so the variable would still contain the value you were expecting to see.
Either zero or invert the value before reading back, or read into a different variable.