#include <LiquidCrystal_I2C.h> // liquidcrystal i2c
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
#include <math.h>
int distance[6];
float Volume = 0.0;
void setup() {
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn LCD light ON
lcd.clear(); // clear display
lcd.setCursor(0, 1); // move cursor to (0, 0)
if(Volume == 0.0){
lcd.print("Calculating ... ");// print message at (0, 0)
}
else{
lcd.print("Volume=");// print message at (0, 0)
}
for (int i=15; i<=17; i++) {
pinMode(i, INPUT); // Set Pin2 as Input Pin (connected to ECHO Pin)
}
pinMode(A0, OUTPUT); // Set Pin3 as Output Pin (connected to TRIG Pin)
}
void loop() {
if(Volume != 0.0){
lcd.setCursor(0, 1); // move cursor to (0, 0)
lcd.print("Volume=");// print message at (0, 0)
lcd.print(Volume);// print message at (0, 0)
lcd.print(" m^3");// print message at (0, 0)
}
int j = 2;
// Start a new measurement:
for (int i=15; i<=17; i++) {
digitalWrite(A0, HIGH); // Start sending the UltraSound Sound
delayMicroseconds(10); // Sending Duration 10uSec.
digitalWrite(A0, LOW); // End sending the UltraSound Sound
// Read the result:
distance[i] = pulseIn(i, HIGH) / 58;
lcd.setCursor(0, 0); // move cursor to (2, 1)
if(i == 15) {
lcd.print("LENGTH= "); // Clear The Screen at (2, 1)
}
else if(i == 16) {
lcd.print("HEIGHT= "); // Clear The Screen at (2, 1)
}
else if(i == 17) {
lcd.print("WIDTH = "); // Clear The Screen at (2, 1)
}
lcd.print(distance[i] / 100.0); // print message at (2, 1)
lcd.print(" m"); // print message at (2, 1)
delay(2000);
}
Volume = (distance[15] / 100.0) * (distance[16] / 100.0) * (distance[17] / 100.0);
delay(500);
}