Break out of the 45 minute while loop

Hi everyone
I wrote a temperature control program for Arduino and the VOID LOOP routine has several ( about 7 ) while loop which two of them has 45 minutes long temperature soak and 15 minutes long soak. So my question is how to break out the 45 minutes while loop by pressing a button ( actually STOP button ) ?
regards

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

We need to see your code.

Thanks.. Tom.. :slight_smile:

Hi,
Are you actually checking the STOP button while trapped in the while loop.

Can you tell us the application and what you want the code to do?

Thanks.. Tom.. :slight_smile:

Dear Tom
the Cycle code is (the Cycle is inside the Loop) :

void CYCLE(){
    while ( ( thermocouple.readCelsius() - offset ) <= 850 ){
    digitalWrite(SSR, LOW);
  }
  lastmillis1 = millis();
  while( ( millis() - lastmillis1 ) <= ( 2400L * 1000 ) ){
    if( ( thermocouple.readCelsius() - offset ) <= 850 ){
      digitalWrite(SSR, LOW);
      delay(100);
    }
    if( ( thermocouple.readCelsius() - offset ) > 850 ){
      digitalWrite(SSR, HIGH);
      delay(100);
    }
  }
  BEEP1();
  while ( digitalRead ( START_SW ) == HIGH ){
    if( ( thermocouple.readCelsius() - offset ) <= 700 ){
      digitalWrite(SSR, LOW);
      delay(100);
    }
    if( ( thermocouple.readCelsius() - offset ) > 700 ){
      digitalWrite(SSR, HIGH);
      delay(100);
    }
  }
  while ( ( thermocouple.readCelsius() - offset ) < Htemp ){
    Ctemp = thermocouple.readCelsius()- offset;
    delay(1000);
    if ( ( thermocouple.readCelsius() - offset - Ctemp ) < 1 ){
      digitalWrite(SSR, LOW);
    }
    else {
      digitalWrite(SSR, HIGH);
    }
  }
  lastmillis2 = millis();
  while( ( millis() - lastmillis2 ) <= ( 900L * 1000 ) ){
    if( ( thermocouple.readCelsius() - offset ) <= Htemp ){
      digitalWrite(SSR, LOW);
      delay(100);
    }
    else if( ( thermocouple.readCelsius() - offset ) > Htemp ){
      digitalWrite(SSR, HIGH);
      delay(100);
    }
  }
  BEEP2();
}

Tom
Actually something like that, Is this possible to check the while loop condition when trapped inside the while loop for a long time ? OR somehow, break the routine (cycle) which the program is trapped inside the one of while loops inside the Cycle routine ?

Hi,
Consider writing with if statements instead of while.

Instead of reading your temperature when ever you need it , read it at the start of the void loop(), assign the temperature to a variable and use that variable through your code until it loops back.

While loops are blocking code, if statements allow the code to keep flowing.

We need to see your complete code?
What is it supposed to do?

Tom... :slight_smile:

Break out of the 45 minute while loop

You should not have a 45 minute loop in the first place. Nothing should take longer than a few millisecs.

Have a look at how millis() is used to manage timing without blocking in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time. And there may be dozens of calls to some functions before it is actually time for anything to happen.

...R

Tom
the complete code is :

#include <Wire.h>
#include <LM75.h>
#include "max6675.h"


const int thermoDO = 6;
const int thermoCS = 0;
const int thermoCLK = 7;
const int LS100_LED = 8;
const int LS200_LED = 9;
const int METALCAST_LED = 10;
const int HP_LED = 11;
const int MP_LED = 12;
const int LP_LED = 13;
const int START_LED = 14;
const int STOP_LED = 15;
const int LS100_SW = 31;
const int LS200_SW = 30;
const int METALCAST_SW = 29;
const int HP_SW = 28;
const int MP_SW = 27;
const int LP_SW = 26;
const int START_SW = 25;
const int STOP_SW = 24;
const int SSR = 4;
const int BUZZER = 2;
float type_k = 0;
float lm75 = 0;
float offset = 0;
int Buffer = 0;
int Htemp = 0;
unsigned long lastmillis1 = 0;
unsigned long lastmillis2 = 0;
float Ctemp = 0;

