# Problem Using Buttons to Control my Temperature Controller

**URL:** <https://forum.arduino.cc/t/problem-using-buttons-to-control-my-temperature-controller/662536>\
**Category:** Programming\
**Created:** [June 25, 2020, 11:28am UTC](https://forum.arduino.cc/t/problem-using-buttons-to-control-my-temperature-controller/662536 "2020-06-25T11:28:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dabliz](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@dabliz](https://forum.arduino.cc/u/dabliz)\
**Post date:** [June 25, 2020, 11:28am UTC](https://forum.arduino.cc/t/problem-using-buttons-to-control-my-temperature-controller/662536/1 "2020-06-25T11:28:23Z")

</div>

Hi,  
Please, i am very new in Arduino as well as Programming, i am working on a temperature controller than uses two buttons. The buttons are used to increase or decrease the working temperature range of the controller. if the temperature range set by pressing the button is different from the sensor temperature reading, the controller would take certain actions.

I have tried this in two ways but NOT working;

1. When i use the void loop for all the codes, the buttons would work very slow, infact, you have to press and hold before it would work and sometimes doesn't work at all
2. when i tried creating another function outside the void loop function. (i.e.) i used the void loop for the buttons alone and the rest of the codes on another loop outside the void loop. the buttons would work okay but the other function loop will not work

Below is the code for the first (1) scenario..

#include \<LiquidCrystal.h\>  
#include \<dht.h\>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;  
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

dht DHT;

#define DHT11\_PIN A0

#define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE);

int ledRed = 8;  
int ledGreen = 9;  
int ledYellow = 10;  
int Buzzer = 6;  
int frequency = 50;  
int buttonPin1 = 7;  
int buttonPin2 = 13;  
int activeButtonstate1;  
int activeButtonstate2;  
int stndtemp = 0;  
int range = 2;  
int heat = A1;  
int fan = A2;

void setup(){  
//Initialize Serial Monitor  
Serial.begin(9600);

//Setting Up INPUT and OUTPUT Pins  
pinMode(buttonPin1, INPUT);  
pinMode(buttonPin2, INPUT);  
pinMode(Buzzer, OUTPUT);  
pinMode(ledRed, OUTPUT);  
pinMode(ledYellow, OUTPUT);  
pinMode(ledGreen, OUTPUT);  
digitalWrite(buttonPin1,LOW);  
digitalWrite(buttonPin2,LOW);

//Initializing LCD  
lcd.begin(16, 2);  
// Print a message to the LCD.  
lcd.print("Set Your Control");  
lcd.setCursor(0,1);  
lcd.print("Temp In Degree C");  
delay(2000);  
lcd.clear();  
}

void loop(){  
//Declaring Variables  
int chk = DHT.read11(DHT11\_PIN);  
int temperature = DHT.temperature;  
int humidity = DHT.humidity;  
int buttonState1 = digitalRead(buttonPin1);  
int buttonState2 = digitalRead(buttonPin2);  
int celsius = ((((5.0/1023.0)\*temperature)\*100) \* 2);

//Prints the Sensor Reading on LCD  
lcd.setCursor(0,0);  
lcd.print("Sys Temp:"); //  
lcd.print(celsius);  
lcd.print((char)223);  
lcd.print("C");  
delay(500);

//I WOULD USE THE BUTTONS TO SET THE TEMPERATURE RANGE THE SYSTEM SHOULD WORK ON  
//If Button 1 is Pressed Increase Set-Point Temperature Value by 1  
if (buttonState1 == HIGH){  
if (stndtemp\<150){  
stndtemp++;  
}  
}  
//If Button 2 is Pressed Decrease Set-Point Temperature Value by 1  
if (buttonState2 == HIGH){  
if (stndtemp\>0){  
stndtemp--;  
}  
}

//Display the Set point Temperature on the LCD for Me to see After the Buttons have been Pressed  
lcd.setCursor(0,1);  
lcd.print("Set Temp:");  
lcd.print(stndtemp);  
lcd.print((char)223);  
lcd.print("C");  
delay(500);

//Use the Lower Limit and Upper Limit Working Temperature on LCD  
lcd.setCursor(0, 1);  
lcd.print("LT= "); //Lower Limit  
lcd.print(stndtemp); //My Set-Point Temperature is the Lower Limit  
lcd.print(" HT= "); //Upper Limit  
lcd.print(stndtemp + range); //My Upper Limit Temperature is the Set-Point + a certain value (range)

//---------THESE ACTIONS SHOULD ONLY EXECUTE AFTER THE BUTTONS HAVE BEEN PRESSED -----------------  
// -------- AND THE LOOP SHOULD RETURN FROM HERE UNTIL THE BUTTON IS PRESSED AGAIN -------

//Compare the Temperature I have Set (Set-Point) to the Temperature Reading of the Environment  
//and then Take an Action if there is any difference between them

if (celsius \< stndtemp){  
digitalWrite(ledRed,HIGH); //Turn on Red LED  
delay(1000);  
digitalWrite(heat, HIGH); //Turn on the Heater  
digitalWrite(fan, LOW); //Turn off the Fan  
tone(Buzzer,1000); //Turn on Alarm  
delay(500);  
noTone(Buzzer); //  
}

else if (celsius \> (stndtemp+range)){  
digitalWrite(ledYellow,HIGH); //Turn on Yellow LED  
delay(1000);  
digitalWrite(heat, LOW); //Turn Off the Heater  
digitalWrite(fan, HIGH); //Turn On the Fan  
tone(Buzzer,1000); //Turn on Alarm  
delay(1000);  
noTone(Buzzer);  
lcd.print("Fan is On"); //Print on the LCD  
delay(500);  
}  
else {  
digitalWrite(ledGreen, HIGH); //Turn on Green LED  
delay(1000);  
lcd.print("Temp is OKAY"); //Print on the LCD  
delay(500);  
}  
return;  
}

Please, i really need help am stocked here

---

<div class="post-metadata">

**Author:** ![GypsumFantastic](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gypsumfantastic/32/236274_2.png) [@GypsumFantastic](https://forum.arduino.cc/u/GypsumFantastic)\
**Post date:** [June 25, 2020, 11:52am UTC](https://forum.arduino.cc/t/problem-using-buttons-to-control-my-temperature-controller/662536/2 "2020-06-25T11:52:24Z")

</div>

You've got a lot of delay() statements in your loop there. You know nothing else can happen during during a delay(), yeah?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [June 25, 2020, 11:52am UTC](https://forum.arduino.cc/t/problem-using-buttons-to-control-my-temperature-controller/662536/3 "2020-06-25T11:52:57Z")

</div>

Welcome to the Forum. Please read these two posts:  
[  
General Guidance and How to use the Forum](https://forum.arduino.cc/index.php?topic=652233.0) and  
[Read this before posting a programming question ...](https://forum.arduino.cc/index.php?topic=97455.0)  
You may also find useful information that would answer your question here:  
[Useful links - check here for reference posts / tutorials](https://forum.arduino.cc/index.php?topic=384198.0)

You have posted code without using code tags. The code tags make the code look

```cpp
like this

```

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "\</\>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:36am UTC](https://forum.arduino.cc/t/problem-using-buttons-to-control-my-temperature-controller/662536/4 "2021-05-06T00:36:54Z")

</div>


