PumpenSteuerung/src/Heating.cpp

84 lines
2.9 KiB
C++
Raw Normal View History

2020-11-10 17:38:33 +01:00
//
// Created by lukas on 10.11.20.
//
#include "Heating.h"
#include "Pins.h"
void Heating::init(Temperature *tempsensor, unsigned mode) {
switch (mode) {
case TIME: {
const unsigned percentOn = 20;
2020-11-17 18:37:47 +01:00
const unsigned refreshperiod = 900;
const auto func = [this]() {
2020-11-10 17:38:33 +01:00
mLuefterTicker.detach();
// every minute turn on the heating
2020-11-10 17:38:33 +01:00
digitalWrite(LuefterPin, HIGH);
digitalWrite(HeizungPin, HIGH);
Serial.println("Turning on heating");
mTurnOffTicker.once((int) ((float) refreshperiod * (float) percentOn / 100.0), []() {
digitalWrite(HeizungPin, LOW);
Serial.println("Turned off heating!");
});
mLuefterTicker.once((int) (((float) refreshperiod * (float) percentOn / 100.0) + 30.0), []() {
digitalWrite(LuefterPin, LOW);
Serial.println("Turned off fan!");
});
};
func();
2020-11-17 18:37:47 +01:00
mHeizungTicker.attach(refreshperiod, func);
break;
}
case HUMIDITY:
mHeatingStatus = false;
mHeizungTicker.attach(10, [&tempsensor, this]() {
// shedule this function because tempread is blocking
schedule_function([this, &tempsensor]() {
// check temperature
Serial.println("checking humidity");
const float hum = tempsensor->getHum();
if (hum == -1) return;
Serial.print("humidity read: ");
Serial.println(hum);
if (hum > 65.0) {
// turn off active turnoff timers
mLuefterTicker.detach();
Serial.println("heating should run now!");
// turn on heating and fan
digitalWrite(LuefterPin, HIGH);
digitalWrite(HeizungPin, HIGH);
mHeatingStatus = true;
} else if (hum < 60.0) {
// if humidity too low turn off heating and fan after 60secs
digitalWrite(HeizungPin, LOW);
Serial.println("heating should NOT run now!");
// if heating status in on set ticker to turn of fan in 60sec
if (mHeatingStatus) {
mLuefterTicker.once(60, []() {
// turn off fan
digitalWrite(LuefterPin, LOW);
Serial.println("turning off fan");
});
}
mHeatingStatus = false;
}
});
});
break;
default:
break;
}
2020-11-10 17:38:33 +01:00
}