Brewery-Controller/boil_kettle/slowPWM.h
Chris Giacofei 8b72a16203 Made a much more involved kettle control class.
All the input/output tracking is handled with class members
instead of passing pointers to global variables.
2022-01-21 10:56:41 -05:00

42 lines
953 B
C++

#ifndef SLOWPWM_h
#define SLOWPWM_h
#include <Arduino.h>
class slowPWM {
private:
byte outputPin;
unsigned long period;
unsigned long lastSwitchTime;
byte outputState;
public:
void begin(byte pin, unsigned long per) {
outputPin = pin;
period = per;
lastSwitchTime = 0;
outputState = LOW;
pinMode(pin, OUTPUT);
}
byte compute(uint8_t duty) {
unsigned long onTime = (duty * period) / 1000;
unsigned long offTime = period - onTime;
unsigned long currentTime = millis();
if (duty == 0) {
outputState = LOW;
} else if (outputState == HIGH && (currentTime - lastSwitchTime >= onTime)) {
lastSwitchTime = currentTime;
outputState = LOW;
} else if (outputState == LOW && (currentTime - lastSwitchTime >= offTime)) {
lastSwitchTime = currentTime;
outputState = HIGH;
}
return outputState;
}
};
#endif