How to program pin high until ??

Hi everyone, I am making a simple temperature controler with temp sensor and relay. I have used some code and modified it to show Fahrenheit and turn on when temp is above 75 degrees, but I want it to stay on until it reaches like say 72 degrees so my fan is not being turned on and off so much. I am running a large fan and I am worried it will burn out the brushes on the fan if it is constantly cycling. Anyone know what I can do to make this work, I tried putting in a delay after the digital write but it just slows down the whole loop and not just after the if statement. here is my code

//initializes/defines the output pin of the LM35 temperature sensor
int outputpin= A0;
int relaypin= 13;

//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);

}

//main loop
void loop()
{
int rawvoltage= analogRead(outputpin);
float millivolts= (rawvoltage/1024.0) * 5000;
float celsius= millivolts/10;
float fahrenheit= ((celsius * 9)/5 + 32);
if (fahrenheit > 75)
{
digitalWrite(13, HIGH);
Serial.println("Fan on");
delay(10000);
}


else {
digitalWrite(13, LOW);
Serial.print("Fan off");
}
Serial.print(fahrenheit);
Serial.println(" degrees Fahrenheit");
delay(1000);

}

else if (fahrenheit < 72)
{
digitalWrite(13, LOW);
}

thanks Larry, I am testing that now. I actually posted the wrong code, and I didnt notice it tell I was reading your reply. I have another issue, it seems as it wont show any temp above 75 degrees once relay is tripped. I think it doesnt loop until it shuts off relay. So it stays stuck on 75 even if the temp is higher if relay is off. Any ideas how I can read actual temp while relay is on? Here is the code I am using;

#include <LiquidCrystal.h>
int reading = 0;
int sensorPin = A0;
int relay = 13;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of columns and rows: 
lcd.begin(16, 2);
pinMode(relay,OUTPUT);
}
void loop() {
reading = analogRead(sensorPin);
int celsius = reading/2;
       int fahrenheit= ((celsius * 9)/5 + 32);
lcd.setCursor(0, 0);
lcd.print("Temperature: "); 
lcd.setCursor(0,1);
lcd.print(fahrenheit, DEC);
lcd.print((char)223);
lcd.print("F");
if (fahrenheit <75) {
digitalWrite(13,HIGH);
               
} 
       else if (fahrenheit >69)
         { 
       digitalWrite(13,LOW);
}
       delay(500);
       lcd.clear();
}

Put some Serial prints back in and do some debugging to your serial monitor.
Maybe Serial.print reading and celsius and fahrenheit as these variables are handled.
For debugging, remove the relay replace it with a LED and series resistor.

Is the relay picked when the output is high or low?

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" button above the posting area (It looks like a scroll with < > inside it).

To stop the fan turning on and off rapidly, all you need is two set points.

For example:

  • Turn on if the temperature is above 75 degrees
  • Turn off if the temperature is below 70 degrees
 if (fahrenheit <75) {

digitalWrite(13,HIGH);

}
  else if (fahrenheit >69)
  {
    digitalWrite(13,LOW);
  }

This seems to be backwards unless setting the pin HIGH turns the fan off. Assuming it does, you have not done what I suggested. There should be a "blank spot" in the middle where the fan is left doing what it was last time.

eg.

if (fahrenheit >=75) 
  {
  digitalWrite(13, LOW);  // too hot, turn fan on
  }
else if (fahrenheit <= 70)
 {
 digitalWrite(13, HIGH);  // too cool, turn fan off
 }
// otherwise leave fan doing whatever it was before

This is called hysteresis.

Basically you want the behaviour at a certain time to depend on past events.

In this case, at temperature 72 (say) the fan could be either on or off, depending on whether it was hot earlier and it is cooling the down, or if it was cold before, and the fan is off, as it wasn't hot enough to turn it on.

you can look to update the display without the delay

you can also just change the evaluation for the pin output in the case where the temperature has changed

untested code:

#include <LiquidCrystal.h>
int lastReading = 0;
int sensorPin = A0;
int relay = 13;
unsigned long lastUpdateTime;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  pinMode(relay,OUTPUT);
  lcd.clear();
}
void loop() 
{
  
  int reading = analogRead(sensorPin);
  int celsius = reading/2;
  int fahrenheit = ((celsius * 9)/5 + 32);
  if (reading != lastReading)  //just trigger an evaluation if temperature has changed
  {
    if (fahrenheit < 75) 
    {
      digitalWrite(13, HIGH);
    } 
    else if (fahrenheit > 69)
    { 
      digitalWrite(13,LOW);
    }
  }
  lastReading = reading;
  
  if (millis() - lastUpdateTime >= 500UL)  //update the display every half second
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temperature: "); 
    lcd.setCursor(0,1);
    lcd.print(fahrenheit, DEC);
    lcd.print((char)223);
    lcd.print("F");
    lastUpdateTime = millis();
  }
}

thanks Nick, I edited my posts. I agree it does seem backwards, but it was the only way to get the relay to turn on with the code. Your code is confusing me a bit, shouldnt it be write High to turn fan on? As power from from pin 13 turns on relay which runs fan that is a cooling fan?? When I read the code it does seem like it should be fahrenheit >=75 but that doesnt turn on the relay at 75?? it seems its almost like a double negative so to speak,, the way you changed the <> and HIGH to LOW. ???

I see your point about the Hysteresis, but how do you put this into code? I am very new to programming and can barely edit others code. Can you give me an example of how to implement Hysteresis?

shouldnt it be write High to turn fan on?

It does depend if the relay is driven by an inverter or the contact you are using NO or NC.

I put the comment there to make it obvious which way around to put the HIGH/LOW.

Often relays are driven by a MOSFET wired as a low-side driver, so that sending a LOW turns it on, not off. (It sinks current rather than sources it).

In your earlier post you had:

  else if (fahrenheit >69)
         { 
       digitalWrite(13,LOW);

So I assumed that setting the pin LOW turned on the fan (I assume it is a cooling fan).

I have used some code and modified it to show Fahrenheit and turn on when temp is above 75 degrees,

followed by:

if (fahrenheit <75) {
digitalWrite(13,HIGH);

I'm not sure what your logic is here, but you are doing something when the temperature is below 75 degrees, so I assumed it was turning the fan off.

I think I am making this too complicated. I see your point, and I agree, code reads if fahrenheit is less than 75. but it doesnt turn on outlet/fan that way. So it must be in the wiring of the relay. I am using sainsmarts relay and it lights up when power hits it from the 13 pin with closes the circuit so it is normally open. So my

if (fahrenheit <75) {
digitalWrite(13,HIGH);// this turns on fan with the current wiring

I see why it is useful to use comments in the code. I think if I was to do as you suggested and write it like

if (fahrenheit >=75) 
  {
  digitalWrite(13, LOW);  // too hot, turn fan on
  }
else if (fahrenheit <= 70)
 {
 digitalWrite(13, HIGH);  // too cool, turn fan off
 }
// otherwise leave fan doing whatever it was before

I think because you change the less than to greater than and the HIGH to LOW, it should work and it will make more sense when looking at code. I will test this and see in the morning as I dont have the project with my at this time. As for the Hysteresis how would I write this into this code??

I don't understand what you are saying. If the temperature is too high, you want to turn the fan on, right?

Computers are logical, they don't re-interpret "if the temperature is below 75" as being "if the temperature is above 75". You need to work your way through this part first.

Yes you are correct I want fan on when temp is too high ie. 75 degrees. Which I have working now, I just want it to know like you said if it was on because it was too high in which I want it to stay on tell it gets below 70 degrees.