Skip to content

Commit 51b3963

Browse files
committed
0.1.3 PulseDivider
1 parent 4f38a0d commit 51b3963

File tree

7 files changed

+116
-20
lines changed

7 files changed

+116
-20
lines changed

libraries/PulseDivider/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.1.3] - 2025-04-11
10+
- fix invert flag in constructor
11+
- add getInCount() and getOutCount() for debugging.
12+
- add example frequencyMultiplier.ino
13+
- update readme.md
14+
- minor edits
15+
916
## [0.1.2] - 2023-09-16
1017
- add getCounter()
1118
- update readme.md
1219
- made private read() inline
1320
- minor edits
1421

15-
1622
## [0.1.1] - 2023-09-14
1723
- fix initial state output (setters).
1824
- optimized checking

libraries/PulseDivider/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023-2024 Rob Tillaart
3+
Copyright (c) 2023-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/PulseDivider/PulseDivider.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//
33
// FILE: PulseDivider.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.2
5+
// VERSION: 0.1.3
66
// DATE: 2023-09-13
77
// PURPOSE: Arduino library to divide a pulse stream with a float factor.
88
// URL: https://github.com/RobTillaart/PulseDivider
99

1010

1111
#include "Arduino.h"
1212

13-
#define PULSEDIVIDER_LIB_VERSION (F("0.1.2"))
13+
#define PULSEDIVIDER_LIB_VERSION (F("0.1.3"))
1414

1515

1616
class PulseDivider
@@ -20,13 +20,12 @@ class PulseDivider
2020
PulseDivider(uint8_t inPin, uint8_t outPin, uint16_t inCount, uint16_t outCount,
2121
uint32_t duration = 1, uint8_t edge = RISING, bool invert = false)
2222
{
23+
setInvert(invert); // must be done before setOutPin() !
2324
setInPin(inPin);
2425
setOutPin(outPin);
2526
setRatio(inCount, outCount);
2627
setDuration(duration);
2728
setEdge(edge);
28-
setInvert(invert);
29-
3029
_prevState = _read();
3130
stop();
3231
}
@@ -116,18 +115,16 @@ class PulseDivider
116115
void start()
117116
{
118117
_prevState = _read();
119-
_counter = _inCount / 2;
118+
_counter = _inCount / 2; // reasonable start position.
120119
_running = true;
121120
}
122121

123-
124122
void stop()
125123
{
126124
_running = false;
127125
_write(_invert ? HIGH : LOW);
128126
}
129127

130-
131128
bool isRunning()
132129
{
133130
return _running;
@@ -161,7 +158,6 @@ class PulseDivider
161158
}
162159
}
163160

