Go Down

Topic: EEPROM reading values problem[SOLVED]!! (Read 1 time) previous topic - next topic

sirus

May 08, 2012, 11:43 pm Last Edit: May 31, 2012, 11:15 am by sirus Reason: 1
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:

Code: [Select]

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()

Grumpy_Mike

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.

Quote
// 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.

sirus

#2
May 08, 2012, 11:55 pm Last Edit: May 09, 2012, 01:35 am by sirus Reason: 1
Sorry my mistake!!!The whole code is:
Code: [Select]

#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!!

Grumpy_Mike

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:-
Code: [Select]
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.

PaulS

Code: [Select]
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?

Go Up