#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int trigPin =3;
int echoPin=2;
long duration;
float distance=0;
float R=0;
float H=0;
float V=0;
float v=0;
float L=0;
float S=0;
int M=0;
int N=0;
int W=0;
LiquidCrystal_I2C lcd(32,16,2);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {12, 11,10,9};
byte colPins[COLS] = {8, 7, 6,5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("chose the form:");
// get the first number from the keypad
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
M =M * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
lcd.clear();
if(M>2 or M<1){
lcd.setCursor(0,0);
lcd.print("chose the form:");
// get the first number from the keypad
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
M =M * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
lcd.clear();
}
while(M=2){
lcd.print("cylinder");
delay(1000);
lcd.clear(); lcd.clear();
lcd.print("enter radius:");
// get the first number from the keypad
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
R = R * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
// clear the LCD and display message for the second number
lcd.clear();
lcd.print("enter high:");
// get the second number from the keypad
lcd.setCursor(0,1);
key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
H = H * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
lcd.clear();
break;
}
while(M=1){
lcd.print("parallelogram");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.clear();
lcd.print("enter width:");
// get the first number from the keypad
lcd.setCursor(0,1);
char key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
W = W * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
// clear the LCD and display message for the second number
lcd.clear();
lcd.print("enter high:");
// get the second number from the keypad
lcd.setCursor(0,1);
key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
H = H * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
lcd.clear();
lcd.print("enter length:");
// get the second number from the keypad
lcd.setCursor(0,1);
key = keypad.waitForKey();
while (key != '#') {
if (key >= '0' && key <= '9') {
N = N * 10 + (key - '0');
lcd.print(key);
}
key = keypad.waitForKey();
}
lcd.clear();
break;
}
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin,HIGH);
if(duration==0){
lcd.setCursor(0,0);
lcd.print("no reading ");
}
else{
distance=duration*0.034/2;
if(M=1){
v=3.14*(R*R)*(distance)*pow(10,-6);
V=3.14*(R*R)*H*pow(10,-6);
S=V-(v*1000);
lcd.setCursor(0,0);
lcd.print("volume (litre):");
lcd.setCursor(0,1);
lcd.print(S);
}if(M=1){
v=W*L*(distance)*pow(10,-6);
v=W*L*H*pow(10,-6);
S=v-(v*1000);
lcd.setCursor(0,0);
lcd.print("volume (litre):");
lcd.setCursor(0,1);
lcd.print(S);
}
}
}