/* This is a program to control the water level in a sump.
Using two pumps to remove water in conjunction with a level sensor.*/
#include <elapsedMillis.h> // Library to control Process Timer
#include <LowPower.h> // Library to control Sleep/idle Mode
int LevelSensor = A0; // Potentiometer Level Sensor Input
int Pump1 = 2; // Pump 1 Output
int Pump2 = 3; // Pump 2 Output
int Alarm = 8; // Alarm Output
boolean alternatePump = 1 ; // Boolean variable to swap pumps
int potValue; // Potentiometer value level sensor
int LowWaterLevel = 1023*0.1; // Low water level
int HighWaterLevel = 1023*0.9; // High water level
elapsedMillis ProcessTimeElapsed; // Process Timer to control Program State
void setup ()
{
Serial.begin(9600);
pinMode(Pump1, OUTPUT);
pinMode(Pump2, OUTPUT);
pinMode(Alarm, OUTPUT);
}
void loop ()
{
potValue = analogRead(LevelSensor); // Analogue read for Level Sensor (LOW & High Level)
if (potValue >= HighWaterLevel && alternatePump == 1 && digitalRead(Pump1) == LOW)
// Water Level reaches High level, Boolean variable is True and Pump 1 is not running.
if (ProcessTimeElapsed < 90000) // Process Timer is below 90 seconds
{
digitalWrite(Pump2, HIGH); // Pump 2 Starts.
alternatePump = 0; // Boolean variable becomes false to alternate pump selection.
ProcessTimeElapsed = 0; // Process Timer is restarted at 0 seconds.
}
if (potValue >= HighWaterLevel && alternatePump == 0 && digitalRead(Pump2) == LOW)
// Water Level reaches High level, Boolean variable is false and Pump 2 is not running.
if (ProcessTimeElapsed < 90000) // Process Timer is below 90 seconds
{
digitalWrite(Pump1, HIGH); // Pump 1 Starts.
alternatePump = 1; // Boolean variable becomes true to alternate pump selection.
ProcessTimeElapsed = 0; // Process Timer is restarted at 0 seconds.
}
if (ProcessTimeElapsed >= 30000 && ProcessTimeElapsed < 90000)
// Process Timer has counted 30 Seconds and is below 90 Seconds - Both pumps run together.
{
digitalWrite(Pump2, HIGH); // Pump 2 Starts if not previously running.
digitalWrite(Pump1, HIGH); // Pump 2 Starts if not previously running.
}
if (ProcessTimeElapsed >= 60000 && ProcessTimeElapsed < 90000)
// Process Timer has counted 60 Seconds and is below 90 Seconds - Plant will alarm.
{
tone(8, 262, 90000); // Sump Pit Control Panel will alarm continuously.
}
if (ProcessTimeElapsed > 90000)
// Process Timer has counted 90 Seconds - Plant will shutdown.
{
digitalWrite(Pump2, LOW); // Pump 2 is shut down.
digitalWrite(Pump1, LOW); // Pump 1 is shut down.
noTone(Alarm); // Sump Put Control Panel will stop alarming.
}
if (potValue <= LowWaterLevel)
// Water level reaches low level. Plant will shutdown.
{
digitalWrite(Pump2, LOW); // Pump 2 is shut down.
digitalWrite(Pump1, LOW); // Pump 1 is shut down.
noTone(Alarm); // Sump Put Control Panel will stop alarming.
ProcessTimeElapsed = 0; // Process Timer is set to 0 seconds.
LowPower.idle(SLEEP_1S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,SPI_OFF, USART0_OFF, TWI_OFF);
// Plant switches to an idle state for 1 second, Low Power Mode to conserve power when water level is low.
}
}