/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.
https://docs.arduino.cc/learn/electronics/lcd-displays
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Set Water Level Distance in CM
int emptyTankDistance = 150 ; //Distance when tank is empty
int fullTankDistance = 30 ; //Distance when tank is full
//Set trigger value in percentage
int triggerPer = 20 ; //start when water level drop below triggerPer
int triggerPer1 = 10 ; //alarm millis
// Define connections to sensor
#define TRIGPIN 10 //D27
#define ECHOPIN 9 //D26
#define input 8
float duration;
float distance;
int waterLevelPer;
int distance1;
bool toggleBuzzer = HIGH; //Define to remember the toggle state
int value;
int ldr = 0;
void setup() {
// set up the LCD's number of columns and rows:
Serial.begin(115200);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(input, INPUT);
pinMode(A1, INPUT);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
ldr = analogRead(A1);
measureDistance();
delay(500);
value = map(ldr, 0, 1023, 0, 100);
Serial.print("Distance: ");
Serial.print(value);
Serial.println(" cm");
}
void measureDistance() {
// Set the trigger pin LOW for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 20us to send pulse
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);
// Return the trigger pin to LOW
digitalWrite(TRIGPIN, LOW);
// Measure the width of the incoming pulse
duration = pulseIn(ECHOPIN, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
// Divide by 1000 as we want millimeters
distance = ((duration / 2) * 0.343) / 10;
if (distance > (fullTankDistance - 10) && distance < emptyTankDistance ) {
waterLevelPer = map((int)distance , emptyTankDistance, fullTankDistance, 0, 100);
//Print result to serial monitor
//Serial.print("Distance: ");
//Serial.print(distance);
//Serial.println(" cm");
//Serial.print(ldr);
lcd.setCursor(0, 0);
// print the number of seconds since reset:
lcd.print("VAL ");
lcd.print(waterLevelPer);
lcd.print("%");
lcd.setCursor(8, 1);
// print the number of seconds since reset:
lcd.print(distance);
lcd.print(" ");
lcd.print("cm");
// Delay before repeating measurement
}
}