"break" position

Hello, i've done a simple project, making temperature controlled relay.

the parts i used :

arduino uno
lm35
5V DC Relay
lcd 16x2
5V DC heater
3 push buttons, up down to set temperature, start button to start the program.

the system works perfectly.

But i want to modify this project by adding cooler. so i can choose between heating or cooling that connected to 1 relay. i put switch between heater and cooler, so i can switch manually, and no need more relay.

the problem is i've tried to modify the sketch, by adding one more push button as a selection to run program of cooler. But i confuse where i put the "break" function to the sketch because it keep looping.

please help.

here is the code

#include <LiquidCrystal.h>
int reading = 0;
int sensorPin = A0;
int relay =8;
float celsius ;
int Tempset = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
 
void setup() {
    Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
 pinMode(relay,OUTPUT);digitalWrite(8,HIGH);
 pinMode(9, INPUT);digitalWrite(9,HIGH);   // pin up
 pinMode(10, INPUT);digitalWrite(10,HIGH);  // pin down
 pinMode(12, INPUT);digitalWrite(12,HIGH);   // pin to trigger heating control
 pinMode(13, INPUT);digitalWrite(13,HIGH);  // pin to trigger cooling control 
 
 lcd.clear();
 delay (200);
 lcd.setCursor (0,0);
 lcd.print("   Heating &     ");
 lcd.setCursor(0,1);
 lcd.print("Cooling Control");
 delay(2000);
 lcd.clear();
}
 
void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.begin(16, 2);
  lcd.setCursor (0,0);
 lcd.print("Set");
 lcd.setCursor(0,1);
 lcd.print("Temperature");
 delay(2000);

 // lcd.scrollDisplayLeft();
 // wait a bit:

 if(digitalRead(9)==LOW||digitalRead(10)==LOW)
{delay(200);
  
    while(1)
  {
    if(digitalRead(9)==LOW){Tempset++;}
    if(digitalRead(10)==LOW){Tempset--;}
    if(digitalRead(12)==LOW){delay(200);heater();}
    if(digitalRead(13)==LOW){delay(200);cooler();}
    lcd.begin(16, 2);lcd.print("Set Temp");
    lcd.setCursor(4, 2);lcd.print(Tempset);
    delay(200);
  }
    }
}
void heater()
{

 reading = analogAvg(sensorPin);
 celsius= ((5.0*reading)*100/1024);
 if(celsius < Tempset )
  digitalWrite(8,LOW);
else if( celsius  > Tempset+1)
  digitalWrite(8,HIGH);
 
 lcd.begin(16, 2);lcd.print("Temp");
  lcd.setCursor(12,2);
  lcd.print(celsius);
  lcd.print("C");
  delay(1000);
 }
void cooler()
{

 reading = analogAvg(sensorPin);
 celsius= ((5.0*reading)*100/1024);
 if(celsius > Tempset )
  digitalWrite(8,LOW);
else if( celsius  < Tempset-1 )
  digitalWrite(8,HIGH);
 
 lcd.begin(16, 2);lcd.print("Temp");
  lcd.setCursor(12,2);
  lcd.print(celsius);
  lcd.print("C");
  delay(1000);
 }

 
 int analogAvg (int sensorPin)
 {
 unsigned int total=0;
 for(int n=0; n<Tempset; n++ )
 total += analogRead (sensorPin);
 return total/Tempset;
 }

Hi,

Could you explain what you mean with looping.

You have two infinite loops, one inside the other:

void loop() {
  //Some code
  while(1) {
     //More code
  }
}

Do you mean that you want to return at "Some code" at one point?

we tend not to run code directly especially using a "while" loop as its easy to enter a while loop it becomes more complicated to exit it.

a better idea would be to make a pointer then have the pointer decide which code should be run. This avoids a while loop and allows the main loop to run all the time like its designed to do.

changed some code and cleaned up some parts. It compiles but probably doesn't work. Try to read it and ask questions about parts that make no sense

#define relay 8//tells compiler that word relay means 8