LM75 sensor(0x48);  // initialize an LM75 object
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

void setup()
{
  pinMode(LS100_LED, OUTPUT);
  pinMode(LS200_LED, OUTPUT);
  pinMode(METALCAST_LED, OUTPUT);
  pinMode(HP_LED, OUTPUT);
  pinMode(MP_LED, OUTPUT);
  pinMode(LP_LED, OUTPUT);
  pinMode(START_LED, OUTPUT);
  pinMode(STOP_LED, OUTPUT);
  pinMode(LS100_SW, INPUT_PULLUP);
  pinMode(LS200_SW, INPUT_PULLUP);
  pinMode(METALCAST_SW, INPUT_PULLUP);
  pinMode(HP_SW, INPUT_PULLUP);
  pinMode(MP_SW, INPUT_PULLUP);
  pinMode(LP_SW, INPUT_PULLUP);
  pinMode(START_SW, INPUT_PULLUP);
  pinMode(STOP_SW, INPUT_PULLUP); 
  Wire.begin();
  delay(500);
  digitalWrite( SSR, HIGH );
  
  //----- Calibration Routine -----//
  if ( ( thermocouple.readCelsius() - sensor.temp() < 5 ) && ( digitalRead( START_SW ) == LOW ) ){
    CALIBRATION();
  }
  LED_TEST();
}

void loop(){
  digitalWrite( START_LED, LOW );
  if ( digitalRead( LS100_SW ) == LOW ){
    digitalWrite( LS100_LED, HIGH );
    digitalWrite( LS200_LED, LOW );
    digitalWrite( METALCAST_LED, LOW );
    digitalWrite( LP_LED, LOW );
    digitalWrite( MP_LED, LOW );
    digitalWrite( HP_LED, LOW );
    Buffer =  10;
  }
  else if ( digitalRead( LS200_SW ) == LOW ){
    digitalWrite( LS100_LED, LOW );
    digitalWrite( LS200_LED, HIGH );
    digitalWrite( METALCAST_LED, LOW );
    digitalWrite( LP_LED, LOW );
    digitalWrite( MP_LED, LOW );
    digitalWrite( HP_LED, LOW );
    Buffer = 20;
  }
  else if ( digitalRead( METALCAST_SW ) == LOW ){
    digitalWrite( LS100_LED, LOW );
    digitalWrite( LS200_LED, LOW );
    digitalWrite( METALCAST_LED, HIGH );
    digitalWrite( LP_LED, LOW );
    digitalWrite( MP_LED, LOW );
    digitalWrite( HP_LED, LOW );
    Buffer = 30;
  }
  else if ( ( digitalRead( LP_SW ) == LOW ) && Buffer == 10 ){
    digitalWrite( LP_LED, HIGH );
    digitalWrite( MP_LED, LOW );
    digitalWrite( HP_LED, LOW );
    Buffer = 11;
    Htemp = 910;
  }
  else if ( ( digitalRead( MP_SW ) == LOW ) && Buffer == 10 ){
    digitalWrite( LP_LED, LOW );
    digitalWrite( MP_LED, HIGH );
    digitalWrite( HP_LED, LOW );
    Buffer = 12;
    Htemp = 915;
  }
  else if ( ( digitalRead( LP_SW ) == LOW ) && Buffer == 20 ){
    digitalWrite( LP_LED, HIGH );
    digitalWrite( MP_LED, LOW );
    digitalWrite( HP_LED, LOW );
    Buffer = 21;
    Htemp = 915;
  }
  else if ( ( digitalRead( MP_SW ) == LOW ) && Buffer == 20 ){
    digitalWrite( LP_LED, LOW );
    digitalWrite( MP_LED, HIGH );
    digitalWrite( HP_LED, LOW );
    Buffer = 22;
    Htemp = 917;
  }
  else if ( ( digitalRead( HP_SW ) == LOW ) && Buffer == 20 ){
    digitalWrite( LP_LED, LOW );
    digitalWrite( MP_LED, LOW );
    digitalWrite( HP_LED, HIGH );
    Buffer = 23;
    Htemp = 920;
  }
  else if ( digitalRead( START_SW ) == LOW ){
    digitalWrite( STOP_LED, LOW );
    STARTPRESS();
  }
}

