4 CHANNEL (LM35 PWM) FAN CONTROLLER QUESTION

Hello friends,

Hello friends, I’m having this big problem in programming, where I noticed a big GAP in the reaction of the last 3 engines. I’m new to C ++ programming and I find it very hard to syntax and file structure, so if anyone can help me I’ll happily spend the entire Proteus project! (attached yet !)

Is there any way to define variables that make the program clean ?

Thank you !

// BLOCO DE PINOS DOS SENSORES

float temp;
float temp2;
float temp3;
float temp4;

int tempPin = A3; // PINO ANALOGICO (CPU) PRA USAR O LM35

int tempPin2 = A2; // PINO ANALOGICO (CASETOP) PRA USAR O LM35

int tempPin3 = A1; // PINO ANALOGICO (HD) PRA USAR O LM35

int tempPin4 = A0; // PINO ANALOGICO (RESERVADO) PRA USAR O LM35


// BLOCO DE TEMPERATURA MINIMA

int tempMin = 20; // TEMPERATURA MINIMA PRA RODAR O FAN (CPU)

int tempMin2 = 25; // TEMPERATURA MINIMA PRA RODAR O FAN (CASETOP)

int tempMin3 = 25; // TEMPERATURA MINIMA PRA RODAR O FAN (HD)

int tempMin4 = 25; // TEMPERATURA MINIMA PRA RODAR O FAN (RESERVADO)


// BLOCO DE TEMPERATURA MAXIMA

int tempMax = 45; // TEMPERATURA MAXIMA (CPU)

int tempMax2 = 38; // TEMPERATURA MAXIMA (CASETOP)

int tempMax3 = 40; // TEMPERATURA MAXIMA (HD)

int tempMax4 = 40; // TEMPERATURA MAXIMA (RESERVADO)


// BLOCO DE PINOS DOS FANS

int fan = 11; // PINO PWM ONDE O FAN (CPU) SERA CONECTADO

int fan2 = 10; // PINO PWM ONDE O FAN (CASETOP) SERA CONECTADO

int fan3 = 9; // PINO PWM ONDE O FAN (HD) SERA CONECTADO

int fan4 = 6; // PINO PWM ONDE O FAN (RESERVADO) SERA CONECTADO


// ZERA VARIAVEIS DE TODOS OS FANS
int fanSpeed = 0; // (CPU)
int fanSpeed2 = 0; // (CASETOP)
int fanSpeed3 = 0; // (HD)
int fanSpeed4 = 0; // (RESERVADO)

void setup() {

pinMode(fan, OUTPUT); // (CPU)
pinMode(fan2, OUTPUT); // (CASETOP)
pinMode(fan3, OUTPUT); // (HD)
pinMode(fan4, OUTPUT); // (RESERVADO)

pinMode(tempPin, INPUT); // (CPU)
pinMode(tempPin2, INPUT); // (CASETOP
pinMode(tempPin3, INPUT); // (HD)
pinMode(tempPin4, INPUT); // (RESERVADO)

Serial.begin(9600);

//BLOCO DE TESTE DAS PORTAS (TRANSISTORES DE SAIDA) POR 2 SEGUNDOS DEPOIS DE ALIMENTADO

digitalWrite(fan, HIGH);
delay(2000); 
digitalWrite(fan, LOW);

digitalWrite(fan2, HIGH);
delay(2000); 
digitalWrite(fan2, LOW);

digitalWrite(fan3, HIGH);
delay(2000); 
digitalWrite(fan3, LOW);

digitalWrite(fan4, HIGH);
delay(2000); 
digitalWrite(fan4, LOW);


}

void loop() {

temp = analogRead(tempPin);
temp2 = analogRead(tempPin2);
temp3 = analogRead(tempPin3);
temp4 = analogRead(tempPin4);

temp = (temp *5.0*100.0)/1024.0; // CALCULA TEMPERATURA EM GRAUS CELCIUS
temp2 = (temp2 *5.0*100.0)/1024.0; // CALCULA TEMPERATURA EM GRAUS CELCIUS
temp3 = (temp3 *5.0*100.0)/1024.0; // CALCULA TEMPERATURA EM GRAUS CELCIUS
temp4 = (temp4 *5.0*100.0)/1024.0; // CALCULA TEMPERATURA EM GRAUS CELCIUS

Serial.print("CPU ----------------- ");
Serial.println (temp);
Serial.print("");

Serial.print("CASETOP ------------- ");
Serial.println(temp2);
Serial.print("");

Serial.print("HD ------------------ ");
Serial.println(temp3);
Serial.print("");

Serial.print("RESERVADO ----------- ");
Serial.println(temp4);
Serial.print("");

delay(1000); // ATRASO EM MILI SEGUNDOS PRA ESTABILIDADE

if(temp < tempMin) { // SE TEMPERATURA MAIS BAIXA QUE tempMin

fanSpeed = 0; // FAN NAO RODA

digitalWrite(fan, LOW);
}

if(temp2 < tempMin2) { // SE TEMPERATURA MAIS BAIXA QUE tempMin2

fanSpeed2 = 0; // FAN2 NAO RODA

digitalWrite(fan2, LOW);
}

if(temp3 < tempMin3) { // SE TEMPERATURA MAIS BAIXA QUE tempMin3

fanSpeed3 = 0; // FAN3 NAO RODA

digitalWrite(fan3, LOW);
}

if(temp4 < tempMin4) { // SE TEMPERATURA MAIS BAIXA QUE tempMin4

fanSpeed4 = 0; // FAN4 NAO RODA

digitalWrite(fan4, LOW);

}

{
if((temp >= tempMin) && (temp <= tempMax)) // SE A TEMPERATURA É MAIOR QUE A FAIXA
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // VELOCIDADE ATUAL DO FAN
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed

}

{
if((temp2 >= tempMin2) && (temp2 <= tempMax2)) // SE A TEMPERATURA É MAIOR QUE A FAIXA
fanSpeed2 = map(temp2, tempMin2, tempMax2, 32, 255); // VELOCIDADE ATUAL DO FAN
analogWrite(fan2, fanSpeed2); // spin the fan at the fanSpeed speed

}

{
if((temp3 >= tempMin3) && (temp3 <= tempMax3)) // SE A TEMPERATURA É MAIOR QUE A FAIXA
fanSpeed3 = map(temp3, tempMin3, tempMax3, 32, 255); // VELOCIDADE ATUAL DO FAN
analogWrite(fan3, fanSpeed3); // spin the fan at the fanSpeed speed

}

{
if((temp4 >= tempMin4) && (temp4 <= tempMax4)) // SE A TEMPERATURA É MAIOR QUE A FAIXA
fanSpeed4 = map(temp4, tempMin4, tempMax4, 32, 255); // VELOCIDADE ATUAL DO FAN
analogWrite(fan4, fanSpeed4); // spin the fan at the fanSpeed speed

}
}

PWM CONTROLLER 4 FAN.zip (96.2 KB)

Well - get rid of the delay(1000); // ATRASO EM MILI SEGUNDOS PRA ESTABILIDADE in your loop and things will go faster...

Also check the missing {} for the if the temp is in between min and max... that’s probably an issue

Should look like this

if (condition) {
   Instruction1;
   Instruction2;
   Instruction3;
}

if you do

if (condition) 
   Instruction1;
Instruction2;
Instruction3;

then only instruction1 is conditional, Instruction2 and Instruction3 will always get executed (regardless of indentation)

And instead of having an excise in counting,

OR give variables useful names

int tempPin = A3; // PINO ANALOGICO (CPU) PRA USAR O LM35
//if it's the CPU, why on earth don't you just call it
const byte CpuTempPin = A3; // PINO ANALOGICO (CPU) PRA USAR O LM35

(also note the const and the switch to byte because that's already big enough.

OR just work with arrays and reduce the code to 1/4th of it's size :slight_smile:

And depending on the board and the temperature sensors, you can increase the precision/resolution of the readings quite significantly by using internal reference voltage: Arduino Playground - LM35HigherResolution

Danois90:
And depending on the board and the temperature sensors, you can increase the precision/resolution of the readings quite significantly by using internal reference voltage: Arduino Playground - LM35HigherResolution

I solved by modifying the code and adding 100 samples of temperature, it was infinitely more stable !!!

Thanks friends for the help!

Here is the correct code:

Many things can be improved...and there is one bug

The bug is that (on a UNO or similar) an int cannot store the sum of 100x a value that can go up to 1023... use unsigned long to accumulate the readings. Note that You don’t need to read analog entry 100x to be stable - reading twice should be enough unless your temperature varies hyper quickly within a few milliseconds (in which case you should lower the duration of the delay)

Your delay(1000); would fit better at the very end of the loop

pins are best defined as const byte rather than int.

Your PC is fast, there is no need to go slow on Serial, use Serial.begin(115200); instead of 9600 and of course set the serial console at 115200 bauds in the pop up

Give meaningful names to your variable and write appropriate comments

Your code repeats 4 times something similar. Using arrays and a for loop would make your code smaller

=======

Also Please correct your posts above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)