EEPROM reading values problem[SOLVED]!!

When you do:-

EEPROM.write(cData, sensorValue);

You only write one byte of data.
When you do:-

sensorValue = analogRead(analogInPin);

You create a variable that takes up 2 bytes of data. In order to write that value you need to split it up into two parts

EEPROM.write(cData, sensorValue & 0xff);
EEPROM.write(cData+1, sensorValue >> 8);

When you increment cData you need to do it by two because each value you put into EEPROM takes up two bytes.

// not cData++; but
cData+=2;

So you also need to do this when reading:-

value = EEPROM.read( addr  ) + (EEPROM.read( addr+1 ) << 8);

and again increment by 2

// not cData++; but
cData+=2;

At the start of your loop() put

if(cData >= 20) cData = 0; // keeps the value under 10 values

1 Like