#include <LiquidCrystal.h>
//int reading = 0; should be a float but not required as global
int sensorPin = A0;
float celsius ;
float Tempset = 0;//needs to be a float 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  pinMode(relay, OUTPUT); digitalWrite(relay, HIGH);
   // pinMode(relay, OUTPUT); digitalWrite(8, HIGH); no idea why we started with relay
   //then went back to saying pin 8 
  //pinMode(9, INPUT); digitalWrite(9, HIGH); // pin up
  //pinMode(10, INPUT); digitalWrite(10, HIGH); // pin down
  // pinMode(12, INPUT); digitalWrite(12, HIGH); // pin to trigger heating control
  // pinMode(13, INPUT); digitalWrite(13, HIGH); // pin to trigger cooling control

  pinMode(9, INPUT_PULLUP);//button common to negative
  pinMode(10, INPUT_PULLUP);//button common to negative
  pinMode(12, INPUT_PULLUP);//button common to negative
  pinMode(13, INPUT_PULLUP);//button common to negative

  lcd.clear();
  delay (200);
  lcd.setCursor (0, 0);
  lcd.print("   Heating &     ");
  lcd.setCursor(0, 1);
  lcd.print("Cooling Control");
  delay(2000);
  lcd.clear();
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

  byte upButton = digitalRead(10);// no one remembers pin numbers use names
  byte downButton = digitalRead(9);// no one remembers pin numbers use names
  byte requestHeaterButton = digitalRead(12);// no one remembers pin numbers use names
  byte requestCollerButton =  digitalRead(13);// no one remembers pin numbers use names
  static byte  heaterRequest ;// static tell compiler not to forget the status 
  static byte  coolingRequest;// static tell compiler not to forget the status 
  static byte  setPointDisplay;// static tell compiler not to forget the status 
  static byte prevSetPointDisplay;// static tell compiler not to forget the status 



  if (setPointDisplay != prevSetPointDisplay) {//added to clear display
    lcd.clear();
  }
  prevSetPointDisplay = setPointDisplay;


  if (setPointDisplay == 0) {
    lcd.begin(16, 2);
    lcd.setCursor (0, 0);
    lcd.print("Set temp");
    lcd.setCursor(0, 1);
    lcd.print("setpoint= ");
    lcd.print(Tempset, 1); //display to .1 degrees
    delay(2000);
  }


  // lcd.scrollDisplayLeft();
  // wait a bit:

  if (upButton == LOW) {//bad way to code as it doesnt deal with debounce
    Tempset++;
    heaterRequest = 0;//pointers
    coolingRequest = 0;//pointers
    setPointDisplay = 0;//pointers
  }
  if (downButton == LOW) {
    Tempset--;
    heaterRequest = 0;
    coolingRequest = 0;
    setPointDisplay = 0;
  }
  if (requestHeaterButton == LOW) {
    delay(200);
    heaterRequest = 1;//heater selected no need for setpoint display
    coolingRequest = 0;
    setPointDisplay = 1;
  }
  if (requestCollerButton == LOW) {
    delay(200);
    heaterRequest = 0;
    coolingRequest = 1;//cooler selected no need for setpoint display
    setPointDisplay = 1;
  }

  if (setPointDisplay == 1) {//done with setpoints screen
    if (coolingRequest == 1) {
      cooler();
    }
    if (heaterRequest == 1) {
      heater();
    }
  }


  // if (digitalRead(9) == LOW || digitalRead(10) == LOW)
  // { delay(200);
  //
  //   while (1)//while has a use this isnt it
  //   {
  //     if (digitalRead(9) == LOW) {
  //      Tempset++;
  //    }
  //    if (digitalRead(10) == LOW) {
  //      Tempset--;
  //    }
  //    if (digitalRead(12) == LOW) {
  //       delay(200);
  //      heater();
  //   }
  //   if (digitalRead(13) == LOW) {
  //     delay(200);
  //     cooler();
  //   }
  //    lcd.begin(16, 2); lcd.print("Set Temp");
  //   lcd.setCursor(4, 2); lcd.print(Tempset);
  //   delay(200);
  //  }
  // }

}//end loop



