I am trying to save the calibration data from the 6050 accelerometer in EEPROM, so that I don't have to re calibrate it at every startup.
I am using the EEPROMAnything library, because my values are larger than 254. This is what I have so far
#include <Wire.h>
#include "gyro_accel.h"
#include <EEPROM.h>
#include "EEPROMAnything.h"
void setup(){
Serial.begin(115200);
cal = EEPROM.read(0); // checks if in EEPROM array 0, there is a calibration value
if (cal == 0xff) {
MPU6050_OffsetCal(); // This will calibrate the gyro and accelerometer, and save the data in EEPROM in address 0-5)
EEPROM_writeAnything(0, gyro_x_OC);
EEPROM_writeAnything(1, gyro_y_OC);
EEPROM_writeAnything(2, gyro_z_OC);
EEPROM_writeAnything(3, accel_x_OC);
EEPROM_writeAnything(4, accel_x_OC);
EEPROM_writeAnything(5, accel_x_OC);
} else {
byte gyro_x_OC = EEPROM_readAnything(0);
byte gyro_y_OC = EEPROM_readAnything(1);
byte gyro_z_OC = EEPROM_readAnything(2);
byte accel_x_OC = EEPROM_readAnything(3);
byte accel_y_OC = EEPROM_readAnything(4);
byte accel_z_OC = EEPROM_readAnything(5);
The problem comes when I want to read the values, it gives me the error:" no matching function for call to 'EEPROM_readAnything(int)". The problem seems to be that EEPROM_readAnything takes 2 values:
The function is as follows: EEPROM_readAnything(int ee, T& value)
I dont understand what is the second value "T& value", wouldn't it make sense when you read a value, that you just specify which position would you like to read, like in my example 0-5?
The second parameter is a variable of the same size as the one you want to read. The function doesn't know if you're reading an int or a float or a long or a byte or what, so you give it a variable of the right size to go by.
By the way, if you're storing values longer than 8 bits (ie larger than 255) then you should have those EEPROM addresses more than 1 apart. For ints you should have them spaced by 2's and for longs by 4's.
Delta_G:
By the way, if you're storing values longer than 8 bits (ie larger than 255) then you should have those EEPROM addresses more than 1 apart. For ints you should have them spaced by 2's and for longs by 4's.
Well, except that I see you are putting the return values into bytes, so you're only reading 8bits at a time regardless of how big you say your numbers may be.
If you're only doing bytes, ditch the EEPROMAnything stuff and just use regular old EEPROM read and write.
if (EEPROM_readAnything(0,gyro_x_OC) != 0xff) {
MPU6050_OffsetCal(); // This will calibrate the gyro and accelerometer, and save the data in EEPROM in address 0-5)
EEPROM_writeAnything(0, gyro_x_OC);
EEPROM_writeAnything(2, gyro_y_OC);
EEPROM_writeAnything(4, gyro_z_OC);
EEPROM_writeAnything(6, accel_x_OC);
EEPROM_writeAnything(8, accel_x_OC);
EEPROM_writeAnything(10, accel_x_OC);
} else {
gyro_x_OC = EEPROM_readAnything(0, gyro_x_OC);
gyro_y_OC = EEPROM_readAnything(2, gyro_y_OC);
gyro_z_OC = EEPROM_readAnything(4, gyro_z_OC);
accel_x_OC = EEPROM_readAnything(6, accel_x_OC);
accel_y_OC = EEPROM_readAnything(8, accel_x_OC);
accel_z_OC = EEPROM_readAnything(10, accel_x_OC);
}
Now it doesn't give me an error when I write for example EEPROM_read(Anything(0, gyro_x_OC);
So basically I want the program to check if there is a calibration stored from before:
if (EEPROM_readAnything(0,gyro_x_OC) != 0xff)
"Is that right? First I thought I had to write is its == 0xff, rather than !=0xff, because I want to see if its empty. If I put ==0xff it doesn't perform the calibration because it looks like there is something already stored in the EEPROM even after I upload the sketch from scratch, I don't get that"
So basically the problem I am having now is that it does a fresh calibration every time I open the Serial monitor, rather than doing the calibration once and then use the old values.
Not like that. The function puts the value it reads into the variable in the second parameter, but it returns the number of bytes read. So the return value (hopefully) won't ever be 0xFF. It will be 1 or 2 or 4 or however big gyro_x_OC is. You'd have to do that in two lines. Read the value in and then compare it.
EEPROM_readAnything(0, gyro_x_OC);
if(gyro_x_OC != 0xFF){
// the EEPROM has been written to in that spot
}
Just as a side note, I'm not entirely clear how you're using this, but if you're setting 0xFF as a flag when something is done like calibration that's the worst choice of a flag. 0xFF is an EEPROM's default value when it hasn't been written to or when it's been cleared. Virtually any other number would be fine as a flag.
As Delta_G says, read the value, then compare. The return is likely to be 2 in every case, if that is the size of gyro_x_OC.
If you are testing for the default uninitialized EEPROM (which is FF FF FF FF etc.) then you need to check for the size of the variable you are reading.
eg.
if (gyro_x_OC != 0xFFFF)
...
Even then, that may not work if gyro_x_OC is signed. Maybe better:
if (EEPROM.read(0) == 0xFF && EEPROM.read(1) == 0xFF)
{
// set up defaults
}
else
{
// read from EEPROM
}
Even that assumes that the EEPROM is totally uninitialized and not used (say) for some other program. Maybe better again is to have some "magic number" in the first couple of bytes.
eg,
const unsigned int magicNumber = 8632;
unsigned int magicNumberValue;
EEPROM_readAnything(0, magicNumberValue);
if (magicNumberValue == magicNumber)
{
// we have valid values, read from EEPROM
}
else
{
// set up defaults, write to EEPROM
// write magic number for next time
magicNumberValue = magicNumber;
EEPROM_writeAnything(0, magicNumberValue);
}