-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensors.cpp
More file actions
145 lines (126 loc) · 2.78 KB
/
sensors.cpp
File metadata and controls
145 lines (126 loc) · 2.78 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "sensors.h"
#include "constants.cpp"
// SETUP
void setupSensors()
{
// Ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// IR sensor
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(IR3, INPUT);
pinMode(IR4, INPUT);
pinMode(IR5, INPUT);
pinMode(IR6, INPUT);
pinMode(IR7, INPUT);
}
// READ DATA
// Infrared sensor
int readLeftIRData()
{
/**
* Read the left IR sensor and return the value.
*
* @return 1 if line detected, 0 otherwise
*/
int ir = digitalRead(IR1);
return ir;
}
int readMiddleLeftIRData()
{
/**
* Read the middle left IR sensor and return the value.
*
* @return 1 if line detected, 0 otherwise
*/
int ir = digitalRead(IR2);
return ir;
}
int readCenterIRData()
{
/**
* Read the center IR sensor and return the value.
*
* @return 0 if no line detected,
* 1 if left and center IR detect a like, 2 if ONLY left IR detect a line,
* 3 if right and center IR detect a line, 4 if ONLY right IR detect a line,
* 5 if left and right IR detect a line
*/
int center_left_ir = digitalRead(IR3);
int center_center_ir = digitalRead(IR4);
int center_right_ir = digitalRead(IR5);
if (center_left_ir == 0 && center_right_ir == 0 && center_center_ir == 0)
{
return 0;
}
else if (center_left_ir == 1 && center_right_ir == 0)
{
if (center_center_ir == 1)
{
return 1;
}
else
{
return 2;
}
}
else if (center_left_ir == 0 && center_right_ir == 1)
{
if (center_center_ir == 1)
{
return 3;
}
else
{
return 4;
}
}
else
{
return 5;
}
}
int readMiddleRightIRData()
{
/**
* Read the middle right IR sensor and return the value.
*
* @return 1 if line detected, 0 otherwise
*/
int ir = digitalRead(IR6);
return ir;
}
int readRightIRData()
{
/**
* Read the right IR sensor and return the value.
*
* @return 1 if line detected, 0 otherwise
*/
int ir = digitalRead(IR7);
return ir;
}
// Ultrasonic sensor
int readUltrasonicData()
{
/**
* Read the distance from the ultrasonic sensor
*
* @return int distance in cm
*/
// Trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Get duration of the pulse
long duration = pulseIn(echoPin, HIGH, 2000UL);
if (duration == 0)
return 999;
// Calculate distance in cm
int distanceCm = duration * 0.034 / 2;
// Return distance in cm
return distanceCm;
}