Adjusted Code for Mixed Water tank
#include <NewPing.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int SvhPin = 2; // Pin for hot water solenoid valve (Svh)
const int SvcPin = 3; // Pin for cold water solenoid valve (Svc)
const int SvdPin = 4; // Pin for drain solenoid valve (Svd)
const int BuzzerPin = 5; // Pin for the buzzer
const int ThPin = 6; // Pin for DS18B20 hot water temperature sensor
const int TcPin = 7; // Pin for DS18B20 cold water temperature sensor
const int TtPin = 8; // Pin for DS18B20 temperature sensor in T tank
const int LevelTrigPin = 9; // Ultrasonic HC-SR04 sensor level trigger pin
const int LevelEchoPin = 10; // Ultrasonic HC-SR04 level sensor echo pin
const int KeypadPin = 11; // Pin for the keypad (Assuming you have a keypad library)
const float MAX_WATER_LEVEL = 15.0; // Maximum water level in T tank
const unsigned long DRAIN_TIME_MS = 10000; // 10 seconds in milliseconds
const unsigned long FILL_TIME_MS = 600000; // 10 minutes in milliseconds
const float DESIRED_TEMPERATURE = 40.0; // Desired water temperature
float desiredTemperature; // Desired water temperature
bool hotWaterNeeded = false;
bool coldWaterNeeded = false;
OneWire oneWireTh(ThPin);
OneWire oneWireTc(TcPin);
OneWire oneWireTt(TtPin);
DallasTemperature thSensor(&oneWireTh);
DallasTemperature tcSensor(&oneWireTc);
DallasTemperature ttSensor(&oneWireTt);
NewPing levelSensor(LevelTrigPin, LevelEchoPin);
float getWaterLevel() {
unsigned int duration = levelSensor.ping_median(5); //Take multiple readings for accuracy
float distance = duration / US_ROUNDTRIP_CM;
float waterLevel = MAX_WATER_LEVEL - distance;
return max(0.0, waterLevel); //Ensure water level is non-negative
}
void setup() {
pinMode(SvhPin, OUTPUT);
pinMode(SvcPin, OUTPUT);
pinMode(SvdPin, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
pinMode(KeypadPin, INPUT);
// Initialize temperature sensors
thSensor.begin();
tcSensor.begin();
ttSensor.begin();
// Set desired temperature (you can get this from your keypad input)
desiredTemperature = 40.0; // Change to your desired temperature
// Open Svd to drain any residue for 10 seconds
openSolenoidValve(SvdPin, DRAIN_TIME_MS);