Almost everything works well, except the relayPin starts HIGH. The temp readings are uniformly higher by about 12-15 degF than they should be. Any suggested corrections? Thanks for the help. My code follows:
// Pins
int SsensorPin = 4; // the pin to read solar panel temperature
int TsensorPin = 5; // the pin to read water tank temperature
int RelayPin = 2; // the pin to control the pump relay
int ledPin = 13; // led pin to verify relay on condition
// Variables
int Ssensor = 0; // holds the value from SsensorPin
int SensorOnDiff = 0; // holds the Ssensor value plus on differential (OnDiff)
int SensorOffDiff = 0; // holds the Ssensor value plus off differential (OffDiff)
int Tsensor = 0; // holds the value from TsensorPin
int SensorReadings = 10; // number of sensor readings to take
int Counter = 0; // sensor readings loop counter
int OnDiff = 4; // temp diff that must exist to turn pump on (dec F)
int OffDiff = 2; // temp diff that must exist to turn pump off (deg F)
void setup() {
pinMode(SsensorPin, INPUT);
pinMode(TsensorPin, INPUT);
pinMode(RelayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
digitalWrite(RelayPin, LOW); // turn pump off
}
void loop() {
// read sensor inputs, convert to degrees, and assign to variables
Counter = 0;
while(Counter < SensorReadings) {
Ssensor += analogRead(SsensorPin) / 2;
Tsensor += analogRead(TsensorPin) / 2;
Counter++;
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}
// calculate average temp from sensor inputs
Ssensor = Ssensor / SensorReadings;
Tsensor = Tsensor / SensorReadings;
// add temperature difference to Ssensor value
SensorOnDiff = Ssensor - OnDiff;
SensorOffDiff = Ssensor - OffDiff;
Serial.print("Solar temp= ");
Serial.println(Ssensor);
Serial.print("Tank temp= ");
Serial.println(Tsensor);
// if heat source is warmer than heat sink sensor, enable relay
if (SensorOnDiff > Tsensor) {
digitalWrite(RelayPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(3000);
}
// if heat source sensor is cooler than heat sink sensor, disable relay
if (SensorOffDiff < Tsensor) {
digitalWrite(RelayPin, LOW);
digitalWrite(ledPin, LOW);
delay(500);
}
}
|