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