#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#define echoPin 18 // attach pin GPIO18 to pin Echo of AJ-SR04M
#define trigPin 5 // attach pin GPIO5 ESP32 to pin Trig of AJ-SR04M
#define sound_speed 0.0344 //sound speed in CM/uSeg
#define buzzerPin 13 // attach pin GPIO13 to Buzzer +VE
#define COLS 16 // Columns of LCD
#define ROWS 2 // Rows of LCD
//LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, COLS, ROWS);
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, COLS, ROWS);
long duration; // Time taken for the pulse to reach the receiver
const float distanceEmpty= 180; // Max Critical Distance (180 cms), value clud change depends on of tank size
const float distanceFull= 35; // OverFull distance between sensor and minim water distance (cm). To check & Resize
const float range=distanceEmpty-distanceFull;
int demora;
char* status;
//variable to calc distance & percentage of tank
float distanceCM;
float percentage_measureCM;
float percen;
void displayLCD(const char* tankstatus, float distanceCalc, int percentageCalc)
{
Serial.println("LCD Msg: " + String(tankstatus));
Serial.println("LCD Distance cm: " + String(distanceCalc));
Serial.println("LCD Percentage %: " + String(percentageCalc));
lcd.clear(); //LCD Clear
lcd.setCursor(0, 0);
lcd.print(String(tankstatus) + String(percentageCalc)+"%");
lcd.setCursor(0, 1);
lcd.print("Dist.(cm):"+ String(distanceCalc));
}
void buzzerAlert(int demora, const char* status)
{
Serial.println("Trigger Buzzer - demora: "+ String(demora)+ " / Tipo:" + String(status));
if (status=="A")
{
tone(buzzerPin,587);
delay(demora);
noTone(buzzerPin);
}
else
{ //Tipo N
tone(buzzerPin,640);
delay(1000);
noTone(buzzerPin);
}
}
void start_measure()
{
digitalWrite(trigPin, LOW); //Clears the trigPin
delayMicroseconds(2); // keep the trigger "OFF"
digitalWrite(trigPin, HIGH); // turn on the Trigger to generate pulse
delayMicroseconds(10); // keep the trigger "ON" for 10 ms to generate pulse
digitalWrite(trigPin, LOW); // Turn off the pulse trigger to stop pulse
}
void setup()
{
pinMode(trigPin, OUTPUT); //sets trigPin as an Output
pinMode(echoPin, INPUT); //sets echoPin as an Input
Serial.begin(115200); //starts the serial communication
Serial.println("Distance measurement using AJ-SR04M");
lcd.init();//LCD inicializing
lcd.backlight(); // Turn on the backlight
displayLCD ("Starting measurement...",0,0); //Display Setup/Start
delay(2000);
lcd.setCursor(0,1);
lcd.print("Start in 5..."); //You can write 16 Characters per line .
delay(1000); //wait 3 sec
lcd.setCursor(0,1);
lcd.print("Start in 4..."); //You can write 16 Characters per line .
delay(1000); //wait 3 sec
lcd.setCursor(0,1);
lcd.print("Start in 3..."); //You can write 16 Characters per line .
delay(1000); //wait 3 sec
lcd.setCursor(0,1);
lcd.print("Start in 2..."); //You can write 16 Characters per line .
delay(1000); //wait 3 sec
lcd.setCursor(0,1);
lcd.print("Start in 1..."); //You can write 16 Characters per line .
delay(1000); //wait 3 sec
// Buzzer test on Setup/Start
buzzerAlert(1000,"N"); //Buzzer alert beep
}
void loop()
{
start_measure(); // Start measure with AJ-SR04M, using trigPin
// If pulse reached the receiver echoPin
// become high Then pulseIn() returns the
// time taken by the pulse to reach the receiver
duration = pulseIn(echoPin, HIGH); //Reads the echoPin, returnd the sound wave travel time in uSeg
distanceCM = duration * sound_speed / 2;
// % Calc. value about distanceCM / distanceFull / range
percentage_measureCM = ((distanceCM-distanceFull)/range)*100;
Serial.println("===========================");
Serial.println("Values");
Serial.println("===========================");
Serial.println("Tank %: " + String(percentage_measureCM));
Serial.print("Distance: " + String(distanceCM));
Serial.println(" cm");
Serial.println("===========================");
if (distanceCM >= distanceEmpty)
{ //Tank Empty (LOW LIQUID)
if (percentage_measureCM > 99) // Si super el Umbral Max
{ percen = 0; // cuando es > 99, ponemos que el tanque esta vacio
}
displayLCD ("Empty! ", distanceCM, percen); // display LCD Empty tank!!
buzzerAlert(3000, "A"); //Buzzer alert code
Serial.println("Lectura :Empty - Wait 10 seg");
delay(10000); //luego de una lectura correcta esperamos 10 seg para volver a leer
}
else
{if (distanceCM <= distanceFull)
{ //Tank Over Full
displayLCD ("Full!", distanceCM, 100);
buzzerAlert(3000, "A"); //Buzzer alert code
Serial.println("Lectura :Empty - Wait 10 seg");
delay(10000); //luego de una lectura correcta esperamos 10 seg para volver a leer
}
else
{ //Tank Normal
displayLCD ("Tank:", distanceCM, (100-percentage_measureCM));
Serial.println("Lectura :OK - Wait 60 seg");
delay(60000); //luego de una lectura correcta esperamos 60 seg para volver a leer
}
}
//Delay ms to start loop again
delay(100);
}