diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -92,6 +92,8 @@ int main(void) uint32_t last_thermostat = 0; int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD + uint8_t thermostat_plant_on = 0; + // Main loop while(1) { @@ -180,16 +182,20 @@ int main(void) temp_frac = status.temp > 0 ? temp_frac : temp_frac * -1; int32_t temp = (status.temp * 10) + temp_frac; - uint8_t plant_on = 0; // EMZ FIXME: This could be way simpler - if(set.val.plant_type == PLANT_HEATER && status.setpoint * 10 < temp) - plant_on = 1; - else if(set.val.plant_type == PLANT_COOLER && status.setpoint * 10 > temp) - plant_on = 1; + if(set.val.plant_type == PLANT_HEATER && status.setpoint * 10 < temp - set.val.hysteresis * 10) + thermostat_plant_on = 1; + else if(set.val.plant_type == PLANT_HEATER && status.setpoint * 10 > temp + set.val.hysteresis * 10) + thermostat_plant_on = 0; + + if(set.val.plant_type == PLANT_COOLER && status.setpoint * 10 > temp + set.val.hysteresis * 10) + thermostat_plant_on = 1; + else if(set.val.plant_type == PLANT_COOLER && status.setpoint * 10 < temp - set.val.hysteresis * 10) + thermostat_plant_on = 0; // EMZ: TODO: Refactor to output_enabled or something - if(status.pid_enabled && plant_on) + if(status.pid_enabled && thermostat_plant_on) { // EMZ TODO: functionalize this // put ssr output on display