Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit ac89504

Browse files
committed
SlowPWM v1.0 - added optional 10/12bits analog resolution
1 parent 62e17b6 commit ac89504

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ Include the library in your sketch:
1010

1111
#include <SlowPWM.h>
1212

13-
### Constructor
13+
### Constructors
1414

15-
This will create a SlowPWM object with a custom duty-cycle:
15+
There are 2 constructors which differ by analog resolution.
16+
17+
#### Default
18+
19+
This will create a SlowPWM object with a custom duty-cycle and 10bit analog resolution:
1620

1721
SlowPWM myPWM(1000, A1, 10);
1822
// 1000ms duty-cycle, input pin: A1, output pin: 10
1923

24+
#### 12bits Analog Resolution
25+
26+
The same as above, but with 12 bits analog resolution:
27+
28+
SlowPWM myPWM(1000, A1, 10, 12);
29+
// 1000ms duty-cycle, input pin: A1, output pin: 10, 12bits.
30+
2031
### Functions
2132

2233
SlowPWM has 3 functions.

SlowPWM.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ SlowPWM::SlowPWM(unsigned long duty_cycle, byte input, byte output) {
1818
_output = output;
1919
_start = 0;
2020
_status = _active = false;
21+
_res = 1023;
22+
}
23+
24+
SlowPWM::SlowPWM(unsigned long duty_cycle, byte input, byte output, byte bits) {
25+
pinMode(input, INPUT);
26+
pinMode(output, OUTPUT);
27+
_duty_cycle = duty_cycle;
28+
_input = input;
29+
_output = output;
30+
_start = 0;
31+
_status = _active = false;
32+
bits == 12 ? _res = 4095 : _res = 1023;
2133
}
2234

2335
void SlowPWM::on() {
@@ -31,7 +43,7 @@ void SlowPWM::update() {
3143
_now = millis();
3244
if (_status) {
3345
if (_start >= _end || _start == 0) {
34-
_high = _start + map(analogRead(_input), 0, 1023, 0, _duty_cycle);
46+
_high = _start + map(analogRead(_input), 0, _res, 0, _duty_cycle);
3547
_end = _start + _duty_cycle;
3648
if (_start == 0) {_start = _now;}
3749
}

SlowPWM.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
class SlowPWM {
1414
public:
1515
SlowPWM(unsigned long duty_cycle, byte input, byte output);
16+
SlowPWM(unsigned long duty_cycle, byte input, byte output, byte bits);
17+
1618
void on();
1719
void off();
1820
void update();
1921
private:
2022
unsigned long _duty_cycle, _start, _high, _end, _now;
23+
int _res;
2124
byte _input, _output;
2225
boolean _status, _active;
2326
};

0 commit comments

Comments
 (0)