Brewery-Controller/boil_kettle/slowPWM.ino

28 lines
755 B
C++

// Bit bang low frequency PWM.
//
// Parameters:
// outputPin (byte) - Pin number to output PWM.
// dutyCycle (byte) - PWM period from 0 - 100.
void slowPWM(byte outputPin, byte dutyCycle, unsigned long period)
{
static byte outputState = LOW;
static unsigned long lastSwitchTime = 0;
unsigned long onTime = (dutyCycle * period) / 100;
unsigned long offTime = period - onTime;
unsigned long currentTime = millis();
if (outputState == HIGH && (currentTime - lastSwitchTime >= onTime))
{
lastSwitchTime = currentTime;
outputState = LOW;
}
if (outputState == LOW && (currentTime - lastSwitchTime >= offTime))
{
lastSwitchTime = currentTime;
outputState = HIGH;
}
digitalWrite(outputPin, outputState);
}