/*
Wokwi | questions
*Project Link:** https://wokwi.com/projects/448477356190265345
Maureen — 12:40 AM Monday, November 24, 2025
help me debug
*/
#define TDS_PIN 32
#define TURB_PIN 33
#define TEMP_PIN 35
#define FLOW_PIN 14
#define LEVEL_TRIG 19
#define LEVEL_ECHO 18
#define VALVE_PIN 17
#define PUMP_PIN 16
volatile int flowPulseCount = 0;
void IRAM_ATTR flowCounter() {
flowPulseCount++;
}
void setup() {
Serial.begin(115200);
pinMode(FLOW_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(FLOW_PIN), flowCounter, RISING);
pinMode(LEVEL_TRIG, OUTPUT);
pinMode(LEVEL_ECHO, INPUT);
pinMode(PUMP_PIN, OUTPUT);
pinMode(VALVE_PIN, OUTPUT);
}
float readLevel() {
digitalWrite(LEVEL_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(LEVEL_TRIG, LOW);
long duration = pulseIn(LEVEL_ECHO, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}
void loop() {
// Read sensors
int tempRaw = analogRead(TEMP_PIN);
float tempC = map(tempRaw, 0, 4095, 10, 60);
int tdsRaw = analogRead(TDS_PIN);
float tdsValue = map(tdsRaw, 0, 4095, 0, 2000);
int turbRaw = analogRead(TURB_PIN);
float turbidity = map(turbRaw, 0, 4095, 0, 100);
float flowRate = flowPulseCount * 0.45;
flowPulseCount = 0;
float level = readLevel();
Serial.println("------ HYBRID SOLAR RO SIMULATION ------");
Serial.print("Temperature (°C): "); Serial.println(tempC);
Serial.print("TDS (ppm): "); Serial.println(tdsValue);
Serial.print("Turbidity (NTU): "); Serial.println(turbidity);
Serial.print("Flow Rate (L/h): "); Serial.println(flowRate);
Serial.print("Tank Level (cm): "); Serial.println(level);
// Control Logic
if (tdsValue > 500 || turbidity > 5) {
digitalWrite(VALVE_PIN, HIGH);
Serial.println("⚠ Water unsafe! Diverting output...");
} else {
digitalWrite(VALVE_PIN, LOW);
}
if (level < 5) {
digitalWrite(PUMP_PIN, LOW);
Serial.println("⚠ Tank empty - Pump OFF");
} else {
digitalWrite(PUMP_PIN, HIGH);
Serial.println("Pump ON");
}
delay(1000);
}
Temp
TDS
Turbidity
Flow
Valve
Pump