Different actions if increasing or decreasing temperatures

Hi Forum
I am new here and new to Arduino.
But never the less I have started building a wood stove controller.
The controlling is done by servos controlling the air vents in regards to the chimney and room temperature.

I have some issues,
I would like to do different actions depending if the temperature is increasing or decreasing. ?
And I cant seem to find any examples on the www.

Also I need to change servo direction. Can that be done by some mapping command or how is it done ?

As I said I am new to this, and my sketch is made up of a lot of snips, from around the www.
Therefor probably not the prettiest code around.
But it pretty much does what I want it to do.

There is still a lot to do, clean up, write in servo2 and so on. But here it is.

// Stove Control ver: 1,3

// Include Library Code
#include <max6675.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#include <PID_v1.h>

// define integers
  int x = 0;
  int setBeepBuzzer = 1;
//  int setStoveMode = 0;                     //New 
  int muteBuzzer = LOW;
  int muteButtonLight = LOW;
  int overTemp = LOW;
  int stoveMode = HIGH;
//  int stoveMode2 = LOW;                   //New
  int glowMode = LOW;                     //New
  int stoveShutdown = LOW;
  double stoveRunTemp = 140;
//  double stoveRunTemp2 = 120;             //New
  int overtempSetPoint = 180;
  int servo1Pos = 11;
  int servo2Pos = 22;
  int roomTempSetPoint = 23;
//  int glowModeSetting = 35;                  //New
  int minServo1Setting = 41;
  int minServo2Setting = 32;
  int stoveShutdownSetting = 2;
  double cdiff = 0;
  double roomTemp = 23;
  double c = 0;
  int cInt = c;
  int cfg = 0;
  double Output;
  double cnew;
  int stoveRunTempInt = stoveRunTemp;
//  int stoveRunTemp2Int = stoveRunTemp2;     //New

// define pin constants  

// Pin 2-7 assigned to LCD
  const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;               // initialize the library by associating any needed LCD interface pin
  LiquidCrystal lcd(rs, en, d4, d5, d6, d7);                              // with the arduino pin number it is connected to

  const int buzzerPin = 8;
  const int RoomTemp = 9;
  const int muteButtonLightPin = 10;
  const int servo1Pin = 11;
  const int servo2Pin = 12;
    
  const int buttonRoomTempSetPointUp = 14;
  const int buttonRoomTempSetPointDown = 15;
  const int buttonMutePin =16;
  const int buttonStoveTempSetPointUp = 17;
  const int buttonStoveTempSetPointDown = 18;

// ThermoCouple // MAX6675 pin assignment
  const int thermo_gnd_pin = 45;                               
  const int thermo_vcc_pin = 47;
  const int thermo_sck_pin = 49;
  const int thermo_cs_pin  = 51;
  const int thermo_so_pin  = 53;
  
  int count = 0;
  
MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);

  Servo servo1;                          //defining servos
  Servo servo2;

  PID myPID(&c, &Output, &stoveRunTemp, 4, .2, 1, DIRECT);

  #define ONE_WIRE_BUS 9                // Data wire is plugged into port 9 on the Arduino
  OneWire oneWire(ONE_WIRE_BUS);        // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) 
  DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature.

void setup() {
    servo1.attach(servo1Pin,560,2350);         // (pin, min, max)
    servo2.attach(servo2Pin,600,2380);         // (pin, min, max)
    pinMode(buzzerPin, OUTPUT);
    pinMode(muteButtonLightPin, OUTPUT);
    pinMode(buttonRoomTempSetPointUp, INPUT);
    pinMode(buttonRoomTempSetPointDown, INPUT);
    pinMode(buttonMutePin, INPUT);
    pinMode(buttonStoveTempSetPointUp, INPUT);
    pinMode(buttonStoveTempSetPointDown, INPUT);

    Serial.begin(9600);
    pinMode(thermo_vcc_pin, OUTPUT);digitalWrite(thermo_vcc_pin, HIGH); 
    pinMode(thermo_gnd_pin, OUTPUT);digitalWrite(thermo_gnd_pin, LOW); 
 
    Serial.println("Balle: MAX6675 test");
delay(500);                                   // wait for MAX chip to stabilize
 
    lcd.begin(16, 2);
    
    servo1.write(180);
    servo2.write(180);
    
    myPID.SetOutputLimits(minServo1Setting, 180);
//    myPID.SetOutputLimits(minServo2Setting, 180);
    myPID.SetMode(AUTOMATIC);
    
    //         1234567890123456
    lcd.print("* Stove Robot  *");
    lcd.setCursor(0, 1);
    //         1234567890123456
    lcd.print("*   by  BALLE  *");
// wait for MAX chip to stabilize
delay(500);
}


