-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRSensor.h
More file actions
68 lines (53 loc) · 1.33 KB
/
Copy pathIRSensor.h
File metadata and controls
68 lines (53 loc) · 1.33 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
#ifndef IR_SENSOR_H
#define IR_SENSOR_H
#include <Arduino.h>
// =====================
// IR SENSOR PINS
// =====================
#define IR_LEFT_PIN A3
#define IR_RIGHT_PIN A4
// =====================
// THRESHOLDS
// =====================
#define IR_BLACK_THRESHOLD 500
#define IR_WHITE_THRESHOLD 200
// =====================
// PUBLIC API
// =====================
inline void irSensorSetup() {
pinMode(IR_LEFT_PIN, INPUT);
pinMode(IR_RIGHT_PIN, INPUT);
}
inline int readIRLeft() {
return analogRead(IR_LEFT_PIN);
}
inline int readIRRight() {
return analogRead(IR_RIGHT_PIN);
}
inline bool irLeftOnBlack() {
return analogRead(IR_LEFT_PIN) > IR_BLACK_THRESHOLD;
}
inline bool irRightOnBlack() {
return analogRead(IR_RIGHT_PIN) > IR_BLACK_THRESHOLD;
}
inline bool irLeftOnWhite() {
return analogRead(IR_LEFT_PIN) < IR_WHITE_THRESHOLD;
}
inline bool irRightOnWhite() {
return analogRead(IR_RIGHT_PIN) < IR_WHITE_THRESHOLD;
}
inline bool bothOnLine() {
return irLeftOnBlack() && irRightOnBlack();
}
inline bool bothOffLine() {
return !irLeftOnBlack() && !irRightOnBlack();
}
inline int getLinePosition() {
bool left = irLeftOnBlack();
bool right = irRightOnBlack();
if (left && right) return 0;
if (left && !right) return -1;
if (!left && right) return 1;
return 0;
}
#endif // IR_SENSOR_H