Go Down

Topic: If statement not tripping, trying to control beer temp. (Read 1 time) previous topic - next topic

SqueezeCheeze

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:

Code: [Select]
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.

Nick Gammon

Code: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

SqueezeCheeze

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.

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

cyclegadget


Quote
as verified with a lightbulb


What kind of lightbulb? Do you mean LED?
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

SqueezeCheeze

#5
Apr 22, 2012, 04:32 am Last Edit: Apr 22, 2012, 04:34 am by SqueezeCheeze Reason: 1
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.

Professor Chaos

#6
Apr 22, 2012, 06:25 am Last Edit: Apr 22, 2012, 06:32 am by Professor Chaos Reason: 1
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.

Nick Gammon

As Prof Chaos said, you need to set the pins to output if you expect much to happen.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

SqueezeCheeze

thanks fellas.  It's working wonderfully now.

-Cheers

Go Up