void loop() {

// basic serial readout and sensor test, print the current temp
//   Serial.print("F = ");
//   Serial.print(thermocouple.readFahrenheit());
   Serial.print("    C = "); 
   Serial.print(thermocouple.readCelsius());

   sensors.requestTemperatures();              //Send the command to get temperatures
   float x=sensors.getTempCByIndex(0);         //Serial.print("Temperature for the device 1 (index 0) is: ");
   Serial.print("    C1= ");                   //Serial.println(x); 
   Serial.println(sensors.getTempCByIndex(0));
   roomTemp = (sensors.getTempCByIndex(0));
delay(1000);

  digitalWrite(muteButtonLightPin, HIGH);      //turn off muteButtonLight


// thermocouple check
  thermocouple.readCelsius(); 
  cnew = thermocouple.readCelsius();
  while (isnan(cnew)) {
  cnew = thermocouple.readCelsius();
  lcd.setCursor(0, 1);
  lcd.print("* T/C Problem  *");
  digitalWrite(muteButtonLightPin, LOW);      //blink muteButtonLight
  delay(1200);
  lcd.setCursor(0, 1);
  lcd.print("*              *");
  digitalWrite(muteButtonLightPin, HIGH);     //blink MuteButtonLight
  delay(500);
}   
    cdiff = cnew - c;
    c = cnew;
    cInt = c; 

// Compute PID
    myPID.Compute();

//****************************************************
// Servo control section
// servoPos = 0 then damper is closed
// servoPos = 180 then damper is fully opened

if (!stoveShutdown && c <= roomTemp + 20) {  // close the damper if the fire is out
      stoveShutdown = HIGH;                                                                    //LOW or HIGH ?
    }
    if (stoveShutdown && c >= roomTemp +25) {  // open the damper when the fire is started
        stoveShutdown = LOW;
    }
    if (overTemp == HIGH) {
      stoveMode = LOW;
    }
    if (roomTemp <= roomTempSetPoint -.5) {
      stoveMode = HIGH;
    }
    if (roomTemp >= roomTempSetPoint +.5 && stoveRunTemp >= 140) {
     stoveRunTemp = stoveRunTemp -20;
    }
    if (stoveShutdown) {
      servo1.write(stoveShutdownSetting);
      servo1Pos = stoveShutdownSetting;
    }
    else {
      if (stoveMode) {
        servo1.write(Output);
        servo1Pos = Output;
      }
      else
      {
        servo1.write(minServo1Setting);
        servo1Pos = minServo1Setting;
      }
    }

// input loop for 5 seconds
    for (int y = 0; y < 50; y++) {
      
// read mute button
    if (muteBuzzer == LOW) {          // HIGH or LOW ?
      muteBuzzer = digitalRead(buttonMutePin);
    }    

// buzzer overtemp alarm section
    if (muteBuzzer) {
      if (c < overtempSetPoint) {     //resets the mute buzzer variable if stove temp goes below overtemp
        muteBuzzer = LOW;             // HIGH or LOW ?
        overTemp = LOW;
        }  
      digitalWrite(buzzerPin, HIGH);
      }
    else
      {
      if (c > overtempSetPoint) {
        digitalWrite(buzzerPin, LOW);
        digitalWrite(muteButtonLightPin, LOW);
        overTemp = HIGH;
      }
      else
        {
        digitalWrite(buzzerPin, HIGH);
        digitalWrite(muteButtonLightPin, HIGH);
        overTemp = LOW;
      }
    }

I would like to do different actions depending if the temperature is increasing or decreasing. ?

Read the current temperature
If it is less than the previous time it was read call the goingDown() function
If it is more than the previous time it was read call the goingUp() function
Save the current temperature as the previous temperature ready for the next time you test it

As to what you do in the two functions that can be anything reasonable such as

void goingDown()
{
  theServo.write(0);  //adjust to suit your requirements
}

void goingUp()
{
  theServo.write(180);  //adjust to suit your requirements
}

I'd investigate PID controller libraries, given the output seems to be analog.

Rather than think of changing temperature and changing direction of servos, you may simply
find that you need a controller whose input is actual temperature and whose output is servo position.

Thanks for your answers, but I might have to ask you for more specific code.
I am way too new in arduino to figure this one out.

UKHeliBob.
Reading the temperature and comparing, am I doing this here (already in the sketch):
cdiff = cnew - c;
c = cnew;
cInt = c;
If so, I think I might have to compare over a minute or two, as the temperature decrease will be very slow.
Is that possible without pausing everything else.

MarkT.
I think the PID input is actual temperature from the thermo couple and output is servo position, but again, I am a novice so I am just guessing.

About servo direction. I could switch the two connections on the motor and the two outer connections on the potentiometer, inside the servo.
But I would rather if it was possible to do in the code.
That would make it much easier to replace servos in the future.

If so, I think I might have to compare over a minute or two, as the temperature decrease will be very slow.
Is that possible without pausing everything else.

Do you mean that you only want to compare old and new temperatures every minute ? If s then that is perfectly possible without pausing everything

Save temperature and the value of millis() when the timing period starts then each time through loop() compare the current value of millis() with the start value. If a minute has elapsed then read the temperature again and act as required and save the temperature and start time again and start over. If a minute has not elapsed then go round loop() again. None of the code in loop() must prevent it running freely

To reverse direction:

position = 45;
servo.write(180 - position);

= 135.

Outsider.
Is that possible to do on a "global" scale, like when defining servos or must it be done every time servoposition is mentioned in the code ?

UKHeliBob.
I don't know how often I need to compare temperatures but the temperature change is not very fast, at least not on the way down.
That's why I am thinking every 60 seconds or even more.
By the way, I found something I believe is yours:
http://forum.arduino.cc/index.php?topic=503368.msg3433063#msg3433063

I don't know how often I need to compare temperatures but the temperature change is not very fast, at least not on the way down.

How about only responding when the temperature has changed by a certain amount whatever the time period over which it occurs ?

Okay I think I have the millis timer up and running, at least I get some positive values (milCelsDiff) out on the way up and some negative on the way down.

UKHeliBob.
In your first reply you suggested more void functions. Is that two voids inside the void loop ?
Can they be isolated to work only when temperature is increasing or decreasing and how does it work ?
Where can I find relevant examples of this methode ?
I am at the parrot level you know :wink: copy paste copy paste.....

// Stove Control ver: (millis delay temp sample)

// Include Library Code
#include <max6675.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#include <PID_v1.h>

// define integers
  int x = 0;
  int setBeepBuzzer = 1;
//  int setStoveMode = 0;                     //New 
  int muteBuzzer = LOW;
  int muteButtonLight = LOW;
  int overTemp = LOW;
  int stoveMode = HIGH;
//  int stoveMode2 = LOW;                   //New
  int glowMode = LOW;                     //New
  int stoveShutdown = LOW;
  double stoveRunTemp = 140;
//  double stoveRunTemp2 = 120;             //New
  int overtempSetPoint = 180;
  int servo1Pos = 11;
  int servo2Pos = 22;
  int roomTempSetPoint = 23;
//  int glowModeSetting = 35;                  //New
  int minServo1Setting = 41;
  int minServo2Setting = 32;
  int stoveShutdownSetting = 2;
  double cdiff = 0;
  double roomTemp = 23;
  double c = 0;
  int cInt = c;
  int cfg = 0;
  double Output;
  double cnew;
  int stoveRunTempInt = stoveRunTemp;
//  int stoveRunTemp2Int = stoveRunTemp2;     //New

// millis timer
  unsigned long startMillis;                    //global variables
  unsigned long currentMillis;
  const unsigned long period = 10000;  // sample period delay, milliseconds
  double milCels = 0;
  double milCelsNew;
  double milCelsDiff = 0;

// define pin constants  

// Pin 2-7 assigned to LCD
  const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;      // initialize the library by associating any needed LCD interface pin
  LiquidCrystal lcd(rs, en, d4, d5, d6, d7);                              // with the arduino pin number it is connected to

  const int buzzerPin = 8;
  const int RoomTemp = 9;
  const int muteButtonLightPin = 10;
  const int servo1Pin = 11;
  const int servo2Pin = 12;
    
  const int buttonRoomTempSetPointUp = 14;
  const int buttonRoomTempSetPointDown = 15;
  const int buttonMutePin =16;
  const int buttonStoveTempSetPointUp = 17;
  const int buttonStoveTempSetPointDown = 18;

// ThermoCouple // MAX6675 pin assignment
  const int thermo_gnd_pin = 45;                               
  const int thermo_vcc_pin = 47;
  const int thermo_sck_pin = 49;
  const int thermo_cs_pin  = 51;
  const int thermo_so_pin  = 53;
  
  int count = 0;
  
MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);

  Servo servo1;                          //defining servos
  Servo servo2;

  PID myPID(&c, &Output, &stoveRunTemp, 4, .2, 1, DIRECT);

  #define ONE_WIRE_BUS 9                // Data wire is plugged into port 9 on the Arduino
  OneWire oneWire(ONE_WIRE_BUS);        // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) 
  DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature.

void setup() {
    servo1.attach(servo1Pin,560,2350);         // (pin, min, max)
    servo2.attach(servo2Pin,600,2380);         // (pin, min, max)
    pinMode(buzzerPin, OUTPUT);
    pinMode(muteButtonLightPin, OUTPUT);
    pinMode(buttonRoomTempSetPointUp, INPUT);
    pinMode(buttonRoomTempSetPointDown, INPUT);
    pinMode(buttonMutePin, INPUT);
    pinMode(buttonStoveTempSetPointUp, INPUT);
    pinMode(buttonStoveTempSetPointDown, INPUT);

    Serial.begin(9600);
    pinMode(thermo_vcc_pin, OUTPUT);digitalWrite(thermo_vcc_pin, HIGH); 
    pinMode(thermo_gnd_pin, OUTPUT);digitalWrite(thermo_gnd_pin, LOW); 
 
    Serial.println("Balle: MAX6675 test");
delay(500);                                   // wait for MAX chip to stabilize
 
    lcd.begin(16, 2);
    
    servo1.write(40);
    servo2.write(40);
    
    myPID.SetOutputLimits(minServo1Setting, 180);
//    myPID.SetOutputLimits(minServo2Setting, 180);
    myPID.SetMode(AUTOMATIC);
    
    //         1234567890123456
    lcd.print("* Stove Robot  *");
    lcd.setCursor(0, 1);
    //         1234567890123456
    lcd.print("*   by  BALLE  *");
// wait for MAX chip to stabilize
//delay(500);

// millis timer
startMillis = millis();  //initial start time
}


void loop() {

// basic serial readout and sensor test, print the current temp
//   Serial.print("F = ");
//   Serial.print(thermocouple.readFahrenheit());
   Serial.print("    C = "); 
   Serial.print(thermocouple.readCelsius());

   sensors.requestTemperatures();              //Send the command to get temperatures
   float x=sensors.getTempCByIndex(0);         //Serial.print("Temperature for the device 1 (index 0) is: ");
   Serial.print("    C1= ");                   //Serial.println(x); 
   Serial.println(sensors.getTempCByIndex(0));
   roomTemp = (sensors.getTempCByIndex(0));
delay(1000);

  digitalWrite(muteButtonLightPin, HIGH);      //turn off muteButtonLight


// thermocouple check
  thermocouple.readCelsius(); 
  cnew = thermocouple.readCelsius();
  while (isnan(cnew)) {
  cnew = thermocouple.readCelsius();
  lcd.setCursor(0, 1);
  lcd.print("* T/C Problem  *");
  digitalWrite(muteButtonLightPin, LOW);      //blink muteButtonLight
  delay(1200);
  lcd.setCursor(0, 1);
  lcd.print("*              *");
  digitalWrite(muteButtonLightPin, HIGH);     //blink MuteButtonLight
  delay(500);
}   
    cdiff = cnew - c;
    c = cnew;
    cInt = c; 

// Compute PID
    myPID.Compute();

// millis timer    
{
  currentMillis = millis();  //get the current "time" 
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    thermocouple.readCelsius(); 
    milCelsNew = thermocouple.readCelsius();
    milCelsDiff = milCelsNew - milCels;
    milCels = milCelsNew;
    startMillis = currentMillis;  //IMPORTANT to save the start time of period  
    
//  Serial.print(milCels); Serial.print(" milCels , ");
//  Serial.print(milCelsNew); Serial.print(" milCelsNew , ");
  Serial.print(milCelsDiff); Serial.print(" milCelsDiff , ");

  }
 }
//****************************************************
// Servo control section

In your first reply you suggested more void functions. Is that two voids inside the void loop ?

I suggested using separate functions to be called when the temperature is increasing or decreasing.

NOTES :
(1) They are functions. The word void in front of their name indicates that they do not return a value to the code that they are called from

(2) They are not "inside the void loop ? " they are called from within the loop() function but are defined outside of it, as they must be.

(3) Using functions rather than putting the code in loop() itself enables them to be given meaningful names, isolates the code and allow the functions to be tested separately if required

Can they be isolated to work only when temperature is increasing or decreasing and how does it work ?

Look closely at my suggested solution. The appropriate function would be called only when the temperature is increasing or decreasing

Well I got the functions working by calling them from while loops, but of course the functions block everything else while running.
That means blocking alarms, configuration changes, like runTemp, room Tempand so on.

I set it up like this:
One function for increasing temp up to 10deg before runTemp
One function for decreasing temp from 10deg below runTemp
And the normal void loop from 10deg below runTemp and up

I think I might try to solve the puzzle by a lot of "if statements" in relation to the millis timer temp check.

Or am I missing something here

xballe:
Or am I missing something here

Yes, you forgot to post your latest code :wink:

I sort of dropped the ide of functions.
Because it seems like a lot of unnecessary repeating of the void loop code, in both functions. If I want everything to work regardless of wich loop is running.
So the code is back to the setup I posted last. The complete code is too long to post here, so a few things are missing, like, servo control, alarms and configuration.

I sort of dropped the ide of functions.
Because it seems like a lot of unnecessary repeating of the void loop code, in both functions.

Oh no it doesn't. I think that you have misunderstood the principles involved.

I probably have misunderstood something, rookie mistake.
I tried to follow the while loop tutorial.
I called the functions via these while loops and it seemed to work as intended.

while (milCelsDiff >= 1.5 && c >= roomTemp +10 && c <= stoveRunTemp -10) { // call goingUp function
goingUp();
}

while (milCelsDiff <= -1.5 && c >= roomTemp +10 && c <= stoveRunTemp -10) { // call goingDown function
goingDown();
}
But it also seemed to block anything outside the functions from working while the loops are valid. And they are valid for a long time (20 min or more)
That's why I thought I needed to copy, anything from the void loop, that I want to work during that time, to the functions.
Does that even make sense?

Forget about using while as it blocks the running of the code. Substitute if for while and let loop() run freely. Each time through loop() the comparisons will be made and if needed the goingUp() or goingDown() function will be called. The rest of your code in loop() will run freely but ensure that it contains no blocking code such as delay() or while()

I'll give the if version a go, when I get back from work in 5 days time

I had a go at the "if" version. A few if's and a little tweaking solved my problems.
I am now a true believer in functions... amen !

I still need to change direction of the servo, controlled by the PID though.
Is that possible once and for all on a global scale, like in the setup or declaration ? And how will it have to be constructed ?
I have searched wide and broad, without finding something I can get to work.
Maybe I should mention that the PID only controlles a part of the total servo range of 0-180, if that makes any difference.

// Include Library Code
#include <max6675.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#include <PID_v1.h>
 
// define integers
 int x = 0;
 int setBeepBuzzer = 1;
 int muteBuzzer = LOW;
 int muteButtonLight = LOW;
 int overTemp = LOW;
 int stoveMode = HIGH;
 int stoveShutdown = LOW;
 double stoveRunTemp = 140;
 int overtempSetPoint = 170;
 int servo1Pos = 11;
 int servo2Pos = 22;
 int roomTempSetPoint = 23;
 int minServo1Setting = 41;
 int minServo2Setting = 32;
 int stoveShutdownSetting = 2;
 double cdiff = 0;
 double roomTemp = 23;
 double c = 0;
 int cInt = c;
 int cfg = 0;
 double Output;
 double cnew;
 int stoveRunTempInt = stoveRunTemp;
 
// millis timer to establish increasing/decreasing temperature
 unsigned long startMillis;           //some global variables
 unsigned long currentMillis;
 const unsigned long period = 15000;  //sample period delay milliseconds
 double milCels = 0;
 double milCelsNew;
 double milCelsDiff = 0;
 
//  double goingUp;
 
// define pin constants  
 
// Pin 2-7 assigned to LCD
 const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;      // LCD interface pins con. to Arduino
 LiquidCrystal lcd(rs, en, d4, d5, d6, d7);                    
 
 const int buzzerPin = 8;
 const int RoomTemp = 9;
 const int muteButtonLightPin = 10;
 const int servo1Pin = 11;
 const int servo2Pin = 12;
   
 const int buttonRoomTempSetPointUp = 14;
 const int buttonRoomTempSetPointDown = 15;
 const int buttonMutePin =16;
 const int buttonStoveTempSetPointUp = 17;
 const int buttonStoveTempSetPointDown = 18;
 
// ThermoCouple // MAX6675 pin assignment
 const int thermo_gnd_pin = 45;                              
 const int thermo_vcc_pin = 47;
 const int thermo_sck_pin = 49;
 const int thermo_cs_pin  = 51;
 const int thermo_so_pin  = 53;
 
 int count = 0;
 
MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);
 
 Servo servo1;                          //defining servos
 Servo servo2;
 
 PID myPID(&c, &Output, &stoveRunTemp, 4, .2, 1, DIRECT);
 
 #define ONE_WIRE_BUS 9                // Data wire is plugged into port 9 on the Arduino
 OneWire oneWire(ONE_WIRE_BUS);        // Setup oneWire to communicate with any OneWire devices, not just Maxim/Dallas
 DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature.
 
