Programming with MAX6675 library

I am having issues programming using the max6675 library found here. I am simply trying to control a relay based on temperature using a switch case and while function ( to have the relay turn on at a specific temperature .. . and eventually have a second relay turn on/off at a different temperature). However, I am not sure how to tell the arduino to test for the temperature during the while statement. I have included the code below. Any help would be much appreciated.

const int acRelay = 12;
const int heaterRelay =11;

// ThermoCouple

const int thermoDO = 5;
const int thermoCS = 4;
const int thermoCLK = 3;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//ThermoCouple setup

Serial.println("MAX6675 test");
pinMode(acRelay, OUTPUT);
pinMode(heaterRelay, OUTPUT);
digitalWrite(acRelay, LOW);
digitalWrite(heaterRelay, LOW);

delay(500);

}

void loop() {
// put your main code here, to run repeatedly:
int grainTemp = thermocouple.readFahrenheit();

Serial.print("Grain Temp: ");
Serial.println(thermocouple.readFahrenheit());

switch(grainTemp){
case 60:
while(grainTemp >=60) {
thermocouple.readFahrenheit();
digitalWrite(acRelay, HIGH);
} //end while
} // end switch

delay(1000);
} // end loop

In one of the calls to readFahrenheit, you save the value.
In the other you don't.
Why?

I don't see the point of the switch/case

Please remember to use code tags when posting code.

int grainTemp = thermocouple.readFahrenheit();

Serial.print("Grain Temp: ");
Serial.println(thermocouple.readFahrenheit());

Why do you need to read the thermocouple twice? Print the value in grainTemp, as the first statement implies you will.

switch(grainTemp){
  case 60:
  while(grainTemp >=60) {
    thermocouple.readFahrenheit();
    digitalWrite(acRelay, HIGH);
  } //end while
} // end switch

A switch statement with one case is silly. A simple if statement would be better.

You are reading the thermocouple again, and throwing the reading away. Store it in grainTemp, so you have a ghost of a chance of the while loop ending.

I guess the switch statement does not what you expect it to do (you must hit exactly 60°F to reach your while loop) and the while inside is absolutely not necessary anyway. Writing the acRelay pin to HIGH a few thousand times a second doesn't help, it's at that state after the first call.

I think what you really need is something like this:

void loop() {
  int grainTemp = thermocouple.readFahrenheit();

  Serial.print("Grain Temp: ");
  Serial.println(thermocouple.readFahrenheit());

  if (grainTemp >= 60) {
    digitalWrite(acRelay, HIGH);
  } else {
    digitalWrite(acRelay, LOW);
  }
  delay(1000);
}

That still has potential for improvements (p.e. switching the relay off only if the temperature is below 55°F to not continuously switching the relay if the temperature is around 60°F) but it probably does what you expected your sketch to do.

Thanks for the immediate responses. I want to use a switch case because i will eventually have to relays controlling a heating element and a cooling element. I am just in the prototyping phase right now and want to get the coding down first since this is my first experience with a k type thermocouple and the max6675 library. I was thinking use a switch case since I will eventually have two conditions while the temperature is >= 60 turn on the cooling element, while the temperature is <=57 turn on the heating element. So my initial thought was something like this:

switch(grainTemp){

case(60):

while (grainTemp>=60){
readgrainTemp;
digitalWrite(acRelay,HIGH);
}
break;
}
case(57):

while (grainTemp<=57){
readgrainTemp;
digitalWrite(heaterRelay,HIGH);
}
break;
}

I was thinking in using a switch case and while statements i could avoid clouding up the code with if and else statements. I will be building on the code later on.

i added the int grainTemp = thermocouple.readFahrenheit(); in there because I got a error code for not initializing the thermocouple.readFahrenheit as an int.

I tried a simple if statement like pylon suggested and it worked like expected but when I tried the while statement it would not. I realize that right now the switch and while are a bit much for whats currently needed but I am trying to plan ahead.

readgrainTemp;

What is this supposed to do?

that is just me explaining what I want the code to do -- hypothetically. i.e. not real code.

my last comment sounded rude - i did not mean for it to sound rude. What I mean is, for a while statement dont you need to have check for the condition that you want first then execute an action if that condition is true? Thats why I wrote in the readgrainTemp (still not the actual code I am using. Thats why I posted on here. Assuming the condition has to be checked I dont know how to tell the arduino to check for that condition for the while statement).

Assuming the condition has to be checked I dont know how to tell the arduino to check for that condition for the while statement).

int grainTemp = thermocouple.readFahrenheit();
if(grainTemp >= 60)
{
    // Turn the AC on
}
while(thermocouple.readFahrenheit() >= 60)
{
    // Do nothing until the grain cools down
}
// Turn the AC off

But, you can see that this is blocking code. It doesn't need to be.

On every pass through loop(), read the temperature. If the value is greater than or equal to 60, turn the AC on. If it is below 60, turn it off. loop() can spin millions of times doing nothing while the AC runs. Or, it can be doing other things while the AC runs.

I tried a simple if statement like pylon suggested and it worked like expected but when I tried the while statement it would not. I realize that right now the switch and while are a bit much for whats currently needed but I am trying to plan ahead.

The switch/while statements are in your case not planning ahead but simply wrong. Even if you add a cooling element and some fans or other stuff that needs to be controlled by the temperature, a switch statement will not be the right choice, not now and not in the future.
I already explained why the while loop is not necessary, it will not make any sense with additional equipment too.

I will stick with the if else commands for this function. Thank you very much for the responses everyone. Sorry for being a newbie.