Use integer math.

This feels like a good idea. ?
This commit is contained in:
Chris Giacofei 2022-01-19 09:47:59 -05:00
parent 2a70f6e89a
commit c4fb9fa0b3
4 changed files with 20 additions and 20 deletions

View File

@ -19,7 +19,7 @@
// Global variables.
uint8_t KettleDuty = 0;
double KettleTemp;
uint8_t KettleTemp;
uint8_t KettleSetpoint;
modes KettleMode = OFF;
@ -45,7 +45,6 @@ unsigned long lastRun = 0;
// Return a character array to represent the
// On/Off state of the kettle.
char* ShowKettleState() {
char* LCD_Line;
if (KettleMode == MANUAL) {
return (char*)"Kettle: Manual";
} else if (KettleMode == AUTOMATIC) {
@ -56,11 +55,13 @@ char* ShowKettleState() {
}
char* ShowKettleSetting() {
char* LCD_Line;
char LCD_Line[20];
if (KettleMode == MANUAL) {
return sprintf(LCD_Line, "Kettle Power: %03d%", KettleDuty);
sprintf(LCD_Line, "Kettle Power: %03d%", KettleDuty / 10);
return LCD_Line;
} else if (KettleMode == AUTOMATIC) {
return sprintf(LCD_Line, "Kettle Temp: %03dF", KettleSetpoint);
sprintf(LCD_Line, "Kettle Temp: %03dF", KettleSetpoint / 10);
return LCD_Line;
} else {
return (char*)"";
}
@ -77,10 +78,10 @@ void doEncoder()
if (result) {
uint8_t speed = rotary.speed();
speed >= 10 ? inc = 5 : inc = 1;
speed >= 10 ? inc = 50 : inc = 10;
}
if (result == DIR_CW && KettleDuty < 100) {
if (result == DIR_CW && KettleDuty < 1000) {
KettleDuty = (KettleDuty / inc) * inc + inc;
} else if (result == DIR_CCW && KettleDuty > 0) {
KettleDuty = (KettleDuty / inc) * inc - inc;
@ -130,7 +131,7 @@ void setup() {
void UpdateBoilKettle(){
static uint8_t last_KettleDuty = 0;
static double last_KettleTemp = 0;
static uint8_t last_KettleTemp = 0;
if (Enter.pressed()) {
@ -157,7 +158,7 @@ void UpdateBoilKettle(){
}
void loop() {
KettleTemp = KettleThermo.readRTD();
KettleTemp = (uint8_t)(KettleThermo.readRTD() * 10);
UpdateBoilKettle();
unsigned long elapsedTime = (millis() - lastRun);

View File

@ -20,8 +20,8 @@ class slowPWM {
Serial.println("Setup PWM");
}
byte compute(byte duty) {
unsigned long onTime = (duty * period) / 100;
byte compute(uint8_t duty) {
unsigned long onTime = (duty * period) / 1000;
unsigned long offTime = period - onTime;
unsigned long currentTime = millis();

View File

@ -3,13 +3,13 @@
#include <EEPROM.h>
#include "thermoControl.h"
thermoControl::thermoControl(double* input_temp, uint8_t* setpoint_temp, uint8_t* output_pwm, uint8_t* max_threshold, uint8_t* hysteresis) {
thermoControl::thermoControl(uint8_t* input_temp, uint8_t* setpoint_temp, uint8_t* output_pwm, uint8_t* max_threshold, uint8_t* hysteresis) {
output_pwm = output_pwm;
input_temp = input_temp;
setpoint_temp = setpoint_temp;
outMax = 100;
outMin = 10;
outMax = 1000;
outMin = 100;
OpMode = OFF;
SampleTime = 100;
@ -22,11 +22,10 @@ bool thermoControl::Compute() {
unsigned long timeChange = (now - lastTime);
if(timeChange>=SampleTime && OpMode == AUTOMATIC) {
double output;
double input = *input_temp;
double error = (double)*setpoint_temp - input;
uint8_t output;
uint8_t error = *setpoint_temp - *input_temp;
if (error >= (double)*max_threshold) {
if (error >= *max_threshold) {
output = outMax;
} else if (error > *hysteresis) {
output = 100 * error / *max_threshold;

View File

@ -19,11 +19,11 @@ class thermoControl {
unsigned long lastTime;
public:
thermoControl(double*, uint8_t*, uint8_t*, uint8_t*, uint8_t*);
thermoControl(uint8_t*, uint8_t*, uint8_t*, uint8_t*, uint8_t*);
bool Compute();
void SetSampleTime(int);
void SetPowerLimits(uint8_t, uint8_t);
void SetHysteresis(double);
void SetHysteresis(uint8_t);
void SetThreshPWR(uint8_t);
void SetMode(modes);
modes GetMode();