/*
Water Temperature Control System
This Arduino code controls a water temperature control system using a DC motor and an L298 H-bridge motor driver.
The system aims to maintain the water temperature at a desired setpoint, adjustable through a potentiometer.
Hardware Components:
- Arduino Uno
- DS18B20 Temperature Sensor
- L298 H-Bridge Motor Controller
- Geared DC Motor
- Two 1k Potentiometers (One for temperature setpoint, one for valve position)
- LEDs for status indication
Safety Feature:
- During a warm-up period (adjustable via warmupDuration), the motor remains off and LEDs indicating temperature control and valve position are also off to prevent hot water delivery at startup.
Wiring:
- Follow the hardware wiring instructions for your specific setup.
Authors:
- [Your Name]
Date:
- [Date]
License:
- This code is released under the [License Name] license.
*/
#include <OneWire.h>
#include <DallasTemperature.h>
// Constants for pin assignments and system parameters
#define ONE_WIRE_BUS 2
#define motorEnablePin 3
#define motorDirectionPin1 4
#define motorDirectionPin2 5
#define potPin A0
#define valvePotPin A1
#define ledPinTempControl 6
#define ledPinValvePos 7
#define ledPinTolerance 8
const int maxTemperature = 50;
const int maxValveAngle = 90;
const int initialValveAngle = 0;
const int temperatureTolerance = 2;
const unsigned long warmupDuration = 30000; // Warm-up duration in milliseconds (30 seconds)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int desiredTemperature;
int currentTemperature;
int valvePosition;
unsigned long warmupStartTime = 0;
bool warmupComplete = false;
void setup() {
pinMode(motorEnablePin, OUTPUT);
pinMode(motorDirectionPin1, OUTPUT);
pinMode(motorDirectionPin2, OUTPUT);
pinMode(ledPinTempControl, OUTPUT);
pinMode(ledPinValvePos, OUTPUT);
pinMode(ledPinTolerance, OUTPUT);
sensors.begin();
sensors.requestTemperatures();
analogWrite(motorEnablePin, map(initialValveAngle, 0, maxValveAngle, 0, 255));
warmupStartTime = millis();
Serial.begin(9600);
}
void loop() {
if (!warmupComplete && (millis() - warmupStartTime >= warmupDuration)) {
warmupComplete = true;
}
valvePosition = map(analogRead(valvePotPin), 0, 1023, 0, maxValveAngle);
sensors.requestTemperatures();
currentTemperature = sensors.getTempCByIndex(0);
desiredTemperature = map(valvePosition, 0, maxValveAngle, 20, maxTemperature);
int temperatureDifference = currentTemperature - desiredTemperature;
if (warmupComplete) {
int directionPin1 = LOW;
int directionPin2 = HIGH;
if (temperatureDifference > 0) {
directionPin1 = HIGH;
directionPin2 = LOW;
}
digitalWrite(motorDirectionPin1, directionPin1);
digitalWrite(motorDirectionPin2, directionPin2);
analogWrite(motorEnablePin, map(valvePosition, 0, maxValveAngle, 0, 255));
controlLEDs(temperatureDifference);
} else {
analogWrite(motorEnablePin, 0);
controlLEDs(0);
}
if (desiredTemperature > maxTemperature) {
desiredTemperature = maxTemperature;
}
Serial.print("Desired Temperature: ");
Serial.print(desiredTemperature);
Serial.print("°C - Current Temperature: ");
Serial.print(currentTemperature);
Serial.print("°C - Valve Position: ");
Serial.print(valvePosition);
Serial.println(" degrees");
delay(1000);
}
void controlLEDs(int temperatureDifference) {
digitalWrite(ledPinTempControl, temperatureDifference > temperatureTolerance);
digitalWrite(ledPinValvePos, temperatureDifference <= temperatureTolerance);
digitalWrite(ledPinTolerance, temperatureDifference <= temperatureTolerance);
}