void LED_TEST(){
  digitalWrite(LS100_LED, HIGH);
  digitalWrite(LS200_LED, HIGH);
  digitalWrite(METALCAST_LED, HIGH);
  digitalWrite(LP_LED, HIGH);
  digitalWrite(MP_LED, HIGH);
  digitalWrite(HP_LED, HIGH);
  digitalWrite(START_LED, HIGH);
  digitalWrite(STOP_LED, HIGH);
  delay(1000);
  digitalWrite(LS100_LED, LOW);
  digitalWrite(LS200_LED, LOW);
  digitalWrite(METALCAST_LED, LOW);
  digitalWrite(LP_LED, LOW);
  digitalWrite(MP_LED, LOW);
  digitalWrite(HP_LED, LOW);
  digitalWrite(START_LED, LOW);
  digitalWrite(STOP_LED, LOW);
}

void CALIBRATION(){
  lm75 = sensor.temp();
  type_k = thermocouple.readCelsius();
  offset = type_k - lm75;
}

void STARTPRESS(){
  if( (Buffer == 11) || (Buffer == 12) || (Buffer == 21) || (Buffer == 22) || (Buffer == 23) ){
    digitalWrite(START_LED, HIGH);
    CYCLE(); 
  }
  else if ( Buffer == 30 ){
    Htemp = 940;
    digitalWrite(START_LED, HIGH);
    CYCLE();
  }
  else {
    digitalWrite(BUZZER, HIGH);
    delay(100);
    digitalWrite(BUZZER, LOW);
    delay(50);
    digitalWrite(BUZZER, HIGH);
    delay(100);
    digitalWrite(BUZZER, LOW);
    delay(50);
    digitalWrite(BUZZER, HIGH);
    delay(100);
    digitalWrite(BUZZER, LOW);
    delay(50);
  }
}

void CYCLE(){
    while ( ( thermocouple.readCelsius() - offset ) <= 850 ){
    digitalWrite(SSR, LOW);
  }
  lastmillis1 = millis();
  while( ( millis() - lastmillis1 ) <= ( 2400L * 1000 ) ){
    if( ( thermocouple.readCelsius() - offset ) <= 850 ){
      digitalWrite(SSR, LOW);
      delay(100);
    }
    if( ( thermocouple.readCelsius() - offset ) > 850 ){
      digitalWrite(SSR, HIGH);
      delay(100);
    }
  }
  BEEP1();
  while ( digitalRead ( START_SW ) == HIGH ){
    if( ( thermocouple.readCelsius() - offset ) <= 700 ){
      digitalWrite(SSR, LOW);
      delay(100);
    }
    if( ( thermocouple.readCelsius() - offset ) > 700 ){
      digitalWrite(SSR, HIGH);
      delay(100);
    }
  }
  while ( ( thermocouple.readCelsius() - offset ) < Htemp ){
    Ctemp = thermocouple.readCelsius()- offset;
    delay(1000);
    if ( ( thermocouple.readCelsius() - offset - Ctemp ) < 1 ){
      digitalWrite(SSR, LOW);
    }
    else {
      digitalWrite(SSR, HIGH);
    }
  }
  lastmillis2 = millis();
  while( ( millis() - lastmillis2 ) <= ( 900L * 1000 ) ){
    if( ( thermocouple.readCelsius() - offset ) <= Htemp ){
      digitalWrite(SSR, LOW);
      delay(100);
    }
    else if( ( thermocouple.readCelsius() - offset ) > Htemp ){
      digitalWrite(SSR, HIGH);
      delay(100);
    }
  }
  BEEP2();
}

void BEEP1(){
  digitalWrite(BUZZER, HIGH);
  delay(600);
  digitalWrite(BUZZER, LOW);
  delay(50);
}

void BEEP2(){
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  delay(50);
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  delay(50);
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  delay(50);
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  delay(50);
  digitalWrite(BUZZER, HIGH);
  delay(200);
  digitalWrite(BUZZER, LOW);
  delay(50);
}

The purpose of the code is to rise the temperature to 850 degree and hold it for 4o minutes and rise again to desired temperature with 60 degrees per minute and hold it on Htemp for 15 minutes