Help with tempreture control code

Hi,

I'm a beginner so I'm sure this is probably an easy fix to my problem but I can't figure out how to write the code to keep a pin on after it has dropped below the initial value that set it HIGH.

Here is my code:

/**********************************************/
const int relayPin = 2; //The S pin of the relay
float tem = 0; // Celsius temp variable
float far = 0; // Fahrenheit temp variable
long lmVal = 0; // Value of AnalogPin A0

/**********************************************/
void setup()
{
pinMode(relayPin, OUTPUT); //initialize relay as an output
Serial.begin(9600); // start serial port at 9600 bps:
}

/***********************************************/
void loop()
{
lmVal = analogRead(A0);
tem = (lmVal * 0.0048828125 * 100); //5/1024=0.0048828125;1000/10=100
far = (tem*9/5+32); //formula to convert Celsius to Fahrenheit
Serial.print(" Fahrenheit: ");
Serial.println(far);
if (far > 77){
digitalWrite(relayPin, HIGH); //Close the relay
}
// Somewhere about here I want relayPin (2) to stay high until far drops below 72
// basically leaving relayPin on until the temperature drops 5 degrees below turn on point

else {
digitalWrite(relayPin, LOW); //disconnect the relay
}

delay(1000);
Serial.print(" Celsius: ");
Serial.println(tem);
}

/*************************************************/

This is a temperature control project. I want to turn on a relay controlled fan at 77 deg. and leave it on until it gets to 72 deg. I'm asking for help with some code to accomplish this.
I am using the serial mode only to test and see what's happening.

Maybe something like this is already in the forum but I'm not sure what to search for.
Thank you very much!

Welcome to the Forum. Please read the two posts

How to use this forum - please read.
and
Read this before posting a programming question ...

at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online here.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. So before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like digitalWrite(3,0). But such a statement doesn't reveal anything about the purpose. digitalWrite(hornRelayPin, LOW) does. You can do that by declaring const byte hornRelayPin = 3; before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see "daysPerYear/5", it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.

if (far > 77){
  digitalWrite(relayPin, HIGH); //Close the relay  
} 
     // Somewhere about here I want relayPin (2) to stay high until far drops below 72
     // basically leaving relayPin on until the temperature drops 5 degrees below turn on point

  else {
    digitalWrite(relayPin, LOW); //disconnect the relay 
  }

So lose the else and write the pin low when the far gets below 72.

if(far > 77){
    digitalWrite(relayPin, HIGH);
}
if (far < 72){
   digitalWrite(relayPin, LOW);
}

Now between 72 and 77 it will keep whatever value was last written to it.

Thanks Delta_G that works!

I was over thinking it. I knew it would be a simple solution.

Thanks for your help I really appreciate it.