I'm using an Arduino Mega 2560 and whenever I press the button to set the hot temperature, it doesn't work and the lcd first the default like display temperature and target threshold then the lcd prints setting hot threshold then threshold set then setting cold threshold then threshold set and it loops, then when i press the up or down button both of the temperature thresholds adds or subtracts the values, it is my first time using buttons for temperature control, usually i only use a potentiometer but this time i want to explore the use of buttons and in the future maybe i'll be connecting this to blynk software.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define ONE_WIRE_BUS 2 // DATA WIRE FOR TEMP SENSOR
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Assigning Pins for Arduino
// Button Pins
const int hot = 44;
const int cold = 46;
const int up = 50;
const int down = 48;
const int set = 52;
// Ultrasonic Sensor Pins
// Water Level Sensor
const int levelSenseTrig = 3;
const int levelSenseEcho = 4;
// Hot Valve Sensor
const int hotDispTrig = 5;
const int hotDispEcho = 6;
// Cold Water Valve Sensor
const int coldDispTrig = 7;
const int coldDispEcho = 8;
// Relay Pins
const int hotValve = 31; // Solenoid Valve for Hot Water
const int coldValve = 33; // Solenoid Valve for Cold Water
const int pump = 35; // Water Pump
const int heater = 37; // Heating Coil
const int compressor = 39; // Compressor
// Variables
float hotThreshold = 35;
float coldThreshold = 10;
int hotIncrement = 5;
int coldIncrement = 1;
// Distances Values in centimeter
int waterLevelMinimum = 10;
int waterLevelMaximum = 15;
int tripValve = 5;
// Values
bool setHotThreshold = false;
bool setColdThreshold = false;
// Addresses of 2 DS18B20s
uint8_t sensor1[8] = {0x28, 0x02, 0xE3, 0x79, 0x97, 0x00, 0x03, 0x6F}; // for Cold Sensor
uint8_t sensor2[8] = {0x28, 0x6E, 0x04, 0x94, 0x97, 0x0A, 0x03, 0x69}; // for Hot Sensor
void setup()
{
Serial.begin(9600);
// Initialize LCD
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
// Relay Pins
pinMode(hotValve, OUTPUT);
pinMode(coldValve, OUTPUT);
pinMode(pump, OUTPUT);
pinMode(heater, OUTPUT);
pinMode(compressor, OUTPUT);
// Button Pins
pinMode(hot, INPUT_PULLUP);
pinMode(cold, INPUT_PULLUP);
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
// Ultrasonic Sensor
pinMode(levelSenseTrig, OUTPUT);
pinMode(levelSenseEcho, INPUT);
pinMode(hotDispTrig, OUTPUT);
pinMode(hotDispEcho, INPUT);
pinMode(coldDispTrig, OUTPUT);
pinMode(coldDispEcho, INPUT);
sensors.begin();
}
void loop()
{
sensors.requestTemperatures();
float hotThermo = sensors.getTempCByIndex(0);
float coldThermo = sensors.getTempCByIndex(1);
updateLCD(hotThermo, coldThermo);
updateRelays(hotThermo, coldThermo);
adjustThresholds();
delay(500);
}
// Display LCD
void updateLCD(float coldThermo, float hotThermo)
{
lcd.setCursor(0, 0);
lcd.print("H:");
lcd.print(hotThermo);
lcd.print((char)223);
lcd.print("C T:");
lcd.print(hotThreshold);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("C:");
lcd.print(coldThermo);
lcd.print((char)223);
lcd.print("C T:");
lcd.print(coldThreshold);
lcd.print((char)223);
lcd.print("C");
}
// Relay Control
void updateRelays(float coldThermo, float hotThermo)
{
// Heater
if (hotThermo >= hotThreshold)
{
digitalWrite(heater, HIGH);
}
else
{
digitalWrite(heater, LOW);
}
// Compressor
if (coldThermo <= coldThreshold)
{
digitalWrite(compressor, HIGH);
}
else
{
digitalWrite(compressor, LOW);
}
// Water Pump
if (getUltrasonicDistance(levelSenseTrig, levelSenseEcho) >= waterLevelMaximum)
{
digitalWrite(pump, HIGH);
}
else if (getUltrasonicDistance(levelSenseTrig, levelSenseEcho) <= waterLevelMinimum)
{
digitalWrite(pump, LOW);
}
// Solenoid Valve for Hot Water
if (getUltrasonicDistance(hotDispTrig, hotDispEcho) <= tripValve)
{
digitalWrite(hotValve, HIGH);
}
else
{
digitalWrite(hotValve, LOW);
}
// Solenoid Valve for Cold Water
if (getUltrasonicDistance(coldDispTrig, coldDispEcho) <= tripValve)
{
digitalWrite(coldValve, HIGH);
}
else
{
digitalWrite(coldValve, LOW);
}
}
// Adjust Thresholds
void adjustThresholds()
{
if (digitalRead(hot) == HIGH)
{
setHotThreshold = true;
setColdThreshold = false;
lcd.clear();
lcd.print("Setting Hot Threshold");
delay(1000);
lcd.clear();
do {
if (digitalRead(up) == HIGH)
{
hotThreshold += hotIncrement;
delay(200);
}
if (digitalRead(down) == HIGH)
{
hotThreshold -= hotIncrement;
delay(200);
}
} while (digitalRead(set) == LOW);
setHotThreshold = false;
lcd.clear();
lcd.print("Threshold Set");
delay(1000);
lcd.clear();
}
if (digitalRead(cold) == HIGH)
{
setColdThreshold = true;
setHotThreshold = false;
lcd.clear();
lcd.print("Setting Cold Threshold");
delay(1000);
lcd.clear();
do {
if (digitalRead(up) == HIGH)
{
coldThreshold += coldIncrement;
delay(200);
}
if (digitalRead(down) == HIGH)
{
coldThreshold -= coldIncrement;
delay(200);
}
} while (digitalRead(set) == LOW);
setColdThreshold = false;
lcd.clear();
lcd.print("Threshold Set");
delay(1000);
lcd.clear();
}
}
// Compute Distance for Ultrasonic Sensor
float getUltrasonicDistance(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}