HomeWaterLeaksDetection
PulseGenerator.cpp
Go to the documentation of this file.
1 #include "PulseGenerator.h"
2 
3 PulseGenerator::PulseGenerator(time_t delayHigh, time_t delayLow, int outputPin) {
4  // stores the values passed through the
5  // constructor into member variables
6  this->delayHigh = delayHigh;
7  this->delayLow = delayLow;
8  this->outputPin = outputPin;
9 
10  // initially reset (default state)
11  reset();
12 }
13 
15  // resets the member variables
16  // to their default values
17  value = LOW;
19  pulseCount = 0;
20 
21  // set the output pin into LOW state
22  pinMode(outputPin, OUTPUT);
23  digitalWrite(outputPin, value);
24 }
25 
27  // calculate the time difference
28  // between the current time and the time when
29  // the state of the pin changed for the last time
30  time_t currentTime = millis();
31  time_t deltaTime = DELTA_TIME(currentTime, timeLastValueChange);
32 
33  // if it is time to change the state of the pin,
34  // either from HIGH to LOW or from LOW to HIGH, change it.
35  if ((value == LOW && deltaTime >= delayLow) || (value == HIGH && deltaTime >= delayHigh)) {
36  // update the values of the member function
37  timeLastValueChange = currentTime;
38  value = !value;
39 
40  // update the state of the output pin
41  digitalWrite(outputPin, value);
42 
43  // if the state of the pin just went from HIGH to LOW,
44  // increment the counter of pulses
45  // (another pulse was just generated)
46  if (value == LOW) {
47  pulseCount++;
48  }
49  }
50 }
PulseGenerator::update
void update()
Updates the state of the output pin.
Definition: PulseGenerator.cpp:26
PulseGenerator::value
int value
current state of the output pin (HIGH/LOW)
Definition: PulseGenerator.h:27
PulseGenerator::reset
void reset()
Resets the pulse generator.
Definition: PulseGenerator.cpp:14
PulseGenerator::pulseCount
int pulseCount
number of pulses that have been generated so far
Definition: PulseGenerator.h:28
time_t
unsigned long time_t
Referring to the data type unsigned long as time_t.
Definition: DateTime.h:20
PulseGenerator::delayLow
time_t delayLow
delay of the pulse in LOW state
Definition: PulseGenerator.h:24
PulseGenerator.h
PulseGenerator::delayHigh
time_t delayHigh
delay of the pulse in HIGH state
Definition: PulseGenerator.h:23
DELTA_TIME
#define DELTA_TIME(time1, time2)
Calculates the time difference between two times given as parameters.
Definition: DateTime.h:35
PulseGenerator::timeLastValueChange
time_t timeLastValueChange
time when the state of the pin changed for the last time
Definition: PulseGenerator.h:30
PulseGenerator::PulseGenerator
PulseGenerator(time_t delayHigh, time_t delayLow, int outputPin)
Creates an instance of the class.
Definition: PulseGenerator.cpp:3
PulseGenerator::outputPin
int outputPin
number of the pin where the pulses are generated
Definition: PulseGenerator.h:25