-
Notifications
You must be signed in to change notification settings - Fork 1
/
cLFPWM.h
49 lines (37 loc) · 1 KB
/
cLFPWM.h
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
// cLFPWM.h
#ifndef _CLFPWM_h
#define _CLFPWM_h
#include "Arduino.h"
class cLFPWM
{
public:
// Default period is 10sec
cLFPWM(unsigned long SampleTime_ = 10000)
{
SampleTime = SampleTime_;
LastTime = millis();
}
void setSampleTime(unsigned long SampleTime_){SampleTime = max(100,SampleTime_);}
boolean get( double Power )
{
// Limit Power
Power = min(max(Power,0),1);
unsigned long elapsedTime = millis()-LastTime;
// Interval in which the PWM is high:
unsigned long activeTime = round(Power*SampleTime);
// active if elapsedTime is less than activeTime, false if it is more
boolean active = (elapsedTime < activeTime);
// SampleTime is over and new period starts
if(elapsedTime >= SampleTime) {
//Set the next Last Period Time
LastTime = millis();
// Set active if switch time for next period is not exceeded
active = (elapsedTime < (activeTime+SampleTime));
}
return active;
}
private:
unsigned long SampleTime;
unsigned long LastTime;
};
#endif