void heater()
{
  getTemp();
  if (celsius < Tempset )
    digitalWrite(relay, LOW);//same relay as the cooler?
  else if ( celsius  > Tempset + 1)
    digitalWrite(relay, HIGH);

  lcd.begin(16, 2); lcd.print("Temp");
  lcd.setCursor(12, 2);
  lcd.print(celsius, 1);
  lcd.print("C");
  //would be wise to display heating or cooling
  delay(1000);
}
void cooler()
{

  getTemp();
  if (celsius > Tempset )
    digitalWrite(relay, LOW);//same relay as the heater?
  else if ( celsius  < Tempset - 1 )
    digitalWrite(relay, HIGH);

  lcd.begin(16, 2); lcd.print("Temp");
  lcd.setCursor(12, 2);
  lcd.print(celsius, 1);
  lcd.print("C");
  //would be wise to display heating or cooling
  delay(1000);
}

// this makes no sense it looks like it will average readings at some point
// but its doing it for tempset rather than the anologe reading.

//int analogAvg (int sensorPin)
//{
//  unsigned int total = 0;
// for (int n = 0; n < Tempset; n++ )//for loop for one time?
//   total += analogRead (sensorPin);
// return total / Tempset;
//}

void getTemp() {
  //readind doesnt need to be a static or global as its only used 
  //during the calculation then it can be forgottten
 float reading = analogRead(sensorPin);//wake pin up
  reading = analogRead(sensorPin);//read pin
  
  // could read pin 10 times adding results then divide the by 10

  celsius = ((5.0 * reading) * 100 / 1024);//global float update
}

jbellavance:
Hi,

Could you explain what you mean with looping.

You have two infinite loops, one inside the other:

void loop() {

//Some code
  while(1) {
    //More code
  }
}


Do you mean that you want to return at "Some code" at one point?

example when i press pin 13 button to trigger cooling program to run, the program do run but the display is back to "Set Temperature"

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.begin(16, 2);
  lcd.setCursor (0,0);
 lcd.print("Set");
 lcd.setCursor(0,1);
 lcd.print("Temperature");
 delay(2000);
if(digitalRead(9)==LOW||digitalRead(10)==LOW)
{delay(200);

while(1)
  {
    if(digitalRead(9)==LOW){Tempset++;}
    if(digitalRead(10)==LOW){Tempset--;}
    if(digitalRead(12)==LOW){delay(200);heater();}
    if(digitalRead(13)==LOW){delay(200);cooler();}
    lcd.begin(16, 2);lcd.print("Set Temp");
    lcd.setCursor(4, 2);lcd.print(Tempset);
delay(200);
  }
    }
}

gpop1:
we tend not to run code directly especially using a "while" loop as its easy to enter a while loop it becomes more complicated to exit it.

a better idea would be to make a pointer then have the pointer decide which code should be run. This avoids a while loop and allows the main loop to run all the time like its designed to do.

changed some code and cleaned up some parts. It compiles but probably doesn't work. Try to read it and ask questions about parts that make no sense

#define relay 8//tells compiler that word relay means 8

#include <LiquidCrystal.h>
//int reading = 0; should be a float but not required as global
int sensorPin = A0;
float celsius ;
float Tempset = 0;//needs to be a float
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  pinMode(relay, OUTPUT); digitalWrite(relay, HIGH);
  // pinMode(relay, OUTPUT); digitalWrite(8, HIGH); no idea why we started with relay
  //then went back to saying pin 8
  //pinMode(9, INPUT); digitalWrite(9, HIGH); // pin up
  //pinMode(10, INPUT); digitalWrite(10, HIGH); // pin down
  // pinMode(12, INPUT); digitalWrite(12, HIGH); // pin to trigger heating control
  // pinMode(13, INPUT); digitalWrite(13, HIGH); // pin to trigger cooling control

pinMode(9, INPUT_PULLUP);//button common to negative
  pinMode(10, INPUT_PULLUP);//button common to negative
  pinMode(12, INPUT_PULLUP);//button common to negative
  pinMode(13, INPUT_PULLUP);//button common to negative

lcd.clear();
  delay (200);
  lcd.setCursor (0, 0);
  lcd.print("  Heating &    ");
  lcd.setCursor(0, 1);
  lcd.print("Cooling Control");
  delay(2000);
  lcd.clear();
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

byte upButton = digitalRead(10);// no one remembers pin numbers use names
  byte downButton = digitalRead(9);// no one remembers pin numbers use names
  byte requestHeaterButton = digitalRead(12);// no one remembers pin numbers use names
  byte requestCollerButton =  digitalRead(13);// no one remembers pin numbers use names
  static byte  heaterRequest ;// static tell compiler not to forget the status
  static byte  coolingRequest;// static tell compiler not to forget the status
  static byte  setPointDisplay;// static tell compiler not to forget the status
  static byte prevSetPointDisplay;// static tell compiler not to forget the status

if (setPointDisplay != prevSetPointDisplay) {//added to clear display
    lcd.clear();
  }
  prevSetPointDisplay = setPointDisplay;

if (setPointDisplay == 0) {
    lcd.begin(16, 2);
    lcd.setCursor (0, 0);
    lcd.print("Set temp");
    lcd.setCursor(0, 1);
    lcd.print("setpoint= ");
    lcd.print(Tempset, 1); //display to .1 degrees
    delay(2000);
  }

// lcd.scrollDisplayLeft();
  // wait a bit:

if (upButton == LOW) {//bad way to code as it doesnt deal with debounce
    Tempset++;
    heaterRequest = 0;//pointers
    coolingRequest = 0;//pointers
    setPointDisplay = 0;//pointers
  }
  if (downButton == LOW) {
    Tempset--;
    heaterRequest = 0;
    coolingRequest = 0;
    setPointDisplay = 0;
  }
  if (requestHeaterButton == LOW) {
    delay(200);
    heaterRequest = 1;//heater selected no need for setpoint display
    coolingRequest = 0;
    setPointDisplay = 1;
  }
  if (requestCollerButton == LOW) {
    delay(200);
    heaterRequest = 0;
    coolingRequest = 1;//cooler selected no need for setpoint display
    setPointDisplay = 1;
  }

if (setPointDisplay == 1) {//done with setpoints screen
    if (coolingRequest == 1) {
      cooler();
    }
    if (heaterRequest == 1) {
      heater();
    }
  }

// if (digitalRead(9) == LOW || digitalRead(10) == LOW)
  // { delay(200);
  //
  //  while (1)//while has a use this isnt it
  //  {
  //    if (digitalRead(9) == LOW) {
  //      Tempset++;
  //    }
  //    if (digitalRead(10) == LOW) {
  //      Tempset--;
  //    }
  //    if (digitalRead(12) == LOW) {
  //      delay(200);
  //      heater();
  //  }
  //  if (digitalRead(13) == LOW) {
  //    delay(200);
  //    cooler();
  //  }
  //    lcd.begin(16, 2); lcd.print("Set Temp");
  //  lcd.setCursor(4, 2); lcd.print(Tempset);
  //  delay(200);
  //  }
  // }

}//end loop

void heater()
{
  getTemp();
  if (celsius < Tempset )
    digitalWrite(relay, LOW);//same relay as the cooler?
  else if ( celsius  > Tempset + 1)
    digitalWrite(relay, HIGH);

lcd.begin(16, 2); lcd.print("Temp");
  lcd.setCursor(12, 2);
  lcd.print(celsius, 1);
  lcd.print("C");
  //would be wise to display heating or cooling
  delay(1000);
}
void cooler()
{

getTemp();
  if (celsius > Tempset )
    digitalWrite(relay, LOW);//same relay as the heater?
  else if ( celsius  < Tempset - 1 )
    digitalWrite(relay, HIGH);

lcd.begin(16, 2); lcd.print("Temp");
  lcd.setCursor(12, 2);
  lcd.print(celsius, 1);
  lcd.print("C");
  //would be wise to display heating or cooling
  delay(1000);
}

// this makes no sense it looks like it will average readings at some point
// but its doing it for tempset rather than the anologe reading.

//int analogAvg (int sensorPin)
//{
//  unsigned int total = 0;
// for (int n = 0; n < Tempset; n++ )//for loop for one time?
//  total += analogRead (sensorPin);
// return total / Tempset;
//}

void getTemp() {
  //readind doesnt need to be a static or global as its only used
  //during the calculation then it can be forgottten
float reading = analogRead(sensorPin);//wake pin up
  reading = analogRead(sensorPin);//read pin
 
  // could read pin 10 times adding results then divide the by 10

celsius = ((5.0 * reading) * 100 / 1024);//global float update
}

Thank you so much, you really make it detail, God bless you!
Sorry for late answer, your code make sense, but i don't try it yet, will definitely notify you soon. thank you again bro!