// Program: WaterLevelWarning-E_v3.ino
// Wokwi: https://wokwi.com/projects/376766975385873409
// Use GPIO36 (VP)
const int sensorPin = 36; // define input pin for the
// tank level sensor
const int sensorMin = 0; // sensor minimum
const int sensorMax = 4095; // sensor maximum
void setup() {
// initialize serial communication
Serial.begin(115200);
}
void loop() {
int sensorReading, waterLevel;
// read the sensor value
// (4095 - 0) * 3/4 + 1 = 3071 + 1 = 3072
// 0-1023 -> 0, 1024-2047 -> 1, 2048-3071 -> 2, 3072-4095 -> 3
sensorReading = analogRead(sensorPin);
Serial.print(sensorReading);
Serial.print(", ");
// scale the sensor range to a range of four water levels (0 ~ 3)
waterLevel = map(sensorReading, sensorMin, (3 * sensorMax + sensorMin) / 4 + 1, 0, 3);
Serial.print(waterLevel);
Serial.print(", ");
// print something different depending on the scaled water level
switch (waterLevel) {
case 0: Serial.println("Water Level Nearly OUT");
break;
case 1: Serial.println("Water Level Low");
break;
case 2: Serial.println("Water Level Medium");
break;
case 3: Serial.println("Water Level Nearly FULL");
break;
}
}