I am working on saving QTRA calibration values to EEPROM so calibration doesn’t have to happen on every power up, and more importantly so the function can continue if there is a crash/restart.
When calibration is run, the values get saved to EEPROM-
void set_sensor() //Calibrate the reflectance sensors Add LED indication that calibration is happening
{
delay(500);
for (int i = 0; i < 200; i++) // make the calibration take about 5 seconds
{
qtra.calibrate(); // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
}
// print the calibration minimum values measured when emitters were on
Serial.begin(9600);
Serial.print("Minimum Values: ");
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMinimumOn[i]);
EEPROM.writeInt(i*2,qtra.calibratedMinimumOn[i]); //Write the minimum values into EEPROM. Each takes 2 slots, 0-1,2-3,4-5,6-7
Serial.print(' ');
}
Serial.println();
Serial.print("Maximum Values: ");
// print the calibration maximum values measured when emitters were on
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtra.calibratedMaximumOn[i]);
EEPROM.writeInt((i*2)+10,qtra.calibratedMaximumOn[i]); //Write the minimum values into EEPROM. Each takes 2 slots, 10-11,12-13,14-15,16-17
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(1000);
}
During setup I have a call to a function that will read the EEPROM into a pair of int arrays-
void restore_sensor(){
for (int i = 0; i < NUM_SENSORS; i++)
{minimum_from_EEPROM[i] = EEPROM.readInt(i*2);
}
for (int i = 0; i < NUM_SENSORS; i++)
{maximum_from_EEPROM[i] = EEPROM.readInt((i*2)+10);
}
This all works so far, and I can bring those arrays over to the QTRSensors.cpp tab with
extern int minimum_from_EEPROM[4];
extern int maximum_from_EEPROM[4];
From here I don’t know how to get the values in the array transferred into calibratedMinimumOn[i] and calibratedMaximumOn[i] in such a way that they will be used by the readCalibrated function. It needs to start out with the EEPROM values, but change to what ever values get determined by a subsequent calibration.
Since I don’t use the calibration reset, I tried changing it so that instead of resetting them to 0, it would reset the calibration to the EEPROM values.
// Resets the calibration.
void QTRSensors::resetCalibration( ) //This has been changed to reset the calibration to the values that were stored in EEPROM
{
unsigned char i;
for(i=0;i<_numSensors;i++)
{
calibratedMinimumOn[i] = minimum_from_EEPROM[i];
calibratedMaximumOn[i] = maximum_from_EEPROM[i];
}
}
Then I called it in Setup()
restore_sensor(); //Read sensor calibration values out of EEPROM into minimum_from_EEPROM and maximum_from_EEPROM arrays
qtra.resetCalibration(); //Run reset function from QTRA library, but instead of setting the value to 0, set to the values in the above arrays.
This didn’t work, and in the Serial Monitor I had what looked like an error about an overflow. What can I do to get the values from my minimum_from_EEPROM[4]; and maximum_from_EEPROM[4]; arrays into the library for use with QTRA?