I'm having trouble getting my code to do what I want.
If fanmode ==1 and temp_set is 19.5 i want the fan to go on 75% until temp_srum reaches 18.5 and the go to 42%. but not jump back to 75% until temp_srum goes below 18.0 again.
When the code below is used the fan goes to 42% when temp_srum is > 18 and the keeps flickering between 75 and 42 as temp_srum is flipping between beeing greater or less than 18.
// Adjust fan speed according to temperatures and fan mode
if (fanmode == 0) {
fanspeed = 0; // Inactivate fan, you are in OFF mode
}
if (fanmode == 2) {
fanspeed = 75; // Put fan on 75%, you are in FULL mode
}
else if (fanmode == 1 && temp_vrum < 20) {
fanspeed = 20; // You are below 20deg in vrum, put fan on 20%
}
else if (fanmode == 1 && temp_vrum > 20 && temp_srum < temp_set-1.5 && temp_srum <
temp_set-1.0) {
fanspeed = 75; // You are in AUTO mode, put fan on 75%
}
else if (fanmode == 1 && temp_vrum > 20 && temp_srum <= temp_set-0.1 && temp_srum <
temp_set+0.2) {
fanspeed = 42; // You are in AUTO mode, put fan on 42%
}
else if (fanmode == 1 && temp_vrum > 20 && temp_srum >= temp_set+0.2) {
fanspeed = 20; // You are in AUTO mode, put fan on 20%
}
dimmer.setPower(fanspeed); // Send new fan speed value to fan controller
Here is the problem:
(temp_srum < temp_set - 1.5 && temp_srum < temp_set - 1.0)
This is the same as:
(temp_srum < temp_set - 1.5)
Similar to other. You have to specify range, however not like this linear. Another problem I have found is that on fanmode=1 and exactly 20 degree, your fan will not be set at all.
Hi. I'm new to programming and now I have a problem that I cant seem to figure out.
I'm trying to adjust the speed of a fan depending on a temp reading. I want to have the fan going on 75% until temp reaches 18.5 and then switch to 42%. BUT, I dont want it to go back to 75% until temp drops below 18.0.
The reason is that I don't want the fan to flip between 42% and 75% when the temp is going quickly up and down around the defined temp.
I tried with if/else if but can't get it to do what I want. Any suggestions?
This is not solution for your requirements. It is just an example how your code may be much clearer and readable.
This is just brief and basic example of an automatic cooling control based on predefined temperature differences.
And of course, not tested.
int fanmode = 0;
int fanspeed = 0;
bool setFan = true; // indicate fan need to be set
float t_c = 0; // current room temp
float t_s = 0; // temp when fan is set
void doSetfan()
{
// setFan(fanspeed);
setFan = false;
t_s = t_c; // temp wen fan is set
}
void setup() {
// Read temp from sensor and put it in t_c
}
void loop() {
// put your main code here, to run repeatedly:
float diff = 0;
switch (fanmode)
{
case 0:
fanspeed = 0;
break;
case 1:
if (t_c > 20)
{
// Cooling
diff = abs(t_c - t_s);
if ( diff < 0.5) // If less than 0.5 degree difference - stop fan
{
fanspeed = 0;
setFan = true;
}
if ( diff > 1.0) // If more than 1 degree difference - cool very slow
{
fanspeed = 20;
setFan = true;
}
if ( diff > 2.0) // If more than 2 degree difference - cool slower
{
fanspeed = 42;
setFan = true;
}
if ( diff > 5.0) // If more than 5 degree difference - cool faster
{
fanspeed = 75;
setFan = true;
}
}
else
{
//Heating up?
}
break;
case 2:
fanspeed = 75;
break;
}
// Go and set fan if requested
if (setFan) doSetfan();
// sleep time, read temperature in t_c after few seconds or whatever
}