If statement not tripping, trying to control beer temp.

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.

int 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;
    }

The two "state" variables here are different variables. Putting "int" there inside the "if" creates a local state variable, different from the global one. Lose the "int" there.

Thanks a bunch that fixed alot. Amazing how something seemingly so small will make a huge impact. I can verify it runs the if statements but and changes the "state" but it won't turn the relay on as verified with a lightbulb and the serial print.

Show the serial print output?

as verified with a lightbulb

What kind of lightbulb? Do you mean LED?

26 diff 76.82 degrees F 598 time 0 state 0 on/off 25 diff 75.95 degrees F 599 time 0 state 0 on/off 25 diff 75.95 degrees F 600 time 1 state 0 on/off 25 diff 75.07 degrees F 601 time 1 state 0 on/off 25 diff 75.07 degrees F 602 time 1 state 0 on/off

so confuzled. should have turned the compressorRelay on just before assigning state to 1.

@cyclegadget: I had a SSR hooked up to a lightbulb. Didn't want to burn out my compressor while I am trouble shooting this.

You need to set your pins to outputs with the pinMode command before using digitalWrite: http://arduino.cc/en/Tutorial/DigitalPins

And I'm not sure if you can use digitalRead to read the state of a pin set as an output.

EDIT: apparently you can... digitalRead will return the state that you set with digitalWrite.

As Prof Chaos said, you need to set the pins to output if you expect much to happen.

thanks fellas. It’s working wonderfully now.

-Cheers