// Water Level Detector Sample
#include <LiquidCrystal.h> //library to use LCD commands
#define Led 1
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); //lcd pin connected to digital pins 12, 11, 10, 9, 8, and 7 of the ARDUINO
int sensor[]={2,3,4,5,6}; //output of "sensor" connected to digital pins 2, 3, 4, 5 and 6. This is an example of array variable.
/* An alternative way of defining the connection of these sensors is to list them individually.
Ex: sensor1=2;
sensor2=3;
and so on... */
/* For simulation purpose, the DIP switch is used in place of the water level sensors */
void setup() {
/* The following "for" command sets the "sensor" variable as INPUT */
pinMode(Led,OUTPUT);
for (int i=0; i<4; i++)
pinMode(sensor[i],INPUT);
/* If the variables "sensor" are listed individually, set also each of these input variables individually.
ex. pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
and so on ... */
lcd.begin(16, 2); //Initialize the LCD (16 columns and 2 rows). The LCD is now ready to be used.
}
void loop() {
// ...
int a=digitalRead(sensor[0]); //reads the status of "sensor" connected tp pin 2
int b=digitalRead(sensor[1]); //reads the status of "sensor" connected tp pin 3
int c=digitalRead(sensor[2]); //reads the status of "sensor" connected tp pin 4
int d=digitalRead(sensor[3]); //reads the status of "sensor" connected tp pin 5
int e=digitalRead(sensor[4]); //reads the status of "sensor" connected tp pin 6
if(a==0 && b==0 && c==0 && d==0 && e==0) //Tests if ALL senors are in the LOW state
{
lcd.setCursor(0, 0); //starts the text at column 0 and row 0
lcd.print(" Tank is Empty! "); //command to print text in the LCD
}
else if(a==1 && b==0 && c==0 && d==0 && e==0) //Tests if the firt sensor is HIGH and all the others is LOW
{
lcd.setCursor(0, 0);
lcd.print("Tank is 25% Full");
}
else if(a==1 && b==1 && c==0 && d==0 && e==0)
{
lcd.setCursor(0, 0);
lcd.print("Tank is 50% Full");
}
else if(a==1 && b==1 && c==1 && d==0 && e==0)
{
lcd.setCursor(0, 0);
lcd.print("Tank is 75% Full");
}
else if(a==1 && b==1 && c==1 && d==1 && e==0)
{
lcd.setCursor(0, 0);
lcd.print(" Tank is Full! ");
digitalWrite(Led,LOW);
}
else if(a==1 && b==1 && c==1 && d==1 && e==1)
{
/* The following instructions blinks the text in the LCD */
lcd.setCursor(0, 0);
lcd.print(" Overflowing! ");
delay(500);
lcd.clear();
delay(500);
tone(13, 500, 500); //generates a sound alarm
digitalWrite(Led,HIGH);
}
}