12 _type = QTRType::Analog;
23 uint8_t *oldSensorPins = _sensorPins;
24 _sensorPins = (uint8_t *)realloc(_sensorPins,
sizeof(uint8_t) * sensorCount);
25 if (_sensorPins ==
nullptr) {
31 for (uint8_t i = 0; i < sensorCount; i++) {
32 _sensorPins[i] = pins[i];
35 _sensorCount = sensorCount;
45 if (timeout > 32767) {
49 if (_type == QTRType::RC) {
59 _samplesPerSensor = samples;
66 _oddEmitterPin = emitterPin;
67 pinMode(_oddEmitterPin, OUTPUT);
76 _oddEmitterPin = oddEmitterPin;
77 _evenEmitterPin = evenEmitterPin;
78 pinMode(_oddEmitterPin, OUTPUT);
79 pinMode(_evenEmitterPin, OUTPUT);
87 pinMode(_oddEmitterPin, INPUT);
92 pinMode(_evenEmitterPin, INPUT);
101 if (dimmingLevel > 31) {
104 _dimmingLevel = dimmingLevel;
110 bool pinChanged =
false;
116 if (emitters == QTREmitters::All ||
117 (_emitterPinCount == 2 && emitters == QTREmitters::Odd)) {
120 (digitalRead(_oddEmitterPin) == HIGH)) {
121 digitalWrite(_oddEmitterPin, LOW);
129 if (_emitterPinCount == 2 &&
130 (emitters == QTREmitters::All || emitters == QTREmitters::Even)) {
133 (digitalRead(_evenEmitterPin) == HIGH)) {
134 digitalWrite(_evenEmitterPin, LOW);
139 if (wait && pinChanged) {
142 delayMicroseconds(1200);
144 delayMicroseconds(200);
151 bool pinChanged =
false;
152 uint16_t emittersOnStart;
158 if (emitters == QTREmitters::All ||
159 (_emitterPinCount == 2 && emitters == QTREmitters::Odd)) {
165 (_dimmable || (digitalRead(_oddEmitterPin) == LOW))) {
166 emittersOnStart = emittersOnWithPin(_oddEmitterPin);
174 if (_emitterPinCount == 2 &&
175 (emitters == QTREmitters::All || emitters == QTREmitters::Even)) {
181 (_dimmable || (digitalRead(_evenEmitterPin) == LOW))) {
182 emittersOnStart = emittersOnWithPin(_evenEmitterPin);
187 if (wait && pinChanged) {
192 while ((uint16_t)(micros() - emittersOnStart) < 300) {
193 delayMicroseconds(10);
196 delayMicroseconds(200);
203uint16_t QTRSensors::emittersOnWithPin(uint8_t pin)
205 if (_dimmable && (digitalRead(pin) == HIGH)) {
210 digitalWrite(pin, LOW);
211 delayMicroseconds(1200);
214 digitalWrite(pin, HIGH);
215 uint16_t emittersOnStart = micros();
217 if (_dimmable && (_dimmingLevel > 0)) {
220 for (uint8_t i = 0; i < _dimmingLevel; i++) {
221 delayMicroseconds(1);
222 digitalWrite(pin, LOW);
223 delayMicroseconds(1);
224 digitalWrite(pin, HIGH);
230 return emittersOnStart;
238 case QTREmitters::Odd:
239 offEmitters = QTREmitters::Even;
242 case QTREmitters::Even:
243 offEmitters = QTREmitters::Odd;
246 case QTREmitters::All:
250 case QTREmitters::None:
260 uint16_t turnOffStart = micros();
270 while ((uint16_t)(micros() - turnOffStart) < 1200) {
271 delayMicroseconds(10);
278 for (uint8_t i = 0; i < _sensorCount; i++) {
316void QTRSensors::calibrateOnOrOff(CalibrationData &calibration,
QTRReadMode mode)
323 if (!calibration.initialized) {
324 uint16_t *oldMaximum = calibration.maximum;
325 calibration.maximum = (uint16_t *)realloc(calibration.maximum,
326 sizeof(uint16_t) * _sensorCount);
327 if (calibration.maximum ==
nullptr) {
333 uint16_t *oldMinimum = calibration.minimum;
334 calibration.minimum = (uint16_t *)realloc(calibration.minimum,
335 sizeof(uint16_t) * _sensorCount);
336 if (calibration.minimum ==
nullptr) {
344 for (uint8_t i = 0; i < _sensorCount; i++) {
345 calibration.maximum[i] = 0;
346 calibration.minimum[i] = _maxValue;
349 calibration.initialized =
true;
352 for (uint8_t j = 0; j < 10; j++) {
353 read(sensorValues, mode);
355 for (uint8_t i = 0; i < _sensorCount; i++) {
357 if ((j == 0) || (sensorValues[i] > maxSensorValues[i])) {
358 maxSensorValues[i] = sensorValues[i];
362 if ((j == 0) || (sensorValues[i] < minSensorValues[i])) {
363 minSensorValues[i] = sensorValues[i];
369 for (uint8_t i = 0; i < _sensorCount; i++) {
372 if (minSensorValues[i] > calibration.maximum[i]) {
373 calibration.maximum[i] = minSensorValues[i];
378 if (maxSensorValues[i] < calibration.minimum[i]) {
379 calibration.minimum[i] = maxSensorValues[i];
391 readPrivate(sensorValues);
397 readPrivate(sensorValues);
407 readPrivate(sensorValues, 0, 2);
413 readPrivate(sensorValues, 1, 2);
427 readPrivate(offValues);
429 for (uint8_t i = 0; i < _sensorCount; i++) {
430 sensorValues[i] += _maxValue - offValues[i];
431 if (sensorValues[i] > _maxValue) {
434 sensorValues[i] = _maxValue;
466 read(sensorValues, mode);
468 for (uint8_t i = 0; i < _sensorCount; i++) {
469 uint16_t calmin, calmax;
497 uint16_t denominator = calmax - calmin;
500 if (denominator != 0) {
501 value = (((int32_t)sensorValues[i]) - calmin) * 1000 / denominator;
506 }
else if (value > 1000) {
510 sensorValues[i] = value;
518void QTRSensors::readPrivate(uint16_t *sensorValues, uint8_t start, uint8_t step)
520 if (_sensorPins ==
nullptr) {
526 for (uint8_t i = start; i < _sensorCount; i += step) {
527 sensorValues[i] = _maxValue;
529 pinMode(_sensorPins[i], OUTPUT);
531 digitalWrite(_sensorPins[i], HIGH);
534 delayMicroseconds(10);
544 uint32_t startTime = micros();
547 for (uint8_t i = start; i < _sensorCount; i += step) {
549 pinMode(_sensorPins[i], INPUT);
554 while (time < _maxValue) {
559 time = micros() - startTime;
560 for (uint8_t i = start; i < _sensorCount; i += step) {
561 if ((digitalRead(_sensorPins[i]) == LOW) && (time < sensorValues[i])) {
563 sensorValues[i] = time;
572 case QTRType::Analog:
574 for (uint8_t i = start; i < _sensorCount; i += step) {
578 for (uint8_t j = 0; j < _samplesPerSensor; j++) {
579 for (uint8_t i = start; i < _sensorCount; i += step) {
581 sensorValues[i] += analogRead(_sensorPins[i]);
586 for (uint8_t i = start; i < _sensorCount; i += step) {
587 sensorValues[i] = (sensorValues[i] + (_samplesPerSensor >> 1)) /
597uint16_t QTRSensors::readLinePrivate(uint16_t *sensorValues,
QTRReadMode mode,
611 for (uint8_t i = 0; i < _sensorCount; i++) {
612 uint16_t value = sensorValues[i];
613 if (invertReadings) {
614 value = 1000 - value;
624 avg += (uint32_t)value * (i * 1000);
631 if (_lastPosition < (_sensorCount - 1) * 1000 / 2) {
636 return (_sensorCount - 1) * 1000;
640 _lastPosition = avg / sum;
641 return _lastPosition;
645QTRSensors::~QTRSensors()
const uint8_t QTRNoEmitterPin
Represents an undefined emitter control pin.
QTRReadMode
Emitter behavior when taking readings.
QTREmitters
Emitters selected to turn on or off.
const uint8_t QTRMaxSensors
The maximum number of sensors supported by an instance of this class.
void calibrate(QTRReadMode mode=QTRReadMode::On)
Reads the sensors for calibration.
void setDimmingLevel(uint8_t dimmingLevel)
Sets the dimming level.
void setTypeRC()
Specifies that the sensors are RC.
void setEmitterPin(uint8_t emitterPin)
Sets the emitter control pin for the sensors.
void setEmitterPins(uint8_t oddEmitterPin, uint8_t evenEmitterPin)
Sets separate odd and even emitter control pins for the sensors.
void setSamplesPerSensor(uint8_t samples)
Sets the number of analog readings to average per analog sensor.
void setTypeAnalog(uint16_t maxVal=1023)
Specifies that the sensor type is analog.
void emittersOn(QTREmitters emitters=QTREmitters::All, bool wait=true)
Turns the IR LEDs on.
void emittersOff(QTREmitters emitters=QTREmitters::All, bool wait=true)
Turns the IR LEDs off.
CalibrationData calibrationOff
Data from calibrating with emitters off.
void read(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the raw sensor values into an array.
void readCalibrated(uint16_t *sensorValues, QTRReadMode mode=QTRReadMode::On)
Reads the sensors and provides calibrated values between 0 and 1000.
void resetCalibration()
Resets all calibration that has been done.
void setTimeout(uint16_t timeout)
Sets the timeout for RC sensors.
void emittersSelect(QTREmitters emitters)
Turns on the selected emitters and turns off the other emitters with optimized timing.
CalibrationData calibrationOn
Data from calibrating with emitters on.
void releaseEmitterPins()
Releases emitter pin/pins that have been set.
void setSensorPins(const uint8_t *pins, uint8_t sensorCount)
Sets the sensor pins.
uint16_t * minimum
Lowest readings seen during calibration.
uint16_t * maximum
Highest readings seen during calibration.
bool initialized
Whether array pointers have been allocated and initialized.