finite state machine help

Hi
Im doing my first project on the arduino and i cant seem to get the code to work properly… it does compile but it doesnt seem to work.
The code is supposed to implement a control system to control a relay in my water cooler which turns on/off the compressor to cool the water. This is done with a state machine(which i believe is the problem) and a timer which makes sure the compressor is turned on for no longer than 45mins.

here is the code…

// defined states
# define COMPAR_LOW 0
# define COMPAR_HIGH 1
//inputs
int sensorPin = 0;        //temerature sensor
float sensorValue = 0;    //temperature value
int temp_potPin = 1;      //reference temperature 
float temp_pot_value = 0; //reference temperature value
//outputs
int relay_outPin = 11;
int status_ledPin = 12;
// Internal vars
unsigned int softTime = 0 ;
unsigned int seconds = 0 ;
int  begin_count = 0;
int result = 6;
int state;

//Timer2 overflow interrupt vector handler
ISR(TIMER2_OVF_vect) {
  // set bit 7 in the status register high to disable global interupts
  SREG = 128 ;
  //Toggle the IO pin to the other state.
  softTime += 1;
  sensorValue = analogRead(sensorPin);
  temp_pot_value = analogRead(temp_potPin);
  TCNT2 = result; 
  
  // set bit 7 in the status register low to enable global interupts
  SREG = 0 ;
}

void setup(void)
{
  //Timer2 Settings: Timer Prescaler /256, mode 0
  //Timmer clock = 8MHz/256 = 31250Hz or 32us
  //The /256 prescale gives us a good range to work with so we just hard code this for now.
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 0<<CS20; 
  //Timer2 Overflow Interrupt Enable   
  TIMSK2 = 1<<TOIE2;

  TCNT2 = result; 
   pinMode(sensorPin, INPUT);
   pinMode(temp_potPin, INPUT);
   pinMode(relay_outPin, OUTPUT);
   pinMode(status_ledPin, OUTPUT);
   Serial.begin(9600);          //  setup serial  
  
}

void loop()
{
  // print the values of the state and ADC values for debugging
  Serial.println( state );
  Serial.println( sensorValue );
  Serial.println( temp_pot_value );
   
  switch ( state )
  {
    case COMPAR_LOW:
    
    digitalWrite(relay_outPin, LOW);
    digitalWrite(status_ledPin, LOW);
    if ( sensorValue < 1.1*temp_pot_value )
    {
      state = COMPAR_LOW; 
    }
    else if ( sensorValue > 1.1*temp_pot_value )
    {
      state = COMPAR_HIGH;
    }
    
    break;
    
    case COMPAR_HIGH:
    
    digitalWrite(relay_outPin, HIGH);
    digitalWrite(status_ledPin, HIGH);
    if ( sensorValue > 0.9*temp_pot_value )
    {
      state = COMPAR_HIGH; 
    }
    else if ( sensorValue < 0.9*temp_pot_value )
    {
      state = COMPAR_LOW;
    }
    
    break;

  }
  while ( state = COMPAR_HIGH )
  {
    begin_count = 1;
  }
  
  while ( begin_count = 1 )
  {
    if( softTime > 124)
    {
       // softTime counts 125 8ms intervals to give 1 second
       seconds ++;
       softTime = 0; 
    }
    if ( seconds > 2700 )
    {
       state = COMPAR_LOW; //if after 45mins the cooler is 
       seconds = 0;        //still on turn it off and reset
       begin_count = 0;    //the timer
    }
  }
}

Brent,

You'll probably want to change this linewhile ( state = COMPAR_HIGH )towhile ( state == COMPAR_HIGH )andwhile ( begin_count = 1 )towhile ( begin_count == 1 )

Rather than using a timer interrupt, my preference would be to just use millis() and poll the ports in a loop. It certainly makes debugging easier.

Regards,

-Mike

to help catch this error some people have advocated the habit of putting the constant first in comparisons: if ( 10 = x) { } gives an error message.

Thanks for the suggestion, that was a silly mistake on my part... Now after some debugging i figured out that my timing method was the problem and not my state machine... So my other noobish question is this... I have a state machine running and checking state conditions... now along comes a state and i gotta start a timer which i had initially implemented in the main loop after the state machine. This will screw up my program since the timer affects the checking and updating of the state. I have worked with QNX before and my usual approach would be to throw the timer in a seperate thread. Since i know this cant be done here... how could i go about implementing the timer?

Thanks

Brent