user30569: I am working on this thermostat, which has a predefined int that sets pin high and low. But now I want to give it a particular range on which the pin 7 stays high, let's say from 24°C to 26°C, because I don't want my relay clicking every second.
const int sensor=A1; // Assigning analog pin A1 to variable 'sensor'
int relay=7;
float tempc;  //variable to store temperature in degree Celsius
float vout;  //temporary variable to hold sensor reading

void setup() {
    pinMode(sensor,INPUT); // Configuring pin A1 as input
    Serial.begin(9600);    
}

void loop() {
    vout=analogRead(sensor);
    vout=(vout*500)/1023;
    tempc=vout; // Storing value in Degree Celsius 
    vout=analogRead(sensor);
    vout=(vout*500)/1023;
    tempc=vout; // Storing value in Degree Celsius 
    Serial.print("temprature in C is ");
    Serial.print(tempc);
    Serial.println();
    delay(2000);
    if (tempc <24) {
      digitalWrite(7,HIGH);
      Serial.print("heating is on as " );
    } else {
      digitalWrite(7,LOW);
      Serial.print("heating is off as ");
    }


user7508: Your question is not very clear, but I assume, like Mark Smith, that you
want your thermostat to be
hysteretic between 24°C and
26°C. The algorithm is as follows:

If the heater is OFF and you get a temperature reading below 24°C,
then you switch it ON.
If the heater is ON and you get a reading above 26°C, then you switch
it OFF.
In any other case, you leave the heater alone: it will stay in
whatever state you have previously set it.

Translated to C++:
// Pinout.
const uint8_t sensor = A1;  // temperature sensor
const uint8_t heater = 7;   // heater connected through a relay

void setup()
{
    pinMode(sensor, INPUT);
    pinMode(heater, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    static bool heaterOn;
    float temperature = 500.0 / 1024 * analogRead(sensor);

    // Switch ON if the temperature drops below 24 Celsius.
    if (!heaterOn && temperature < 24) {
        Serial.println(F("Switching the heater ON."));
        digitalWrite(heater, HIGH);
        heaterOn = true;
    }

    // Switch OFF if the temperature rises above 26 Celsius.
    else if (heaterOn && temperature > 26) {
        Serial.println(F("Switching the heater OFF."));
        digitalWrite(heater, LOW);
        heaterOn = false;
    }
}


30569: <p>I am working on this thermostat, which has a predefined int that sets pin high and low. But now I want to give it a particular range on which the pin 7 stays high, let's say from 24°C to 26°C, because I don't want my relay clicking every second.</p>

<pre><code>const int sensor=A1; // Assigning analog pin A1 to variable 'sensor'
int relay=7;
float tempc;  //variable to store temperature in degree Celsius
float vout;  //temporary variable to hold sensor reading

void setup() {
    pinMode(sensor,INPUT); // Configuring pin A1 as input
    Serial.begin(9600);    
}

void loop() {
    vout=analogRead(sensor);
    vout=(vout*500)/1023;
    tempc=vout; // Storing value in Degree Celsius 
    vout=analogRead(sensor);
    vout=(vout*500)/1023;
    tempc=vout; // Storing value in Degree Celsius 
    Serial.print("temprature in C is ");
    Serial.print(tempc);
    Serial.println();
    delay(2000);
    if (tempc &lt;24) {
      digitalWrite(7,HIGH);
      Serial.print("heating is on as " );
    } else {
      digitalWrite(7,LOW);
      Serial.print("heating is off as ");
    }
</code></pre>
 7508: <p>Your question is not very clear, but I assume, like Mark Smith, that you
want your thermostat to be
<a href="https://en.wikipedia.org/wiki/Hysteresis" rel="nofollow noreferrer">hysteretic</a> between 24°C and
26°C. The algorithm is as follows:</p>

<ul>
<li><p>If the heater is OFF and you get a temperature reading below 24°C,
then you switch it ON.</p></li>
<li><p>If the heater is ON and you get a reading above 26°C, then you switch
it OFF.</p></li>
<li><p>In any other case, you leave the heater alone: it will stay in
whatever state you have previously set it.</p></li>
</ul>

<p>Translated to C++:</p>

<pre><code>// Pinout.
const uint8_t sensor = A1;  // temperature sensor
const uint8_t heater = 7;   // heater connected through a relay

void setup()
{
    pinMode(sensor, INPUT);
    pinMode(heater, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    static bool heaterOn;
    float temperature = 500.0 / 1024 * analogRead(sensor);

    // Switch ON if the temperature drops below 24 Celsius.
    if (!heaterOn &amp;&amp; temperature &lt; 24) {
        Serial.println(F("Switching the heater ON."));
        digitalWrite(heater, HIGH);
        heaterOn = true;
    }

    // Switch OFF if the temperature rises above 26 Celsius.
    else if (heaterOn &amp;&amp; temperature &gt; 26) {
        Serial.println(F("Switching the heater OFF."));
        digitalWrite(heater, LOW);
        heaterOn = false;
    }
}
</code></pre>
