Turn slowPWM into a class.

Slightly cleaner looking this way.
This commit is contained in:
Chris Giacofei 2022-01-14 08:23:51 -05:00
parent 69bf638c39
commit 22842dfc70
3 changed files with 48 additions and 35 deletions

View File

@ -15,6 +15,7 @@
// My Includes
#include "config.h"
#include "button.h"
#include "slowPWM.h"
// Pin definitions
#define encoderCLK 2
@ -28,12 +29,10 @@ bool KettleOn = false;
// User I/O objects.
Button Enter;
slowPWM boilPWM;
MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27,20,4);
float output = 0;
int updateInterval = 1000;
EthernetClient net;
MQTTClient mqtt_client;
@ -76,7 +75,7 @@ LiquidMenu menu(lcd);
void setup() {
unsigned long lastRun = millis() - updateInterval;
unsigned long lastRun = millis() - UpdateInterval;
Serial.begin(9600);
rotary.begin();
Ethernet.begin(mac, ip);
@ -88,7 +87,7 @@ void setup() {
pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP);
Enter.begin(encoderBTN);
pinMode(encoderBTN, INPUT_PULLUP);
boilPWM.begin(kettlePWM, PeriodPWM);
// if you get a connection, report back via serial:
if (Ethernet.linkStatus() == LinkON) {
@ -121,9 +120,9 @@ void UpdateBoilKettle(){
}
if (KettleOn) {
slowPWM(kettlePWM, KettleDuty, PeriodPWM);
boilPWM.compute(KettleDuty);
} else {
slowPWM(kettlePWM, 0, PeriodPWM);
boilPWM.compute(0);
}
}
@ -132,7 +131,7 @@ void loop() {
unsigned long elapsedTime = (millis() - lastRun);
if (elapsedTime >= updateInterval) {
if (elapsedTime >= UpdateInterval) {
mqtt_client.loop();
//if (!mqtt_client.connected()) ConnectMQTT();

41
boil_kettle/slowPWM.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef SLOWPWM_h
#define SLOWPWM_h
#include <Arduino.h>
class slowPWM {
private:
byte outputPin;
byte dutyCycle;
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);
}
void compute(byte duty) {
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);
}
};
#endif

View File

@ -1,27 +0,0 @@
// 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);
}