#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int trigPin = 7;
int echoPin = 6;
float duration, cm;
int trigPins = 4;
int echoPins = 5;
float durations, cms;
Servo servoOpen;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPins, OUTPUT);
pinMode(echoPins, INPUT);
servoOpen.attach(9);
servoOpen.write(0);
updateLCD();
}
void loop() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW); delayMicroseconds(2);
duration = pulseIn(echoPin, HIGH);
digitalWrite(trigPins, LOW); delayMicroseconds(2);
digitalWrite(trigPins, HIGH); delayMicroseconds(10);
digitalWrite(trigPins, LOW); delayMicroseconds(2);
durations = pulseIn(echoPins, HIGH);
cm = (duration/2) / 29.1;
cms = (durations/2) / 29.1;
if(cm <= 250)
{
servoOpen.write(90);
delay(500); }
else
{
servoOpen.write(0);
delay(500);
}
updateLCD();
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kondisi :");
lcd.setCursor(0, 1);
if (cms >= 210 && cms <= 300) {
lcd.print("Sedikit");
}
else if (cms <= 209 && cms >= 70) {
lcd.print("Setengah");
}
else if (cms >= 0 && cms <= 69){
lcd.print("Penuh");
}
else {
lcd.print("Kosong");
}
}