thermostat compressor delay

My goal is to add a delay from the last time that the “relay” has been turned on. My current situation is the temp flickers 1 degree and schort cycles the ac compressor. ive tried adding a delay but it causes a delay with all future inputs IE changing the temp. Any on have any advice on how to solve the issue?

#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>                                                        //spi library for 5110
#include <Wire.h>                                                       //I2C library for rtc
#include <Adafruit_GFX.h>                                               //graphix lib for drawing
#include <Adafruit_SH1106.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
#define DHTPIN 7     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                             DEFINES
//----------------------------------------------------------------------------------------------------------------------------------------
#define RELAY A2                                                        //relay A2
#define BUTTON 8                                                        //encoder D8
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                             VARIABLES
//----------------------------------------------------------------------------------------------------------------------------------------
byte tset = 74;                                                         //master temp set
int temperature = 0;                                                    //temp
int select = 1;                                                         //selection variable
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                              SETUP
//----------------------------------------------------------------------------------------------------------------------------------------
void setup()   {

  display.begin(SH1106_SWITCHCAPVCC, 0x3C); // 3.3V power supply
  display.clearDisplay(); // Clear the display and ram
  Serial.begin(9600);                                                   //start serial comms
  Wire.begin();                                                         //start I2C
  display.begin();                                                      //start display
  pinMode(2, INPUT);                                                     //rot enc a
  pinMode(3, INPUT);                                                    //rot enc b
  pinMode(RELAY, OUTPUT);                                                //set relay for output
  display.clearDisplay();                                                // clears the screen and buffer
  dht.begin();
}
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                            MAIN LOOP
//----------------------------------------------------------------------------------------------------------------------------------------
void loop() {

  int f = dht.readTemperature(true);
  temperature = f;

  if (tset > temperature ) {                                                 //if set temp is greater than current temp
    digitalWrite(RELAY, HIGH);                                               //turn on the A/C
  }
  else {                                                                    //if not
    digitalWrite(RELAY, LOW);                                               //turn off the A/C
  }

  display.clearDisplay();                                                   //clear
  display.setTextColor(WHITE);                                              //set color
  display.setCursor(0, 8);
  display.setTextSize(2);
  display.print("Temp  Set");                                               //display headers
  display.drawRoundRect(10, 26, 30, 22, 4, WHITE);                           //draw box for current temp
  display.drawRoundRect(74, 26, 30, 22, 4, WHITE);                          //draw box for set temp
  display.setCursor(14, 30);                                                //put cursor in 1st box
  display.print(temperature);                                               //display current temp in F
  display.setCursor(78, 30);                                                //put cursor in 2nd box
  display.print(tset);                                                      //display set temp in F
  display.display();                                                        //display buffer contents

  int a = digitalRead(2);                                                   //rot enc a
  int b = digitalRead(3);                                                  //rot enc b

  if (a == 0 && b == 1) {                                                   //if clockwise
    tset++;                                                                 //increment temp
    delay(100);
  }
  else if (a == 1 && b == 0) {                                              //if counterclockwise
    tset--;                                                                 //decrement temp
    delay(100);
  }
  else {                                                                    //if no movement
    tset = tset;                                                            //do nothing

  }
}

Note the time when the relay is set. Before setiing the relay again, check that enough time has passed.

First of all you need some hysteresis (dead band) between the cut in point and the cut out point (2 degrees F is about right), but looks like your programming is backward, you are turning the relay ON when temperature is BELOW setpoint. Is the AC connected to the NC relay contact?
Please post a wiring diagram.

Im using the standard “arduino” relays with the opto isolator, it works correctly. “hysteresis” pointed me in the perfect direction! Thank you very much! ive attached my updated code. I used a dead band of 4 degrees because this will be controlling a roof top ac on a school bus rv conversion. Its poorly insulated so even with that it may still short cycle the compressor.

