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!