Relay and Thermistor Code

// set relay pin as pin 7 int relay = 7; int thermistorPin = 0; // set resistor value int R1 = 10000; int highTemperatureCycleThreshold = 6; int lowTemperatureCycleTheshold = 6; int delayInMilliseconds = 20000; int RELAY_OFF = 0; int RELAY_ON = 1; int isRelayOn = RELAY_OFF; float HIGH_TEMPERATURE = 20.0; float LOW_TEMPERATURE = 20.0; float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741; // when these flags reach a certain value the relay is triggered int lowTemperatureFlag = 0; int highTemperatureFlag = 0; void setup() { Serial.begin(9600); pinMode(relay, OUTPUT); // default relay to off digitalWrite(relay, LOW); } // convert from voltage to standard temperature values double getThermistorTemperature(int RawADC) { float R2 = (10240000 / RawADC) – R1; float logR2 = log(R2); // temp in Kelvin logR2 = 1 / (c1 + (c2 + (c3 * logR2 * logR2 )) * logR2); // temp in Celsius return logR2 – 273.15; } void loop() { int Vo = analogRead(thermistorPin); double temp = getThermistorTemperature(Vo); // flag if temp is greater than high temp if (temp > HIGH_TEMPERATURE) { highTemperatureFlag = highTemperatureFlag + 1; Serial.println(“highTemperatureFlag: ”); Serial.println(highTemperatureFlag); // turn relay on and reset counter if (highTemperatureFlag >= highTemperatureCycleThreshold) { if (isRelayOn == RELAY_OFF) { digitalWrite(relay, HIGH); isRelayOn = RELAY_ON; Serial.println(“turning relay on”); Serial.println(“isRelayOn: ”); Serial.println(isRelayOn); } Serial.println(“resetting counter (hightemp)”); highTemperatureFlag = 0; } } // flag if temp is less than low temp if (temp < LOW_TEMPERATURE) { lowTemperatureFlag = lowTemperatureFlag + 1; Serial.println(“lowTemperatureFlag: ”); Serial.println(lowTemperatureFlag); // turn relay off and reset counter if (lowTemperatureFlag >= lowTemperatureCycleTheshold) { if (isRelayOn == RELAY_ON) { digitalWrite(relay, LOW); isRelayOn = RELAY_OFF; Serial.println(“turning relay off”); Serial.println(“isRelayOn: ”); Serial.println(isRelayOn); } Serial.println(“resetting counter (lowtemp)”); lowTemperatureFlag = 0; } } Serial.println(temp); delay(delayInMilliseconds); }