Go Down

Topic: EEPROMEx library not behaiving as expected (Read 553 times) previous topic - next topic

gharryh

I am trying to implement the EEPROMEx libray and run into a problem.
(http://playground.arduino.cc/Code/EEPROMLoadAndSaveSettings)
In my setup() if want to see if there is a software change:
Code: [Select]

#include <Streaming.h>
#include <SoftwareSerial.h>
#define SOFTWARE_VERSION "0.1"
const int maxAllowedWrites = 200;
const int memBase = 0;
const int memCeiling = EEPROMSizeATmega328;
boolean OK = true;
int configAdress = 0;

void setup() {
  configAdress  = EEPROM.getAddress(sizeof(StoreStruct)); // Size of config object
  OK = loadConfig();    // Try to load config
  if(OK) {
           Serial << "Config load" << endl;
           }  // If version are same resume
  else   {
          saveConfig();
          Serial << "Config save" << endl;
          }  // else save defaults and use them

}

And the two routines from that example adjusted to my enviroment:
Code: [Select]

bool loadConfig() {
  EEPROM.readBlock(configAdress, confValue);
// for debug only
  Serial << SOFTWARE_VERSION << endl;
  Serial << confValue.softwareVersion << endl;
//
  return (confValue.softwareVersion == SOFTWARE_VERSION);
}

void saveConfig() {
   EEPROM.writeBlock(configAdress, confValue);
}


Althoug both version are the same, saveConfig() is called always.

PaulS

Quote
Althoug both version are the same, saveConfig() is called always.

Correctly, too, I'm almost certain.

Where/how is your structure defined?

Quote
return (confValue.softwareVersion == SOFTWARE_VERSION);

If softwareVersion in the struct is an array or a pointer, that array address or pointer will never be equal to "0.1".

You could use strcmp() to see if the two strings are the same. The addresses certainly aren't.

Cybernetician

#2
Mar 13, 2013, 12:00 pm Last Edit: Mar 13, 2013, 12:04 pm by Cybernetician Reason: 1
Code: [Select]
bool loadConfig() {
 EEPROM.readBlock(configAdress, confValue);
// for debug only
 Serial << SOFTWARE_VERSION << endl;
 Serial << confValue.softwareVersion << endl;
//
 return (confValue.softwareVersion == SOFTWARE_VERSION);
}


Yep, because of this

Quote
return (confValue.softwareVersion == SOFTWARE_VERSION);


put this line against the above

Code: [Select]
return strcmp(confValue.softwareVersion, "0.1")

will solve the issue.

Edit: during typing PaulS already mention that
From Idea To Invention

gharryh

Ok will try that later. If correct the example is wrong.

PaulS

Quote
If correct the example is wrong.

No. The example is comparing one character at time, not the whole string at once like you are trying to.

gharryh

Now i am puzzled. The example shows :
Code: [Select]
void setup() {
OK = loadConfig;
}

void loop() { }

bool loadConfig() {
  EEPROM.readBlock(configAdress, confValue);
  return (confValue.softwareVersion == SOFTWARE_VERSION);
}

So  the result of loadConfig() could be false or true but if i understand you correct this isnt so.

PaulS

Quote
The example shows

If the example really shows assigning the address of a function to OK, it is wrong in more that one way.

Ignore it. You know the right way to do what you want.

Go Up