Skip to content

Instantly share code, notes, and snippets.

@k5njm
Created April 12, 2016 19:06
Show Gist options
  • Save k5njm/14029f29c86f60ca451fa1b956f01125 to your computer and use it in GitHub Desktop.
Save k5njm/14029f29c86f60ca451fa1b956f01125 to your computer and use it in GitHub Desktop.
#include <QTRSensors.h>
#include <EEPROMex.h>
/*
EEPROM Storage/Retrieval of QTR calibration data using serial interface
Gathered from notes on this page: https://forum.pololu.com/t/qtr-calibration-values-from-eeprom/6823/3
Using this EEPROM Library: http://thijs.elenbaas.net/2012/07/extended-eeprom-library-for-arduino/
*/
#define NUM_SENSORS 8 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 microseconds for sensor outputs to go low
#define EMITTER_PIN 10 // emitter is controlled by digital pin 2
// sensors 0 through 7 are connected to digital pins 3 through 10, respectively
QTRSensorsRC qtrrc((unsigned char[]) {2, 3, 4, 5, 6, 7, 8, 9}, NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
//EEPROM Addressing for calibration storage
#define addrCalibratedMinimumOn 0
#define addrCalibratedMaximumOn 100
void setup()
{
Serial.begin(9600);
Serial.println("QTR Sensor Calibration. (C)alibrate, (R)ead, (S)tore to EEPROM, R(E)call from EEPROM");
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch == 'c' || ch == 'C')
{
calibrateQTR();
}
else if (ch == 'r' || ch == 'R')
{
readQTR();
}
else if (ch == 's' || ch == 'S')
{
storeQTR();
}
else if (ch == 'e' || ch == 'E')
{
recallQTR();
}
}
}
void calibrateQTR()
{
Serial.println();
Serial.println("Beginning Calibration Process...");
for (int i = 0; i < 200; i++) // make the calibration take about 10 seconds
{
qtrrc.calibrate(); // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
}
Serial.println("Calibration Complete");
}
void readQTR()
{
Serial.println();
Serial.println("Reading Calibration Data...");
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();
// print the calibration maximum values measured when emitters were on
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMaximumOn[i]);
Serial.print(' ');
}
}
void storeQTR()
{
Serial.println();
Serial.println("Storing Calibration Data into EEPROM...");
EEPROM.writeBlock<unsigned int>(addrCalibratedMinimumOn, qtrrc.calibratedMinimumOn, 8);
EEPROM.writeBlock<unsigned int>(addrCalibratedMaximumOn, qtrrc.calibratedMaximumOn, 8);
Serial.println("EEPROM Storage Complete");
}
void recallQTR()
{
Serial.println();
Serial.println("Recalling Calibration Data from EEPROM...");
qtrrc.calibrate();
EEPROM.readBlock<unsigned int>(addrCalibratedMinimumOn, qtrrc.calibratedMinimumOn, 8);
EEPROM.readBlock<unsigned int>(addrCalibratedMaximumOn, qtrrc.calibratedMaximumOn, 8);
Serial.println("EEPROM Recall Complete");
}
@DarkAnton7032
Copy link

Thanks for the code, but what means the '8' by:
EEPROM.writeBlock(addrCalibratedMinimumOn, qtrrc.calibratedMinimumOn, 8);
Thank you in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment