Temperature system

Hi everyone, I'm creating a temperature system it works like this: between 23 and 40 °c a green led is turn on when the temperature is above 40 the green led turns off and a fan and a buzzer turn on, the buzzer will sound until the temperature is under 40 but the fan won't turn off until the temperature goes iqual to 23 °c at that moment the fan will turn off and the green led will turn on again, the issue is that when the temperature goes under 40 the fan does not keep turn on, can you tell me what's missing in this code?

#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);  // crea objeto y asigna pines a los cuales se
          // encuentran conectados RS, E, D4, D5, D6, D7
int SENSOR;   // variable almacena valor leido de entrada analogica A0
float TEMPERATURA;  // valor de temperatura en grados centigrados
float SUMA;   // valor de la suma de las 5 lecturas de temperatura
int verde=2;
int rojo=3;
int buzzer=4;
void setup() {
  lcd.begin(16, 2); // inicializa lcd en 16 columnas por 2 filas
  // entradas analógicas no requieren inicialización
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
}

void loop() {
   SUMA = 0;         // valor inicial de SUMA en cero
  for (int i=0; i < 5; i++){      // bucle que repite 5 veces
  SENSOR = analogRead(A0);      // lectura de entrada analogica A0
  TEMPERATURA = ((SENSOR * 5000.0) / 1023) / 10;// lectura de entrada analogica A0
            // de entrada A0 en grados centigrados
  SUMA = TEMPERATURA + SUMA;      // suma de cada lectura de temperatura
  delay(500);         // demora de medio seg. entre lecturas
  }
  lcd.setCursor(0, 0);        // cursor en primer fila y primer columna
  lcd.print("ITM Temp: ");        // escribe Temp:
  lcd.print(SUMA/5.0, 1);     // escribe valor promedio de 5 lecturas con
            // un decimal
  lcd.print(" C");        // imprime C
  if (SUMA/5 >= 23.0 && SUMA/5 < 40.0) {
    digitalWrite(verde, HIGH);
    digitalWrite(buzzer, LOW);
    digitalWrite(rojo, LOW);
  } else if (SUMA/5 >= 40) {
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    delay(300);
    digitalWrite(4,LOW);
    delay(3000);
    if(SUMA/5 <= 23.0){
      digitalWrite(2,HIGH);
      digitalWrite(3,LOW); 
    }
  } 
  
}

Absolutely, I can. There is no variable to remember whether the temperature has been exceeded. Without any past history, it is impossible to know whether the fan should remain on.

Is this an assignment?

This s a good opportunity to learn and apply the concepts of a Finite State Machine (FSM).

You can read about that in this forum and elsewhere on the Internet.

Give it a try and have fun!

I've been trying using only conditionals I didn't have any idea of a extra variable, what's the name of that topic, could you give an example?

The name is "state machine".

The way you specified it, it can be in one of two states:
EXCEEDED
IDLE

which in this case can be condensed to one variable:

bool exceeded = false;

To set it:
exceeded = true;

To clear it to IDLE
exceeded = false;

To test it
if (exceeded) {...
else {...

I'll give it a try thank you

I never overlook an opportunity to google

 hysteresis in C++

I found

which seems like it would be worth a few minutes.

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.