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.