-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilitySensor.cpp
69 lines (57 loc) · 1.46 KB
/
UtilitySensor.cpp
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
#include "UtilitySensor.h"
UtilitySensor::UtilitySensor(int ledPin, int sensorPin)
: LedPin(ledPin), SensorPin(sensorPin)
{
}
void UtilitySensor::initialize()
{
//pinMode(SensorPin, INPUT);
pinMode(SensorPin, INPUT_PULLUP);
pinMode(LedPin, OUTPUT);
// set initial LED state
digitalWrite(LedPin, LOW);
}
void UtilitySensor::handleIrq(unsigned long now)
{
digitalWrite(LedPin, LOW);
int sensorReading = digitalRead(SensorPin);
// Reset the last sensor reading if the value changed, due to noise
if (sensorReading != m_lastSensorState)
{
m_lastSensorReading = now;
}
// Whatever the sensor reading is at, it's been there for longer than
// the time between timer interrupts, take it as the actual current state
if (((now - m_lastSensorReading) > 0) &&
(sensorReading != m_sensorState))
{
m_sensorState = sensorReading;
if (sensorReading == HIGH)
{
++m_counter;
digitalWrite(LedPin, HIGH);
}
}
// Save the sensor reading. Next time through the loop, it'll be the last sensor state
m_lastSensorState = sensorReading;
}
unsigned long UtilitySensor::getCounter() const
{
noInterrupts();
return m_counter;
interrupts();
}
void UtilitySensor::setCounter(unsigned long counter)
{
noInterrupts();
m_counter = counter;
interrupts();
}
void UtilitySensor::setOldCounter(unsigned long counter)
{
m_oldCounter = counter;
}
bool UtilitySensor::counterIncremented() const
{
return getCounter() > m_oldCounter;
}