Relay and temperature sensor

Hello everybody,

I have a temperature sensor which shouldn't exceed 37°. I have a relay which activates a heater.
When I reach the threshold, the heater should be stopped. When the temperature goes below, it should be reactivate.

I have a prublem. The first one is related to the threshold. When my values are decreasing or increasing around the threshold (36,8-37,2 ), the relay is flashing because the values are one time below the threshold, one time above.

So I tried to use an average function which outputs datas from a range of values. This works not so bad except when I reach the threshold, the relay is activated but the values aren't sent anymore to the serial termnial. The temperature then isn't red anymore and is increasing higher and higher without any limitations...

Here is my code. Does anybody have an idea where I'm doing wrong?

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product
byte PinChauffage = 2;
byte PinLumiere=3;
byte PinBullage = 4;
int tempSensor = 8;
float temp;                
//Aveage variables
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                


int led = 13;
byte val = 0;
// variable etat Relais 1
int etatRelais1;
boolean lock_temp = true;
int state = 0;
int sensorDelay=500;
// Threshold
int seuil = 37;
int tempMin = 20;





void setup() { 
  Serial.begin(38400);
  pinMode(tempSensor, OUTPUT);
  pinMode(PinChauffage,OUTPUT);
  pinMode(PinLumiere,OUTPUT);
  pinMode(PinBullage,OUTPUT);
  pinMode(led,OUTPUT);
  etatRelais1=HIGH;  
  digitalWrite(PinChauffage,etatRelais1);
  digitalWrite(PinBullage,etatRelais1);
  digitalWrite(PinLumiere,LOW);
  // initialisation données à 0 pour le lisseur
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;        
}   





void loop() { 
  temp = read_temp(); 
  total= total - readings[index];        
  readings[index] = temp;
  total= total + readings[index];      
  index = index + 1;                    
  if (index >= numReadings){        
    index = 0;         
  }    
  average = total / numReadings;    
  delay(1);        // delay in between reads for stability   

  Serial.println("Average :");  
  Serial.println(average);  
  Serial.println("      "); 
  Serial.println("Temp capteur :"); 
  Serial.println(temp);     //print the temperature data
  delay(500);   
  if (average <= seuil){ 
    digitalWrite(PinChauffage,LOW);
    digitalWrite(PinLumiere,LOW);

  }
  if (average > seuil){
    digitalWrite(PinChauffage,HIGH);
    digitalWrite(PinLumiere,HIGH);

  }
  delay(sensorDelay);
}


float read_temp(void){
  float v_out;  //voltage output from temp sensor   
  float temp;  //the final temperature is stored here 
  digitalWrite(A0, LOW);  //set pull-up on analog pin
  digitalWrite(tempSensor, HIGH);  //set pin 2 high, this will turn on temp sensor
  delay(2);               //wait 1 ms for temp to stabilize
  v_out = analogRead(0);  //read the input pin
  digitalWrite(tempSensor, LOW);  //set pin 2 low, this will turn off temp sensor
  v_out*=.0048;         //convert ADC points to volts (we are using .0048 because
  v_out*=1000;          //this device is running at 5 volts)    
  temp= 0.0512 * v_out -20.5128;  //convert volts to millivolts the equation from millivolts to temperature  
  return temp;             //send back the temp
}

An alternative way to handle this is to use two thresholds: turn your heater on when the temp is below the lower one and off when it exceeds the higher. q.v. Hysterisis.

I thought I could find a function for the hysterisis but none. However, I guess I can make one enough simple but wouldn't the problem be the same, if I set a new threshold, wouldn't I encounteer the same problem?

Badiou:
I thought I could find a function for the hysterisis but none. However, I guess I can make one enough simple but wouldn't the problem be the same, if I set a new threshold, wouldn't I encounteer the same problem?

No; hysteresis gives you a 'dead band' so that the output has to drop by a small amount before the heater will come back on.

The behaviour you're simulating is the same as an old fashioned, cheap, simple, reliable bimetallic thermostatic switch; have you considered just using one of those?