-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrampsensor.ino
73 lines (54 loc) · 1.33 KB
/
rampsensor.ino
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
#include <QTRSensors.h>
#define ROBORIO_PORT 11
#define SENSOR_PORT 12
#define LED_PORT 13
#define ON HIGH
#define OFF LOW
#define DEBUG 0
// The sensor threshold should be between the raw value returned
// when above red/blue carpet and white ramps.
#define SENSOR_THRESHOLD 200
QTRSensorsRC qtrrc;
void setLed(int state) {
digitalWrite(LED_PORT, state);
}
void setRoborio(int state) {
digitalWrite(ROBORIO_PORT, state);
}
// Reads a QTR-1RC sensor and returns a value between 0 and the sensor timeout as set
// during init(). Default is 0 (~light) to 2000 (~dark).
unsigned int readSensor() {
unsigned int values[] = { 0 };
qtrrc.read(values);
#if DEBUG
Serial.print("Sensor => ");
Serial.println(values[0], DEC);
#endif
return values[0];
}
// Returns true if the sensor is triggered *count* times in a row.
bool sensorTrigger(unsigned int count = 3) {
while (count-- > 0) {
if (readSensor() >= SENSOR_THRESHOLD) { return false; }
}
return true;
}
void setup(){
pinMode(ROBORIO_PORT, OUTPUT);
pinMode(SENSOR_PORT, INPUT_PULLUP);
pinMode(LED_PORT, OUTPUT);
#if DEBUG
Serial.begin(9600);
#endif
qtrrc.init((unsigned char[]) {SENSOR_PORT}, 1);
setLed(OFF);
}
void loop(){
if (sensorTrigger()) {
setRoborio(LOW);
setLed(ON);
} else {
setRoborio(HIGH);
setLed(OFF);
}
}