-
Notifications
You must be signed in to change notification settings - Fork 2
/
SensorHelper.cpp
63 lines (53 loc) · 1.32 KB
/
SensorHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "QTRSensors.h"
#include "SensorsHelper.h"
#include "Constants.h"
#include <Arduino.h>
#include "Pins.h"
unsigned int arraySensorsValue[NUM_SENSORS];
QTRSensorsAnalog frontalSensors((unsigned char[]){
A8, A7, A6, A5, A4, A3, A2, A1, A0, A20, A22
}, NUM_SENSORS, 1/*, 255*/); // 4(numSamples) and 255(emitterPin) are default
QTRSensorsAnalog rightSensor((unsigned char[]){
RIGHT_SENSOR_PIN
}, 1, 1);
QTRSensorsAnalog leftSensor((unsigned char[]){
LEFT_SENSOR_PIN
}, 1, 1);
void calibrateSensors()
{
frontalSensors.calibrate();
rightSensor.calibrate();
leftSensor.calibrate();
}
void resetCalibration()
{
frontalSensors.resetCalibration();
rightSensor.resetCalibration();
leftSensor.resetCalibration();
}
float readArray()
{
int line = frontalSensors.readLine(arraySensorsValue, QTR_EMITTERS_ON, USE_WHITE_LINE);
return (ARRAY_CENTER_POSITION - line)/1000.0; //invertido (ver followPath)
}
bool readRight()
{
unsigned int rightValue;
rightSensor.readCalibrated(&rightValue);
return rightValue < LINE_VALUE;
}
bool readLeft()
{
unsigned int leftValue;
leftSensor.readCalibrated(&leftValue);
return leftValue < LINE_VALUE;
}
bool changeState()
{
static unsigned long lastLeft = 0;
if ((readLeft()) and (millis() - lastLeft > 200)) {
lastLeft = millis();
return true;
}
return false;
}