164-
165161
inline void doPulse()
166162
{
167163
_counter += _outCount;
@@ -178,7 +174,7 @@ class PulseDivider
178174
private:
179175

180176
// default reference
181-
void _write(uint8_t value)
177+
inline void _write(uint8_t value)
182178
{
183179
digitalWrite(_outPin, value);
184180
}

libraries/PulseDivider/README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ generated when there is an input pulse and an output pulse is "lagging".
108108
In the first tests the library seems to work well, however more testing is needed.
109109

110110

111-
#### Related
111+
### Related
112112

113113
- https://github.com/RobTillaart/PulsePattern
114+
- https://github.com/RobTillaart/OutPin
114115

115116

116117
## Interface
@@ -120,7 +121,7 @@ In the first tests the library seems to work well, however more testing is neede
120121
```
121122

122123

123-
#### Constructor
124+
### Constructor
124125

125126
- **PulseDivider(uint8_t inPin, uint8_t outPin, uint16_t inCount, uint16_t outCount, uint32_t duration = 1, uint8_t edge = RISING, bool invert = false)**
126127
- Define input pin and output pin.
@@ -137,14 +138,17 @@ In the first tests the library seems to work well, however more testing is neede
137138
The PulseDivider can do an 65534 to 1 reduction.
138139

139140

140-
#### Getters / Setters
141+
### Getters / Setters
141142

142143
See description Constructor.
143144

144145
- **void setInPin(uint8_t inPin)** set or change the input pin (runtime).
145146
- **uint8_t getInPin()** returns the set input pin.
146147
- **void setOutPin(uint8_t outPin)** set or change the output pin (runtime).
147148
- **uint8_t getOutPin()** returns the set output pin.
149+
150+
### Ratio and duration
151+
148152
- **void setRatio(uint16_t inCount, uint16_t outCount = 1)** set the ratio between
149153
input pulses and the output pulses. inCount >= outCount > 0.
150154
The range for inCount can be 1..65534 max, the sum of inCount and outCount should not exceed 65535,
@@ -153,14 +157,22 @@ Typically both are less than 1000.
153157
- **void setDuration(uint32_t duration)** set the duration of the pulse.
154158
Note the unit is microseconds.
155159
- **uint32_t getDuration()** returns the set duration.
160+
161+
### Edge and invert
162+
156163
- **void setEdge(uint8_t edge)** sets the "trigger" edge, RISING or FALLING.
157164
- **uint8_t getEdge()** returns the set trigger.
158165
- **void setInvert(bool invert)** inverts the output pulse with respect to the input pulse.
159166
- **bool getInvert()** returns the set flag.
167+
168+
### Debugging
169+
160170
- **uint16_t getCounter()** returns the internal counter (for debugging).
171+
- **uint16_t getInCount()** idem (for debugging).
172+
- **uint16_t getOutCount()** idem (for debugging).
161173

162174

163-
#### Control
175+
### Control
164176

165177
The pulseDivider can be started or stopped in software,
166178
and the state can be requested.
@@ -174,7 +186,7 @@ output to "default" state LOW (unless inverted).
174186
- **bool isRunning()** returns true if running .
175187

176188

177-
#### Worker
189+
### Worker
178190

179191
- **void check()** Call as often as possible as this worker does the
180192
polling, math and calls **doPulse()** when an output pulse is needed.
@@ -193,9 +205,11 @@ polling, math and calls **doPulse()** when an output pulse is needed.
193205
- performance measurements
194206
- test different platforms, configurations, ratios etc.
195207
- optimize math possible? (16 bit instead of 32 bit for UNO).
196-
- for many pulses - **PulseDivider(32 bit counters)**
208+
- add now = micros() in check()
209+
- for many pulses - **PulseDivider32(32 bit counters)**
197210
- allow e.g. 100000 to 1 or even 1000000 to 1 or more.
198-
- separate PulseDivider32 class?
211+
- separate PulseDivider32 class? ==> pulseDivider32.h?
212+
- derived class?
199213

200214
#### Could
201215

@@ -210,9 +224,14 @@ polling, math and calls **doPulse()** when an output pulse is needed.
210224
- make inCount and outCount 32 bit? even finer fractions.
211225
- would slow it down
212226
- would 8 bit make it faster?
227+
- improve read() for AVR
228+
213229

214230
#### Won't (unless requested)
215231

232+
- investigate **frequencyMultiplier.ino** as new core code.
233+
- drawback output does not stop immediately when input stops.
234+
216235

217236
## Support
218237

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// FILE: frequencyMultiplier.ino
2+
// AUTHOR: Rob Tillaart
3+
// DATE: 2024-10-26
4+
// PURPOSE: demo
5+
// URL: https://forum.arduino.cc/t/tacho-inlezen-en-puls-regelen/1315467/6
6+
7+
#include "Arduino.h"
8+
9+
const int inputPin = 9;
10+
const int outputPin = 13;
11+
12+
// INPUT VARS
13+
int currentSignal;
14+
int lastSignal = HIGH;
15+
uint32_t lastHigh = 0;
16+
17+
float multiplier = 0.15; // 0.25, 2, 3, etc
18+
19+
// OUTPUT VARS
20+
uint32_t lastPulse = 0;
21+
uint32_t outputInterval = 0;
22+
23+
// time it takes before output stops after input stops.
24+
// depends on input frequency
25+
const uint32_t STOPDELAY = 1000;
26+
27+
28+
void setup ()
29+
{
30+
Serial.begin(115200);
31+
pinMode (outputPin, OUTPUT);
32+
pinMode (inputPin, INPUT);
33+
}
34+
35+
void loop()
36+
{
37+
// take timestamp for this loop
38+
uint32_t now = millis();
39+
40+
// determine input frequency by measuring between
41+
// RISING edge of HIGH pulses
42+
currentSignal = digitalRead(inputPin);
43+
if ((currentSignal == HIGH) && (lastSignal == LOW))
44+
{
45+
uint32_t inputInterval = now - lastHigh;
46+
outputInterval = inputInterval / multiplier / 2;
47+
lastHigh = now; // remember begin of last pulse
48+
49+
Serial.println(inputInterval);
50+
Serial.println(outputInterval);
51+
Serial.println();
52+
}
53+
lastSignal = currentSignal;
54+
55+
// generate output pulse
56+
if (outputInterval > 0)
57+
{
58+
if ((now - lastPulse) >= outputInterval)
59+
{
60+
// remember last invert output
61+
lastPulse = now;
62+
// invert output
63+
digitalWrite(outputPin, !digitalRead(outputPin));
64+
}
65+
}
66+
67+
// switch to zero if no input pulse for STOPDELAY milliseconds
68+
if (now - lastHigh > STOPDELAY)
69+
{
70+
outputInterval = 0;
71+
}
72+
}
73+
74+
75+
// -- END OF FILE --

libraries/PulseDivider/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/PulseDivider.git"
1717
},
18-
"version": "0.1.2",
18+
"version": "0.1.3",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/PulseDivider/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=PulseDivider
2-
version=0.1.2
2+
version=0.1.3
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library to divide a pulse stream with a float factor.

0 commit comments

Comments
 (0)