First, I'm VERY new here as my code will soon show. I am trying to write code to control my fridge which is home for 5gal of my precious homebrewed beer. It is important that the compressor not be cycled on and off too quickly thus I attempted to include an off-cycle delay that would prevent it from turning on again too quickly. The problem I am currently having with my code is my IF statements are not tripping. I know a previous post had somehting like this recently but I couldn't see how the same solution would work here. Again, I am quite new to this. The serialprint indeed indicates the value of the difference is higher than the deviation but I can't get it to execute the code. I'm assuming it has something to do with the type of numbers I am using but I don't know. Here is my code:
const int probePin = 0;
const int compressorRelay = 13;
float caltempF = 0;
float settemp = 50;
int cycleTime = 6000; // short cycle timer will prevent turining the compressor on and off too much
int deviation = 2;
int previousState = 0;
int logtime = 0;
int state = 0;
void setup (){
Serial.begin(9600);
delay (5000);
}
void loop(){
int reading = analogRead(probePin); //getting the voltage reading from the temp sensor
float voltage = reading * 5.0; // converting that reading to voltage
voltage /= 1024.0;
float temperatureC = (voltage) * 100; //converting voltage to C with 10mV per degree
float caltempF = (temperatureC * 9.0 / 5.0) + 32.0; //now converted to F
int difference = caltempF - settemp;
if (previousState != state){ //detect if there was a state change
if (state == 0){ //determine if the state change was recently switched to off
logtime = 0;}; // reset timer
previousState = state;
}
if (difference <= deviation){
digitalWrite(compressorRelay, LOW);
state = 0;
}
if (difference > deviation){ //only turn on if both the temp is too high AND there has been an appropriate delay between the last initiation of off cycle
digitalWrite(compressorRelay, HIGH);
int state = 1;
}
Serial.print(difference); Serial.print(" diff ");
Serial.print(caltempF); Serial.print(" degrees F ");
Serial.print(logtime); Serial.print(" time ");
Serial.print(state); Serial.print(" state ");
Serial.print(digitalRead(compressorRelay)); Serial.println(" on/off ");
logtime ++;
}
Thanks for any help you might offer.