Hello everyone, this morning I was trying to write a calibration to EEPROM so that my project would stay calibrated after I switched it off.
I read some forum posts, most of which pointed to:
http://playground.arduino.cc/Code/EEPROMWriteAnything
I couldn't figure it out. I'm sure it's easy for people better at C++ than me, but I am unfamiliar with the "struct" and after reading into it a little bit I just decided to come up with my own solution.
I was hoping someone could look at what I was doing and show me how to do it more efficiently using the EEPROMWriteAnything library, or any other technique that you think is cleaner.
This is what I came up with first that gave me problems. Check it out, and if you know what is happening let me know.
#include <EEPROM.h>
int x, addr = 0;
float adj,EEPread;
void setup()
{
Serial.begin(9600);
}
void loop()
{
adj=-.12;//this is the adjustment I am tring to save to EEPROM
int val =(adj * 100)+x;
//I multiply it by 100 to make it easier to work with.
//adj will always be less that .99 so I thought I would
//have no problem with this. I add x just to see a change over time
//to see how it works.
Serial.print("val (what I am trying to save to EEPROM: )");
Serial.println(val);
// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(0, val);
EEPread = EEPROM.read(0);
Serial.print("EEPROM position: ");
Serial.print(0);
Serial.print(" contains ");
EEPread = EEPread;
Serial.println(EEPread);
delay(25);
x++;
delay(5000);
}
Here is my solution to the problem.
/*
* Write adjustment to EEPROM to retain calibration after power is switched.
* adj can be negative or positive. Theoretically should never be more than .99
* Was having issues storing a negative float. This is my work around.
*/
#include <EEPROM.h>
int neg, x;
float adj, EEPread;
void setup()
{
Serial.begin(9600);
}
void loop()
{
//You only get to write to EEPROM 100,000 times... this stops it from writing over and over.
//An EEPROM.write saved is a EEPROM.write earned ;)
if (x <= 2)
{
adj = -.12;//this is the adjustment I am trying to save to EEPROM
//If adj is less than zero (negative) EEPwrite 1 to memory address 0 as a flag
//for future processing. Then multiply adj * -1.0 to make a positive number for easy
//storage.
if (adj < 0)
{
EEPROM.write(0, 1);
adj = adj * -1.0;
Serial.println("removed negative sign adj: ");
Serial.println(adj);
}
//Otherwise just EEPwrite a 0 to the 0 address for future processing.
else
{
EEPROM.write(0, 0);
delay(25); //Is it a good idea to put these delays in?
}
//I multiply adj by 100 to make it easier to work with.
//in my situation adj will always be less than .99 .
int val = (adj * 100);
Serial.println("val (what I am trying to save to EEPROM: )");
Serial.println(val);
// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(1, val);
delay(25);
EEPread = EEPROM.read(1);
neg = EEPROM.read(0);
Serial.println("neg:");
Serial.println(neg);
//If neg == 1 then that means the number was originally a negative,
//this if statement will make it a negative again.
if ( neg == 1)
{
EEPread = EEPread * -1.0;
}
Serial.print("EEPROM position: ");
Serial.print(1);
Serial.print(" contains ");
Serial.println(EEPread);
Serial.print("Now multiply it by .01 to return it to the original number:\n");
EEPread = EEPread * .01;
Serial.println(EEPread);
delay(25);
delay(5000);
Serial.print("\n\n\n");
x++;
}
}
Hope my solution helps someone that can't figure out the EEPROMWriteAnything library.