I found a small inaccuracy with the code today. I wasn't resetting the variable that is used to hold the sensor readings. So, for example, on the first run through it starts as 0, takes 10 readings, averages them to eliminate any noise over the lines. The second time through it reads that averaged number going into it, then adds 10 more values, then averages them. This definitely throws the true readings off. However, since its a differential controller, its not horribly important because both sensors will be off relatively the same amount. However, it is fixed now.
I've been having some issues with the attic fan not turning off like it should, so I've been troubleshooting. Here is the latest code.
Code:
/* Thermal Differential Controller
The program monitors two temperature sensors and
activates a relay when one sensor is warmer than
the other.
*/
// Pin declaration
const int SsensorPin = 4; // the pin to input heat source temperature
const int TsensorPin = 5; // the pin to input sink temperature
const int RelayPin = 2; // the pin to operate the relay
const int ledPin = 13; // led pin (verify load power on condition)
// Variable declaration
int Ssensor = 0; // variable to store the value of the heat source temperature
int SensorOnDiff = 0; // variable to store the Ssensor value plus on differential (OnDiff)
int SensorOffDiff = 0; // variable to store the Ssensor value plus off differential (OffDiff)
int Tsensor = 0; // variable to store the value of the heat sink temperature
int SensorReadings = 15; // number of sensor readings to take
int Counter = 0; // sensor readings loop counter
int OnDiff = 6; // temperature difference that must exist between heat and sink sensors before relay enables (degrees C)
int OffDiff = 2; // temperature difference that must exist between heat and sink sensors before relay disables (degrees C)
void setup()
{
pinMode(SsensorPin, INPUT);
pinMode(TsensorPin, INPUT);
pinMode(RelayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Serial.begin(9600);
digitalWrite(RelayPin, LOW);
}
void loop()
{
// reset sensor variables and counter
Ssensor = 0;
Tsensor = 0;
Counter = 0;
// read sensor inputs and assign to variables
while(Counter < SensorReadings)
{
Ssensor += analogRead(SsensorPin) / 2;
Tsensor += analogRead(TsensorPin) / 2;
Counter++;
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
// smooth & calculate average temp from sensor inputs
Ssensor = Ssensor / SensorReadings;
Tsensor = Tsensor / SensorReadings;
// add temperature difference to Ssensor value
SensorOnDiff = Ssensor - OnDiff;
SensorOffDiff = Ssensor - OffDiff;
// if heat source is warmer than heat sink sensor, enable relay
if (SensorOnDiff > Tsensor)
{
digitalWrite(RelayPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(300000);
}
// if heat source sensor is cooler than heat sink sensor, disable relay
if (SensorOffDiff < Tsensor)
{
digitalWrite(RelayPin, LOW);
digitalWrite(ledPin, LOW);
delay(1000);
}
// send temperature signals back to computer
/*
Serial.print(Ssensor);
Serial.print(", ");
Serial.println(Tsensor);
*/
}