Questions about EEPROM

split of from - Current Amplifiers - #69 by MrDropsy - General Electronics - Arduino Forum -

Yea thanks polymorph, i ended up realising that

My next question is about EEPROM, i essentially have a value which can be calibrated and i want it to be stored even when i power down.

My understanding is that i need to write it as a 8 bit value between 0 and 255. So i need to somehow make a conversion from a decimal value.

and then read the value.

Im trying to read about it, but im not really sure i understand how to use it and where i need to use it.

I would probably want it to be saved after i exit my calibration menu.

heres my calibration menu code

void caseCAL()
{
  lcd.clear();
  lcd.print("Calibration");
  delay(3000);
  
  while(calFlag!=0)
  {
  lcd.clear();
  lcd.print("-");
  lcd.setCursor(6,0);
  lcd.print(factor);
  lcd.setCursor(15,0);
  lcd.print("+");
  lcd.setCursor(3,1);
  float photoDiode_1 = analogRead(A1);
  float photoDiode_2 = analogRead(A1);
  float photoDiode_3 = analogRead(A1);
  float averagetoprint = ((photoDiode_1)+(photoDiode_2)+(photoDiode_3))/3;
  float voltage_1 = photoDiode_1 * ((5.0 / 1023.0)*1000);
  float voltage_2 = photoDiode_2 * ((5.0 / 1023.0)*1000);
  float voltage_3 = photoDiode_3 * ((5.0 / 1023.0)*1000);
  float averageVoltage = ((voltage_1)+(voltage_2)+(voltage_3))/3;
  float lux = factor * averageVoltage;
  lcd.print(lux);
  lcd.print("  Lux");
  delay(1000);
  lcd_key=read_LCD_buttons();
  if(adc_key_in<50 && adc_key_in >=0) factor++;
  if(adc_key_in<650 && adc_key_in >450) factor--;
  if(adc_key_in<850 && adc_key_in>650)casebtnSELECT();
  if(adc_key_in<850 && adc_key_in>650) calFlag=0;
  
  }
  calFlag=1;
  //EEPROM.write(0,factor);
}

everything else ive basically posted before, but now i more flow control.

Ive been trying to use this

http://thijs.elenbaas.net/2012/07/extended-eeprom-library-for-arduino/

as the library can deal with floats and is what i need but I cant seem to get my head around it and figure out where i need read and write, etc.

There seem to be three threads on this subject. Any objection to my merging them? It is kindof confusing to read references to "the other thread" all over the place.

@MrDropsy: Confusion which you have caused.

Sorry guys for "cross posting" my time zone is bit different, so it actually makes it a bit hard to get a hold of anyone. Wont happen again

That's not an excuse. If you want instant results, pay someone. Even then you have to wait for them to come into work. My time zone is different too, I wouldn't wonder.

My next question is about EEPROM, i essentially have a value which can be calibrated and i want it to be stored even when i power down.

My understanding is that i need to write it as a 8 bit value between 0 and 255. So i need to somehow make a conversion from a decimal value.

This is a programming question, right?

My next question is about EEPROM, i essentially have a value which can be calibrated and i want it to be stored even when i power down.

Look, try to use the forum in a reasonable way. Segueing from current amplifiers to reading/writing EEPROM is just ridiculous.

@ Nick Gammon

Well I have been accused of cross posting before so I tried to keep the same project in the same thread. Seems pretty reasonable to me.

I ended up asking the question in programming section anyway.

Good day Sir

I also just realised that merging happened so that's my bad.

Again apologies.

@MrDropsy,
I have split of the EEPROM question to the the programming section as that seems more appropriate. You will get more reactions if a post is in the right section.

The rules for posting look sometimes ambiguous and moderators are only human too, so please forgive us :wink:

To answer the question, all numbers are in binary, no numbers are in decimal inside the processor so there is no need to convert anything into anything.

And I found much more convenient to just decide the maximum precision and convert your value to the integer representing that precision.

As ane xample, if you want to store a temperature ranging between 120 and -50 Celsius with two decimal digits then you can use a centi Celsius unit (1/100th of Celsius) and store a number ranging between 12000 (120 *100) and -5000 (-50 * 100): this way you reason with integer numbers all the way and avoid weird conversion issues you might encountering storing floating numbers binary representations.

This obviously doesn't apply to each and every situation, but only to those situations when you can pre determine the maximum precision (which happens most of the time, at least in my world of applications).

Please note this solves the issue about transmitting such information to other systems as well, where different binary representations of floating points can be in use.

...this solves the issue about transmitting such information to other systems as well...

Maybe, maybe not. Do all systems store an int as high byte then low byte or do some reverse the order (the endian problem)? A more portable solution is to use a union:

union myUnion {
   byte myBytes[sizeof(int)];
   int myInt;
} myInteger;

You can then store the integer in myInteger.myInt, and then write it to EEPROM using myBytes[]. You can then use the sizeof() operator in a for loop to read the integer back out of EEPROM into the array, and then extract it from myInt. This allows you to abstract from the endian problem.

Thanks guys, but I have actually already solved my problem, i downloaded a library which lets me do floats.

The library can be found here

Essentially I have an original value. But then i also made a float variable

Now when I was in calibration mode and went to get out of the menu

I would do:

EEPROM.writeFloat(0,Variable)

then I would read it in my setup

Variable=readFloat(0);

This gave me the desired outcome - I then simply used Variable in my calculations.

If i ever wanted original, i set a default button which made

Variable = Original

I know its not the best way to do it, but i doubt ill ever really need to calibrate it that much