-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_sensors.h
71 lines (55 loc) · 1.3 KB
/
line_sensors.h
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
64
65
66
67
68
69
70
71
#ifndef _Line_follow_h
#define _Line_follow_h
//Number of readings to take for calibration
//const int NUM_CALIBRATIONS = 500;
/*
* Class to represent a single line sensor
*/
class Line_Sensor
{
public:
//Constructor
Line_Sensor(int Line_pin);
//Calibrate
void calibrate();
//Return the uncalibrated value from the sensor
float read_raw();
//Return the calibrated value from the sensor
float read_calibrated();
private:
int pin;
float sensorValue = 0;
float sensorMin = 1023; // minimum sensor value
float sensorMax = 0; // maximum sensor value
};
Line_Sensor::Line_Sensor(int Line_pin)
{
pin = Line_pin;
pinMode(pin, INPUT);
}
float Line_Sensor::read_raw()
{
return analogRead(pin);
}
void Line_Sensor::calibrate()
{
sensorValue = analogRead(pin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
float Line_Sensor::read_calibrated()
{
/*
* Write code to return a calibrated reading here
*/
sensorValue = analogRead(pin);
return (sensorValue - sensorMin) * 1023 / (sensorMax - sensorMin);
// return sensorValue;
}
#endif