Pages: [1]   Go Down
Author Topic: EEPROM reading values problem[SOLVED]!!  (Read 1499 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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()
« Last Edit: May 31, 2012, 04:15:28 am by sirus » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34222
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry my mistake!!!The whole code is:
Code:
#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!!
« Last Edit: May 08, 2012, 06:35:54 pm by sirus » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34222
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!!  smiley-mr-green smiley-wink
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34222
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When you do:-
Code:
EEPROM.write(cData, sensorValue);
You only write one byte of data.
When you do:-
Code:
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
Code:
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.
Code:
// not cData++; but
cData+=2;

So you also need to do this when reading:-
Code:
value = EEPROM.read( addr  ) + (EEPROM.read( addr+1 ) << 8);
and again increment by 2
Code:
// not cData++; but
cData+=2;

At the start of your loop() put
Quote
if(cData >= 20) cData = 0; // keeps the value under 10 values


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot!But could you explain me the last line please???I don't understand it .... smiley-mr-green

Thanks again!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34222
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18809
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18809
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: