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;
- 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
- 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