Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Thermostat-Code problem

Thermostat-Code problem

Scheduled Pinned Locked Moved My Project
7 Posts 2 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Bruno CunhaB Offline
    Bruno CunhaB Offline
    Bruno Cunha
    wrote on last edited by
    #1

    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);
    }

    mfalkviddM 1 Reply Last reply
    0
    • Bruno CunhaB Bruno Cunha

      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);
      }

      mfalkviddM Online
      mfalkviddM Online
      mfalkvidd
      Mod
      wrote on last edited by mfalkvidd
      #2

      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.

      1 Reply Last reply
      1
      • Bruno CunhaB Offline
        Bruno CunhaB Offline
        Bruno Cunha
        wrote on last edited by
        #3

        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"?

        mfalkviddM 1 Reply Last reply
        0
        • Bruno CunhaB Bruno Cunha

          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"?

          mfalkviddM Online
          mfalkviddM Online
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          @Bruno-Cunha it sets the hysteresis to 5 degrees

          1 Reply Last reply
          0
          • mfalkviddM Online
            mfalkviddM Online
            mfalkvidd
            Mod
            wrote on last edited by
            #5

            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);
              }
            
            1 Reply Last reply
            0
            • Bruno CunhaB Offline
              Bruno CunhaB Offline
              Bruno Cunha
              wrote on last edited by
              #6

              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 :)))

              mfalkviddM 1 Reply Last reply
              0
              • Bruno CunhaB Bruno Cunha

                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 :)))

                mfalkviddM Online
                mfalkviddM Online
                mfalkvidd
                Mod
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                1
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                21

                Online

                11.7k

                Users

                11.2k

                Topics

                113.1k

                Posts


                Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • MySensors
                • OpenHardware.io
                • Categories
                • Recent
                • Tags
                • Popular