#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>                                                        //spi library for 5110
#include <Wire.h>                                                       //I2C library for rtc
#include <Adafruit_GFX.h>                                               //graphix lib for drawing
#include <Adafruit_SH1106.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
#define DHTPIN 7     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                             DEFINES
//----------------------------------------------------------------------------------------------------------------------------------------
#define RELAY A2                                                        //relay A2
#define BUTTON 8                                                        //encoder D8
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                             VARIABLES
//----------------------------------------------------------------------------------------------------------------------------------------
byte tset = 74;                                                         //master temp set
int temperature = 0;                                                    //temp
int select = 1;                                                         //selection variable
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                              SETUP
//----------------------------------------------------------------------------------------------------------------------------------------
void setup()   {

  display.begin(SH1106_SWITCHCAPVCC, 0x3C); // 3.3V power supply
  display.clearDisplay(); // Clear the display and ram
  Serial.begin(9600);                                                   //start serial comms
  Wire.begin();                                                         //start I2C
  display.begin();                                                      //start display
  pinMode(2, INPUT);                                                     //rot enc a
  pinMode(3, INPUT);                                                    //rot enc b
  pinMode(RELAY, OUTPUT);                                                //set relay for output
  display.clearDisplay();                                                // clears the screen and buffer
  dht.begin();
}
//----------------------------------------------------------------------------------------------------------------------------------------
//                                                            MAIN LOOP
//----------------------------------------------------------------------------------------------------------------------------------------
void loop() {

  int f = dht.readTemperature(true);
  temperature = f;

  if (tset > temperature +2 ) {                                                 //if set temp is greater than current temp
    digitalWrite(RELAY, HIGH);                                               //turn on the A/C
  }
  else if (tset < temperature -2 ) {                                                                  //if not
    digitalWrite(RELAY, LOW);                                               //turn off the A/C
  }

  display.clearDisplay();                                                   //clear
  display.setTextColor(WHITE);                                              //set color
  display.setCursor(0, 8);
  display.setTextSize(2);
  display.print("Temp  Set");                                               //display headers
  display.drawRoundRect(10, 26, 30, 22, 4, WHITE);                           //draw box for current temp
  display.drawRoundRect(74, 26, 30, 22, 4, WHITE);                          //draw box for set temp
  display.setCursor(14, 30);                                                //put cursor in 1st box
  display.print(temperature);                                               //display current temp in F
  display.setCursor(78, 30);                                                //put cursor in 2nd box
  display.print(tset);                                                      //display set temp in F
  display.display();                                                        //display buffer contents

  int a = digitalRead(2);                                                   //rot enc a
  int b = digitalRead(3);                                                  //rot enc b

  if (a == 0 && b == 1) {                                                   //if clockwise
    tset++;                                                                 //increment temp
    delay(100);
  }
  else if (a == 1 && b == 0) {                                              //if counterclockwise
    tset--;                                                                 //decrement temp
    delay(100);
  }
  else {                                                                    //if no movement
    tset = tset;                                                            //do nothing

  }
}

You still need a delay after turning OFF and before restarting so the compressor does not try to start against head pressure, 5 minutes is normal in residential units.

JCA34F:
You still need a delay after turning OFF and before restarting so the compressor does not try to start against head pressure, 5 minutes is normal in residential units.

Can you point me in the right direction on how to do this without stopping all the other code? Im still very green as you've probably already noticed.

Read #1.

Railroader:
Read #1.

I did but I dont understand how to work that into my code. I tried to mesh another delay code into mine and failed, it delayed but then started clicking the relay, so i pulled it back out and came here to ask for help because im clueless.

When the logic calls for turning on the relay fitst check if enough time has passed since last turning on. If so, turn on the relsy and register the time. Use millis() as the clock. Read Arduino reference for details. It's an excellent handbook.

The way I would do it is:
When relay goes from ON to OFF,
Start a 5 minute millis() timer and set a boolean flag to false, when timer has expired, set flag to true.
When the AC needs to restart, check to see if the timer has expired and the flag is set back to true:

if(tset > temperature and flag == true)
{
   digitalWrite(RELAY, HIGH);    
}

Something like that, I’m about as green as you.

Are you sure your thermostat is causing the compressor short-cycles? On my heat pump/AC, the compressor controller manages that part of the system for safety, not the thermostat.

Paul

@Paul_KD7HB:
The OPs AC is in a vehicle (12VDC ?), I asked for a diagram, but...

Paul_KD7HB:
Are you sure your thermostat is causing the compressor short-cycles? On my heat pump/AC, the compressor controller manages that part of the system for safety, not the thermostat.

Paul

Yes at the moment I have the ductles vent cover on my ac units, once it gets to temp it shuts the compressor down and since the insulation in a stock school bus is crap the temp shoots up quickly before the refridgerant can equalize and it tries to start the compresor and trips the breaker. its good in cooler weather or in the shade. also the temp sensing is currently in the return air intake and it sucks cold air in while running and shuts down early due to thinking the area is cooler than it is… poor design from the factory but ill have it dialed in soon enough.

JCA34F:
@Paul_KD7HB:
The OPs AC is in a vehicle (12VDC ?), I asked for a diagram, but...

I dont have a diagram, im removing the factory T-stat that is a thermocouple type and putting my relay in place. its a line voltage T-stat for the compresor all other wiring ill remain. Yes its in a full size school bus i have a pur sine inverter as well as a diesel generator powering them.

Thanks for everyones input, im not smart enough to figure this one out so ill be ordering a time delay relay to solve the problem.