Turning on a far HELP!!!

So I’m new to coding and its probably slapping me in the face. But I’m looking to put some kind of delay in.
I have a fan on the ceiling in front of my furnace and I want that fan to turn on when it hits 24 C and stay on for a min then turn off. Every thing is wired up and working great but I find it hits a point where its not quite 24 and it turns itself on and off about 10 in with in a min so if I delay that it wont trip the relay so often.

/*

// ****** Start of TM1637 Display code
#include <Arduino.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000

// ****** end of TM1637 Display code

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// updated by Ahmad Nejrabi for Robojax.com videos.
// on Jan 10, 2018 in Ajax, Ontario, Canada

// ****** Start of DHT code
#include “DHT.h”
#define DHTPIN 9 // what digital pin we’re connected to
// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// ********** end of DHT22 code

#define RELAY 7 // the pin connected to relay
void setup()
{
Serial.begin(9600);
Serial.println(“DHT22 Robojax Test with Display”);
pinMode(RELAY,OUTPUT);// set RELAY pin as output
dht.begin();
}

void loop()
{

// Robojax.com test video
Serial.println(getTemp(“c”));

int temp = round(getTemp(“c”));

//display.showNumberDec(temp, false, 3,1);
if(temp >24 ) // temp of the garage
{
digitalWrite(RELAY, HIGH);

}else{
digitalWrite(RELAY, LOW);

}

}// loop end

/*

  • getTemp(String req)
  • returns the temprature related parameters
  • req is string request
  • getTemp(“c”) will return temprature in Celsius
  • getTemp(“hic”) will return heat index in Celsius
  • getTemp(“f”) will return temprature in Fahrenheit
  • getTemp(“hif”) will return temprature in Fahrenheit
  • getTemp(“h”) will return humidity
    */
    float getTemp(String req)
    {

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

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

// 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 Kelvin
float k = t + 273.15;
if(req ==“c”){
return t;//return Cilsus
}else if(req ==“f”){
return f;// return Fahrenheit
}else if(req ==“h”){
return h;// return humidity
}else if(req ==“hif”){
return hif;// return heat index in Fahrenheit
}else if(req ==“hic”){
return hic;// return heat index in Cilsus
}else if(req ==“k”){
return k;// return temprature in Kelvin
}else{
return 0.000;// if no reqest found, retun 0.000
}

}

You need to build some hysteresis into this code so, for example, it switches on at 24 but does not switch off until the temp falls to 22 or 23
Instead of this

 if(temp >24 ) // temp of the garage
 {
  digitalWrite(RELAY, HIGH);
   
 }else{
  digitalWrite(RELAY, LOW);
 }

try this

 if(temp >24 ) // temp of the garage
 {
  digitalWrite(RELAY, HIGH);
 }

 if(temp < 22 ) // temp of the garage
 {
  digitalWrite(RELAY, LOW);
  }

...R

PS ... To make it easy for people to help you use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Wow that really did slap me in the face. Thank you very much

ok so a new problem came up. when its plugged into the computer it works flawless but when I field test it my relay does not come on even with a heat gun blasting on it. The only difference is the I go in serial monitor on the computer and I don't have any displays. all my connections are perfect and its glued to a container so nothing can move. any ideas?

When it's not plugged what is powering both the Arduino and the relays? How exactly is everything connected?

Steve

its power supply is a 5v 2A

Image from Reply #5 so we don't have to download it. See this Simple Image Posting Guide

...R

The power supply is not shown in your diagram.

Minion09:
Wow that really did slap me in the face.

That seems a very strange comment. I made no attempt to slap any person's face.

...R

Robin2:
The power supply is not shown in your diagram.
That seems a very strange comment. I made no attempt to slap any person's face.

...R

It was a figure of speech, Because all I missed was one line of code.

Robin2:
Image from Reply #5 so we don't have to download it. See this Simple Image Posting Guide

...R

I have it plugged in through the standard Arduino supply port. I switched it to a 9 volt supply now and the relays kick on and off just not at the right temperatures. So not it turns on at 29 c (when its set to 24) and turns off at 20 c (when its set to 22)

If you have a 5V supply it should be connected to the Arduino's 5V pin and the relays. Just like your diagram seems to show.

To make sense of "the standard Arduino supply port" we need to know what Arduino you are using. If you mean the barrel connector on Uno then it needs 7V minimum and can supply very little current via the 5V pin if that's where your relay is connected.

Steve

Minion09:
I have it plugged in through the standard Arduino supply port.

See Reply #9 for why it is important to show the power supply in the wiring diagram.

So not it turns on at 29 c (when its set to 24) and turns off at 20 c (when its set to 22)

What are you using to measure the temperature independently of the Arduino?

…R

im using Digital Laser Infrared Thermometer

its just not making any sense to me. inside it works flawless out side in my garage its working now just at the wrong temps.

Minion09:
its just not making any sense to me. inside it works flawless out side in my garage its working now just at the wrong temps.

If it was mine I think I would just fudge the numbers to get it to operate at the temperatures I require.

One thought is that the InfraRed thermometer may react more quickly than the thermocouple or may be measuring the temperature at a slightly different location.

…R