/*
Forum: https://forum.arduino.cc/t/code-check-please/1240893/2
Wokwi: https://wokwi.com/projects/393602101966308353
Sketch from TO at 2024/03/28
Modified by ec2021
*/
// C++ code
enum States {UNKNOWN, LEVELLOW, LEVELNORMAL, LEVELHIGH};
States state = LEVELNORMAL;
States lastState = UNKNOWN;
const unsigned long waitBeforePumpTwo = 30000;
const int potentiometerPin = A5;
const int PUMP1_PIN = 9;
const int PUMP2_PIN = 8;
int PrimaryPump = 0; // Assuming 0 for Pump 1, 1 for Pump 2 (can be modified in loop)
const int HIGH_LED_PIN = 7;
const int LOW_LED_PIN = 6;
const int ALARM_PIN = 12;
int primaryPumpPin;
int secondaryPumpPin;
boolean flashRed = false;
boolean secondPumpOn = false;
unsigned long levelHighStartTime;
void setup() {
Serial.begin(115200);
pinMode(PUMP1_PIN, OUTPUT); // Pump 1 Output;
pinMode(PUMP2_PIN, OUTPUT); // Pump 2 Output;
pinMode(HIGH_LED_PIN, OUTPUT); // Red LED to indicate high water level;
pinMode(LOW_LED_PIN, OUTPUT); // Green LED to indicate Low water level;
pinMode(ALARM_PIN, OUTPUT); // Alarm buzzer
// Set pump pins based on PrimaryPump (can be adjusted in loop if needed)
primaryPumpPin = PrimaryPump == 0 ? PUMP1_PIN : PUMP2_PIN;
secondaryPumpPin = PrimaryPump == 0 ? PUMP2_PIN : PUMP1_PIN;
}
void loop() {
checkLevel();
stateMachine();
flashRedLed();
}
void checkLevel() {
int waterLevel = analogRead(potentiometerPin);
// Handle Low Water Level
switch (waterLevel) {
case 0 ... 200 :
state = LEVELLOW;
break;
case 201 ... 799 :
state = LEVELNORMAL;
break;
case 800 ... 1023:
state = LEVELHIGH;
break;
default:
Serial.println("Error") ;
}
}
void stateMachine() {
switch (state) {
case LEVELLOW:
lowLevel();
break;
case LEVELNORMAL:
normalLevel();
break;
case LEVELHIGH:
highLevel();
break;
}
}
void lowLevel() {
if (lastState != LEVELLOW) {
Serial.println("LOW");
flashRed = false;
digitalWrite(ALARM_PIN, LOW); // Alarm Off
digitalWrite(LOW_LED_PIN, HIGH); // Green LED ON
digitalWrite(HIGH_LED_PIN, LOW); // Red LED Off
digitalWrite(primaryPumpPin, LOW); // Stop Pump (assuming any pump was running)
digitalWrite(secondaryPumpPin, LOW);
secondPumpOn = false;
lastState = state;
}
}
void normalLevel() {
// Normal Level - Turn off LEDs and Alarm
if (lastState != LEVELNORMAL) {
Serial.println("NORMAL");
flashRed = false;
digitalWrite(ALARM_PIN, LOW); // Alarm Off
digitalWrite(LOW_LED_PIN, LOW); // Green LED Off
digitalWrite(HIGH_LED_PIN, LOW); // Red LED Off
lastState = state;
}
}
void highLevel() {
// High Level Detected - Red LED ON and activate Pump sequence
if (lastState != LEVELHIGH) {
Serial.println("HIGH");
flashRed = false;
digitalWrite(ALARM_PIN, HIGH); // Alarm On
digitalWrite(LOW_LED_PIN, LOW); // Green LED Off
digitalWrite(HIGH_LED_PIN, HIGH); // Red LED On
digitalWrite(primaryPumpPin, HIGH); // Start Pump 1
levelHighStartTime = millis(); // Store the start time of LEVELHIGH here
lastState = state;
}
// Check if waitBeforePumpTwo has expired after start of the first pump
// If it has expired and pump 2 is off switch it on
if (millis() - levelHighStartTime >= waitBeforePumpTwo && !secondPumpOn) {
startSecondPump();
}
// The following part is not required but nice for this demo ...
static unsigned long lastPrint = 0;
if (millis() - lastPrint >= 1000 && !secondPumpOn) {
lastPrint = millis();
Serial.print(".");
}
}
void startSecondPump() {
// and flash Red Led
Serial.println("Start Second Pump");
flashRed = true;
secondPumpOn = true;
digitalWrite(ALARM_PIN, HIGH);
digitalWrite(secondaryPumpPin, HIGH); // Start Pump 2
}
void flashRedLed() {
static unsigned long lastChange = 0;
static byte ledState = HIGH;
if (!flashRed) {
return;
}
if (millis() - lastChange > 300) {
lastChange = millis();
digitalWrite(HIGH_LED_PIN, ledState);
ledState = !ledState;
}
}
Pump 1
Pump 2
Alarm
High Level
Low Level
Level Sim