-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectanceSensor.cpp
More file actions
71 lines (59 loc) · 1.38 KB
/
ReflectanceSensor.cpp
File metadata and controls
71 lines (59 loc) · 1.38 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
/*
--FILE--
ReflectanceSensor.cpp
--AUTHOR--
Name: Josh Alan
GitHub: theDataSmith
E-mail: thedatasmith1@gmail.com
--PROJECT--
Euclid, the line-following robot.
GitHub: github.com/theDataSmith/euclid
--CREATION DATE--
11 / 22 / 2016
*/
#include "ReflectanceSensor.h"
namespace EuclidRobot
{
ReflectanceSensor::ReflectanceSensor(int pin) : pin(pin)
{
setHighOutput();
}
void ReflectanceSensor::setHighOutput()
{
//Set the pin to OUTPUT HIGH.
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
/*
Record the time in microseconds so that if we set the pin LOW,
we can make sure it's been at least MICROS_MIN_DELAY microseconds.
*/
if (reading)
{
reading = false;
microsDelayStart = micros();
}
}
void ReflectanceSensor::setLowInput()
{
//Get the current time in microseconds.
unsigned long microsTime = micros();
//Make sure it's been at least MICROS_MIN_DELAY microseconds since the pin was set HIGH.
if (microsTime - microsDelayStart < MICROS_MIN_DELAY)
{
delayMicroseconds(MICROS_MIN_DELAY + microsDelayStart - microsTime);
}
//Put the pin in LOW INPUT mode.
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
reading = true;
}
bool ReflectanceSensor::read()
{
//If it's not in input mode, return HIGH. Otherwise, return the state of the pin.
return !reading || digitalRead(pin);
}
bool ReflectanceSensor::isReading()
{
return reading;
}
}