Help with servo thermostat

Hi all,
I'm trying to get a servo to press the start button on an AC remote when temperature goes too high and to press again when temperature is under the low limit. I also created a variable that register if the AC is already running (if the button was press before) so it doesn't activate the servo again when tempMin is reach without the AC ON.

My code compile fine but somehow does not activate the servo when the tempMax is reach.
Here is the code:

#include "DHT.h"
#include <Servo.h>
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

Servo myservo;
DHT dht(DHTPIN, DHTTYPE);

int tempMin = 25.5; 

int tempMax = 27;

void setup() {
  Serial.begin(9600);
  dht.begin();
  myservo.attach(9);
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  
  int r=0;//variable to know if the AC is running

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  Serial.print("Operation Mode: ");
  Serial.print(r);
  Serial.print("\n");

if ((t > tempMax) && (r=0)) {
   myservo.write(10);
  delay(600);
  myservo.write(150);
  r=1;
  Serial.print("AC starting\t");
  Serial.print(r);
  }
  
if ((t < tempMin) && (r=1)) {
   myservo.write(10);
  delay(600);
  myservo.write(150);
  r=0;
  Serial.print("AC stoping\t");
  Serial.print(r);
  }

}

Any idea what is wrong?

Thank you

I think this is the problem

  int r=0;//variable to know if the AC is running

Try changing it to

  static int r=0;//variable to know if the AC is running

The way you have it the value starts at 0 every time loop() repeats. Using the word "static" makes it remember the value from the previous iteration.

As a general comment do NOT use single character variable names. When I try to find all the occurrences of the variable called 'r' every 'r' in the program gets highlighted.

...R

Thanks Robin,
static in makes much more sense.
Did not work at first but I figured that I had other errors like making sure in my condition that the status is check correctly with ==

Thanks also for the advice on not having single character variable, much easier for code review. Here is my final code and seems to work just fine in case someone else want inspiration from it:

#include "DHT.h"
#include <Servo.h>
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

Servo myservo;
DHT dht(DHTPIN, DHTTYPE);

int tempMin = 26; 

int tempMax = 27;
static int op = 0;//variable to know if the AC is running

void setup() {
  Serial.begin(9600);
  dht.begin();
  myservo.attach(9);
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float hum = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float temp = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float far = dht.readTemperature(true);
  

  // Check if any reads failed and exit early (to try again).
  if (isnan(hum) || isnan(temp) || isnan(far)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(far, hum);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(temp, hum, false);

  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println(" *C ");
  Serial.print("Operation Mode: ");
  Serial.print(op);
  Serial.print("\n");

if ((temp > tempMax) && (op == 0)) {
   myservo.write(10);
  delay(600);
  myservo.write(150);
  op = 1;
  Serial.print("AC starting\t");
  Serial.print(op);
  }
  
if ((temp < tempMin) && (op == 1)) {
   myservo.write(10);
  delay(600);
  myservo.write(150);
  op = 0;
  Serial.print("AC stoping\t");
  Serial.print(op);
  }
}

Hi Yopo,

Well done on getting this code working and running!

This is EXACTLY what i have been trying to do, with not much success, for the past two months.

I shall give your code a try and post back if i have any success

Best Regards,

G.

I tried the code out and i couldnt get it to verify. It kept stopping at the part that deals with Heat Index. I simply removed this section of code as all i need is temperature in Degrees C. It verified after i did this and uploaded fine. The code works perfectly Yopo.

I have added another servo into the code. In my case this opens and closes a valve in a 4" tube.

Again, many thanks for sharing this Yopo, it was exactly what i have been looking for.

G.