Repository navigation
Expand file tree
/
Copy pathThinkercad Code
More file actions
76 lines (61 loc) · 2.17 KB
/
Copy pathThinkercad Code
File metadata and controls
76 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Pin definitions
const int NC_LED = 8; // Mixer valves (on if redirect, off if normal flow)
const int NO_LED = 9; // Tank valves (On if water level(A4) is above 300)
const int tempPin = A5; // Temperature sensor
const int tempPotPin = A0; // Potentiometer for temperature setpoint
const int waterLevelPin = A4; // Potentiometer for water level
void setup() {
Serial.begin(9600);
Serial.println("System Booting");
// Set pin modes
pinMode(NC_LED, OUTPUT);
pinMode(NO_LED, OUTPUT);
// Initialize valves (both off)
digitalWrite(NC_LED, LOW);
digitalWrite(NO_LED, LOW);
delay(1000);
Serial.println("System Ready");
}
void loop() {
// Read temperature setpoint potentiometer
int tempPotValue = analogRead(tempPotPin);
// Calculate setpoint and system state
float setpoint = 15.0;
bool systemActive = false;
if(tempPotValue > 100) { // Dead zone for off position
systemActive = true;
// Map 101-1023 to 15-35°C
setpoint = map(tempPotValue, 101, 1023, 150, 350) / 10.0;
}
// Always read water level (even when system is inactive)
int waterLevelValue = analogRead(waterLevelPin);
bool waterLevelHigh = waterLevelValue > 300;
if(systemActive) {
// Read temperature
int sensorValue = analogRead(tempPin);
float voltage = sensorValue * (5.0 / 1023.0);
float tempC = (voltage - 0.5) * 100.0; // LM50 conversion
// Temperature control logic
if(tempC < setpoint) {
digitalWrite(NC_LED, HIGH); // Redirect water (Mixer valve)
} else {
digitalWrite(NC_LED, LOW); // Normal flow
}
// Water level control logic
digitalWrite(NO_LED, waterLevelHigh ? HIGH : LOW); // Tank valve
// Update Serial Monitor
Serial.print("Set: ");
Serial.print(setpoint, 1);
Serial.print("°C | Actual: ");
Serial.print(tempC, 1);
Serial.print("°C | Water: ");
Serial.print(waterLevelHigh ? "High" : "Low ");
Serial.print(" | State: ");
Serial.println(tempC < setpoint ? "Redirecting" : "Normal Flow");
} else { // System off
digitalWrite(NC_LED, LOW);
digitalWrite(NO_LED, LOW);
Serial.println("System OFF - Both valves closed");
}
delay(200);
}