Thermostat-Code problem



  • Hi, so im trying to make a a project with the arduino, where i have this:
    -Potenciometer
    -temperature sensor
    -Relay

    So the goal is to read the temperature of something that is heating and turn that off at a certain value that is regulated with the potenciometer.This Is simple , but i want to do something like turn off at 100ºC and only turn that on again at 95ºC. How can you help me? This is the code:

    // Relé ligado ao pino 7

    int porta_rele1 = 7;
    //Relé ligado ao pino 8
    int porta_rele2 = 8;

    float celsius;
    const int porta_sensor1 = A0; // sonda 1 ligada ao pino AO
    const int porta_sensor2 = A2; // sonda 2 ligada ao pino A1
    const int porta_pot1 = A1; // potenciometro 1 ligada ao pino AO
    const int porta_pot2 = A3; // potenciometro 2 ligada ao pino AO

    void setup()
    {
    //Define pinos para o rele como saida
    pinMode(porta_rele1, OUTPUT);
    pinMode(porta_rele2, OUTPUT);
    //Define pinos dos sensores como entrada
    pinMode(porta_pot1, INPUT);
    pinMode(porta_pot2, INPUT);
    pinMode(porta_sensor1, INPUT);
    pinMode(porta_sensor2, INPUT);
    //Estado inicial dos reles - desligados
    digitalWrite(porta_rele1, LOW);
    digitalWrite(porta_rele2, LOW);

    analogReference(INTERNAL);
    Serial.begin(9600);

    Serial.begin(9600);
    pinMode(porta_pot1, INPUT);
    pinMode(porta_sensor1, INPUT);
    pinMode(porta_pot2, OUTPUT);
    pinMode(porta_sensor2, OUTPUT);
    }

    void loop()
    {
    int value1 = analogRead(porta_sensor1); //valor lido na sonda 1
    long celsius1 = value1/9.31;
    int value2 = analogRead(porta_sensor1); //valor lido na sonda 2
    long celsius2 = value2/9.31;
    Serial.print(celsius1);
    Serial.print(celsius2);

    int valueA = analogRead(porta_pot1);
    int valueB = analogRead(porta_pot2);

    long celsiusA = valueA/9.31; // 10 mV por graus C.
    Serial.print(celsiusA);
    long celsiusB = valueB/9.31; // 10 mV por graus C.
    Serial.print(celsiusB);

    if(celsius1 <= celsiusA)
    {

    digitalWrite(porta_sensor1, HIGH); // aquecedor no pino 9

    }
    else
    {

    digitalWrite(porta_sensor1, LOW); // aquecedor no pino 9

    }
    delay(1000);
    }


  • Mod

    Big welcome to the MySensors community @Bruno-Cunha !

    I don't understand what valueA and valueB in your sketch is, but I would just add

    #define HYSTERESIS 5 // 5 Degrees hysteresis
    bool heating = false;
    

    at start of the sketch
    and change

    if(celsius1 <= celsiusA)
    {
    digitalWrite(porta_sensor1, HIGH); // aquecedor no pino 9
    }
    else
    {
    digitalWrite(porta_sensor1, LOW); // aquecedor no pino 9
    }
    

    to

    if(heating)
      if (celsius1 > celsiusA)
      {
        digitalWrite(porta_sensor1, LOW);
        heating = false;
      }
    }
    else
      {
      if (celsius1 <= celsiusA - HYSTERESIS)
      {
        digitalWrite(porta_sensor1, HIGH);
        heating = true;
      }
    }
    

    Edit: Sorry. I messed up. The original code was wrong but I think the updated post should be ok.



  • celsius1 is the value read by the sensor anda cesliusA is the value regulated by the potenciometer. One question, what is the purpose of the "#define HYSTERESIS 5"?


  • Mod

    @Bruno-Cunha it sets the hysteresis to 5 degrees


  • Mod

    I figured out a simpler way. There is no need for the heating variable.

      if (celsius1 > celsiusA)
      {
        digitalWrite(porta_sensor1, LOW);
      }
      if (celsius1 <= celsiusA - HYSTERESIS)
      {
        digitalWrite(porta_sensor1, HIGH);
      }
    


  • Yes it worked, i tried something similar before but i did "celsiusA -5" i didnt know i need a variable.I think its solved, thank you very much :)))


  • Mod

    @Bruno-Cunha it is not a variable, and using - 5 would be the same, but using 5 makes it harder to understand and maintain the code. See https://refactoring.com/catalog/replaceMagicNumberWithSymbolicConstant.html for more info on why I prefer using define.


Log in to reply
 

Suggested Topics

  • 8
  • 7
  • 1
  • 3
  • 1
  • 2

10
Online

11.4k
Users

11.1k
Topics

112.7k
Posts