This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSlowPWM.cpp
More file actions
92 lines (83 loc) · 2.05 KB
/
SlowPWM.cpp
File metadata and controls
92 lines (83 loc) · 2.05 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
SlowPWM - v1.0.1 - 19/11/2014
Arduino library for low frequency PWM.
Created by William Koch.
Released into the public domain.
*/
#include "Arduino.h"
#include "SlowPWM.h"
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
SlowPWM::SlowPWM(unsigned long duty_cycle, byte input, byte output) {
pinMode(input, INPUT);
pinMode(output, OUTPUT);
_duty_cycle = duty_cycle;
_inputPin = input;
_output = output;
_start = 0;
_status = _active = false;
_res = 1023;
bool _inputPin = true;
}
SlowPWM::SlowPWM(unsigned long duty_cycle, int* input, byte output) {
pinMode(output, OUTPUT);
_duty_cycle = duty_cycle;
_inputInt = input;
_output = output;
_start = 0;
_status = _active = false;
_res = 1023;
bool _inputPin = false;
}
SlowPWM::SlowPWM(unsigned long duty_cycle, byte input, byte output, byte bits) {
pinMode(input, INPUT);
pinMode(output, OUTPUT);
_duty_cycle = duty_cycle;
_inputPin = input;
_output = output;
_start = 0;
_status = _active = false;
bits == 12 ? _res = 4095 : _res = 1023;
bool _inputPin = true;
}
void SlowPWM::on() {
if (!_status) {
_status = true;
}
SlowPWM::update();
}
void SlowPWM::update() {
_now = millis();
if (_status) {
if (_start >= _end || _start == 0) {
if (_inputPin) {
_high = _start + map(analogRead(_inputPin), 0, _res, 0, _duty_cycle);
}
else {
int input = *_inputInt;
_high = _start + map(input, 0, 255, 0, _duty_cycle);
}
_end = _start + _duty_cycle;
if (_start == 0) {_start = _now;}
}
if (BETWEEN(_now, _start, _high) && !_active) { // _now >= _start && _now <= _high
digitalWrite(_output, HIGH);
_active = true;
} else if (BETWEEN(_now, _high, _end) && _active) { // _now > _high && _now < _end
digitalWrite(_output, LOW);
_active = false;
} else if (_now >= _end) {
_start = _end;
}
}
}
void SlowPWM::off() {
if (_status) {
_status = false;
digitalWrite(_output, LOW);
}
}
void SlowPWM::setDutyCycle(unsigned long dC) {
if (dC > 0) {
_duty_cycle = dC;
}
}