EEPROM reading values problem[SOLVED]!!

Hello everybody and nice to meet you all!
I'm newbie by using Arduino's microcontrollers so i would like your help.

The problem is:
I want to read 10 values from input, especially units, and i would like to write them into the eeprom.
I think that my data goes to the eeprom fine, but the problem is that when i load them, i don't take the same values...
Could you help me please???????
Thanks in advance!

Here is my loop's funtion code:

void loop()
{
   if( cData <= 10 ){
      // read the analog in value:
      sensorValue = analogRead(analogInPin);                   

      //write these 10 values to eeprom
      EEPROM.write(cData, sensorValue);
      // change the analog out value:
      analogWrite(analogOutPin, sensorValue);           

      // print the results to the serial monitor:
      Serial.print("sensor = " );                       
      Serial.print(sensorValue);       
 
      Serial.print("\t output = ");        
      Serial.println(((sensorValue*5.0)/1024),4); //output result into volts 
                                                  //with 4 digits precision
      // wait 10 milliseconds before the next loop
      // for the analog-to-digital converter to settle
      // after the last reading:
      cData++;
      delay(10);    
    }     
    byte serialData = Serial.read();  
    Serial.println("Press l to load values from eeprom");
    if ((char)serialData =='l')  {
      int addr = 0;
      while (addr <= 10) {            
        // read a byte from the current address of the EEPROM
        byte value = EEPROM.read( addr );
        Serial.print( addr );
        Serial.print("\t");
        Serial.print(value, DEC);      
        Serial.println();
        addr++;
     }     
  }  
}// ...loop()

I can't see where you set a value for cData. You increment it but never reset it. Mind you by only posting a part of your code it is hard to tell.

As you have it in a loop you will rapidly exceed the maximum number of write cycles.
Do it all in the setup() while you are testing to avoid this.

// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:

There is no need to do that.

Sorry my mistake!!!The whole code is:

#include <EEPROM.h>

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9;  // Analog output pin that the LED is attached to
 
 int sensorValue = 0;        // value read from the pot
 int outputValue = 0;        // value output to the PWM (analog out)
 
 int storeData[10];
 int cData = 0;

//----------------------------------------------------------------------------------------
 
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
   
  Serial.print("Units         to        Volts\n");
  Serial.print("-------------------------------\n");
}
//----------------------------------------------------------------------------------------
void loop()
{
   if( cData <= 10 ){
      // read the analog in value:
      sensorValue = analogRead(analogInPin);                   
      
      // change the analog out value:
      analogWrite(analogOutPin, sensorValue);           
      
      //write these 10 values to eeprom
      EEPROM.write(cData, sensorValue);

      // print the results to the serial monitor:
      Serial.print("sensor = " );                       
      Serial.print(sensorValue);       
 
      Serial.print("\t output = ");        
      Serial.println(((sensorValue*5.0)/1024),4); //output result into volts 
                                                  //with 4 digits precision
      // wait 10 milliseconds before the next loop
      // for the analog-to-digital converter to settle
      // after the last reading:
      cData++;
      delay(10);    
    }     
    byte serialData = Serial.read();  
    //Serial.println("Press l to load values from eeprom");
    if ((char)serialData =='l')  {
      int addr = 0;
      while (addr <= 10) {            
        // read a byte from the current address of the EEPROM
        byte value = EEPROM.read( addr  );
        Serial.print( addr );
        Serial.print("\t");
        Serial.print(value, DEC);      
        Serial.println();
        addr++;
     }     
  }  
}// ...loop()
//----------------------------------------------------------------------------------------

Thanks!!

You haven't address my points. Namely that cData never gets reset, and it writes to all the EEPROM memory very quickly and continuous. Do not do this.

Then the read out only reads the first 10 bytes. I think that is why you think it is not working.

Also analog read returns a 16 bit value and you are storing an 8 bit value into EEPROM.

Also you need to learn something about the scope of variables, for example did you know that this line:-

 byte value = EEPROM.read( addr  );

Will create a new variable called value EVERY time it is run in that loop. So in the end you have 10 variables all with the same name. This only gets reset once loop ends.

It would help if you said what you are trying to achieve, the basic reading and writing to EEPROM is fine, it is what is surrounding it that is all mixed up.

 int sensorValue = 0;        // value read from the pot

      sensorValue = analogRead(analogInPin);                   
      
      //write these 10 values to eeprom
      EEPROM.write(cData, sensorValue);

The EEPROM.write() function write ONE byte to the EEPROM. You are passing it an int. Do you really want to guess which half of the int it wrote?

Well!!The description of my problem has to do that i've got multiple analog inputs(how to do that?With one potensionmeter on the input)
Then i'm trying to collect the first 10 values( thats for start, the next step is to do something like that in every 10 minutes) and then i should read these 10 values in eeprom.

After that, i should sent a command from a usb interface application which i've done, to preview this 10 values.

Thanks a lot!!!

It is not clear why you need to use EEPROM at all. Start with that. Why do you need to persist sensor data across resets?

PaulS thank you my friend for you advice!If the project belogs to me, there would be no problem about it!They demand from me to use eeprom...I don't know why!!!All the internet is full with source code and ideas about storage read and writing in other external storage types like,SD,USB etc...

Take it to the other side that it's something like school homework.In this case, you can't say something to your teacher!!!Thats my side now my friend if you could understand me!!!

Thanks again for all!I'll appreciate if you could help a newbie like me!!! :grin: :wink:

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

Thanks a lot!But could you explain me the last line please???I don't understand it .... :grin:

Thanks again!

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

If the value cData is greater or equal to 20 then make the value of cData equal to zero. As you need two addresses for storing one value by keeping the address value under 20 you will restrict the code to storing only 10 values.

This lets you just keep adding 2 to the value of cData, which is your EEPROM address and it never gets above a value of 20.
As I have told you many times in this thread, but you seem to ignore me, you are not resetting the value of where you write to in EEPROM where as you are resetting the value of where you read from. This is because your reading is in a loop that reads all the values but your writing is not.

sirus:
I think that my data goes to the eeprom fine, but the problem is that when i load them, i don't take the same values...

It doesn't go into the EEPROM fine. Let's start with that.

http://arduino.cc/playground/Code/EEPROMWriteAnything