#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
int echoPin =A0;
int trigPin =A1;
int offset = 2;
char keys[4][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte _R[4] = {9, 8, 7, 6};
byte _C[3] = {5, 4, 3};
char key;
String rx;
int count;
int radius,height,length,width;
unsigned long maxVolume,volume;
bool Mode =0; // 0 cubic 1 cylyndric
Keypad keypad = Keypad(makeKeymap(keys),_R,_C,4,3);
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(trigPin, OUTPUT);
wizard();
}
void loop() {
if(Mode){
volume = constrain(height-distance(),0,400);
volume = (pow(radius,2)*3.14)*volume/1000;
}else{
volume = constrain(height-distance(),0,400);
volume = ((length/100)*(width/100)*(volume/100))*1000;
}
lcd.setCursor(0,1);
lcd.print(volume);
lcd.print(" L ");
lcd.setCursor(10,1);
lcd.print(floor((volume*100)/maxVolume),0);
lcd.print(" % ");
delay(200);
}
void wizard(){
lcd.clear();
lcd.print("1 pour Cubic");
lcd.setCursor(0,1);
lcd.print("2 pour Cylyndric");
do{
key = keypad.waitForKey();
}while(key != '1' and key != '2');
if(key=='1'){
Mode = 0;
lcd.clear();
lcd.print("Cubic");
lcd.setCursor(0,1);
lcd.print("Length: cm");
readKeypad();
length = rx.toInt();
lcd.setCursor(0,1);
lcd.print("Width : cm");
readKeypad();
width = rx.toInt();
lcd.setCursor(0,1);
lcd.print("Height: cm");
readKeypad();
height = rx.toInt();
maxVolume = ((length/100)*(width/100)*(height/100))*1000;
lcd.clear();
lcd.print("Max Volume:");
lcd.setCursor(0,1);
lcd.print(maxVolume);
lcd.print(" L");
}else{
Mode=1;
lcd.clear();
lcd.print("cylender");
lcd.setCursor(0,1);
lcd.print("Radius: cm");
readKeypad();
radius = rx.toInt();
lcd.setCursor(0,1);
lcd.print("Height: cm");
readKeypad();
height = rx.toInt();
maxVolume = (pow(radius,2)*3.14)*height/1000;
lcd.clear();
lcd.print("Max Volume:");
lcd.setCursor(0,1);
lcd.print(maxVolume);
lcd.print(" L");
}
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Volume (litre):");
}
float distance(){
digitalWrite(trigPin, 0);
delayMicroseconds(2);
digitalWrite(trigPin, 1);
delayMicroseconds(10);
digitalWrite(trigPin, 0);
long duration = pulseIn(echoPin, 1);
return((duration*.0343)/2)-offset;
}
void readKeypad(){
count=0;
rx=" ";
while(1){
key = keypad.waitForKey();
if(key=='#')break;
if(key=='*'){
if(count>0)count--;
rx[count]=' ';
}else if(count<3){
rx[count]=key;
count++;
}
lcd.setCursor(8,1);
lcd.print(rx);
}
}