-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncPulse.cpp
More file actions
54 lines (48 loc) · 1013 Bytes
/
AsyncPulse.cpp
File metadata and controls
54 lines (48 loc) · 1013 Bytes
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
/*
AsyncPulse.h - Library for getting a pulse with custom period.
Created by Giuse Petroso, 2020.
Released into the public domain.
*/
#include "Arduino.h"
#include "AsyncPulse.h"
AsyncPulse::AsyncPulse(unsigned long timeOn, unsigned long timeOff)
{
_state = 1;
_timeOn = timeOn;
_timeOff = timeOff;
_countOn = 0;
_countOff = 0;
}
int AsyncPulse::get()
{
unsigned long millisecs = millis();
if (_state == 1)
{
if (millisecs - _countOn >= _timeOn)
{
_state = 0;
_countOff = millisecs;
}
}
if (_state == 0)
{
if (millisecs - _countOff >= _timeOff)
{
_state = 1;
_countOn = millisecs;
}
}
Serial.print("STATE: ");
Serial.print(_state);
Serial.print(" | TON: ");
Serial.print(millisecs - _countOn);
Serial.print(" / ");
Serial.print(_timeOn);
Serial.print(" | TOFF: ");
Serial.print(millisecs - _countOff);
Serial.print(" / ");
Serial.print(_timeOff);
Serial.println("");
// return the state
return _state;
}