void setup() {
   servo1.attach(servo1Pin,560,2350);         // (pin, min, max)
   servo2.attach(servo2Pin,600,2380);         // (pin, min, max)
   pinMode(buzzerPin, OUTPUT);
   pinMode(muteButtonLightPin, OUTPUT);
   pinMode(buttonRoomTempSetPointUp, INPUT);
   pinMode(buttonRoomTempSetPointDown, INPUT);
   pinMode(buttonMutePin, INPUT);
   pinMode(buttonStoveTempSetPointUp, INPUT);
   pinMode(buttonStoveTempSetPointDown, INPUT);
 
   Serial.begin(9600);
   pinMode(thermo_vcc_pin, OUTPUT);digitalWrite(thermo_vcc_pin, HIGH);
   pinMode(thermo_gnd_pin, OUTPUT);digitalWrite(thermo_gnd_pin, LOW);
 
   Serial.println("Balle: MAX6675 test");
delay(500);                                   // wait for MAX chip to stabilize
 
   lcd.begin(16, 2);
   
   servo1.write(40);
   servo2.write(40);
   
   myPID.SetOutputLimits(minServo1Setting, 180);
//    myPID.SetOutputLimits(minServo2Setting, 180);
   myPID.SetMode(AUTOMATIC);
   
   //         1234567890123456
   lcd.print("* Stove Robot  *");
   lcd.setCursor(0, 1);
   //         1234567890123456
   lcd.print("*   by  BALLE  *");
// wait for MAX chip to stabilize
//delay(500);
 
// millis timer
startMillis = millis();  //initial start time
}
 
 
void loop() {
 
// basic serial readout and sensor test, print the current temp
//   Serial.print("F = ");
//   Serial.print(thermocouple.readFahrenheit());
  Serial.print("    C = ");
  Serial.print(thermocouple.readCelsius());
 
  sensors.requestTemperatures();              //Send the command to get temperatures
  float x=sensors.getTempCByIndex(0);         //Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.print("    C1= ");                   //Serial.println(x);
  Serial.println(sensors.getTempCByIndex(0));
  roomTemp = (sensors.getTempCByIndex(0));
delay(500);
 
 digitalWrite(muteButtonLightPin, HIGH);      //turn off muteButtonLight
 
 
// thermocouple check
 thermocouple.readCelsius();
 cnew = thermocouple.readCelsius();
 while (isnan(cnew)) {
 cnew = thermocouple.readCelsius();
 lcd.setCursor(0, 1);
 lcd.print("* T/C Problem  *");
 digitalWrite(muteButtonLightPin, LOW);      //blink muteButtonLight
 delay(1200);
 lcd.setCursor(0, 1);
 lcd.print("*              *");
 digitalWrite(muteButtonLightPin, HIGH);     //blink MuteButtonLight
 delay(500);
}  
   cdiff = cnew - c;
   c = cnew;
   cInt = c;
 
// Compute PID
   myPID.Compute();
 
// millis timer    
{
 currentMillis = millis();                   //get the current "time"
 if (currentMillis - startMillis >= period)  //test whether the period has elapsed
 {
   thermocouple.readCelsius();
   milCelsNew = thermocouple.readCelsius();
   milCelsDiff = milCelsNew - milCels;
   milCels = milCelsNew;
   startMillis = currentMillis;              //IMPORTANT to save the start time of period  
   
//  Serial.print(milCels); Serial.print(" milCels , ");
//  Serial.print(milCelsNew); Serial.print(" milCelsNew , ");
 Serial.print(milCelsDiff); Serial.print(" milCelsDiff , ");
 
 }
}
 
//*****************************************************************************************************
 
// Servo control section