water level meter

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.0 kB
1
Indexable
Never
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//membuat objek lcd
LiquidCrystal_I2C lcd(0x27, 16, 2); 

//mendefinikan pin ultrasonik
#define trigpin 4  
#define echopin 2 

//mendefinisikan pin lampu, relay sama buzzer
int ledlow = 5; 
int ledmiddle = 6; 
int ledhigh = 7; 
int buzzer = 8; 
int relay = 9;

//mendeklarasikan durasi dan distance
int duration, distance;

void setup()
{
Serial.begin(9600);
lcd.begin();

  //mengatur pin sebagai input atau output
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  pinMode(ledlow, OUTPUT); 
  pinMode(ledmiddle, OUTPUT);
  pinMode(ledhigh, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(relay, OUTPUT);

  //mengatur penulisan pin digital saat awal sistem dihidupkan
  digitalWrite(ledlow, LOW); 
  digitalWrite(ledmiddle, LOW);
  digitalWrite(ledhigh, LOW);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW);

 delay(1000); 
}

void loop()
{

digitalWrite(trigpin, LOW); //memastikan tidak ada pengiriman gelombang suara (membersihkan)
delayMicroseconds(2);
digitalWrite(trigpin, HIGH); //memberikan trigger kepada transmiter selama 10 microsecond
delayMicroseconds(10);
digitalWrite(trigpin, LOW);

duration = pulseIn(echopin, HIGH); //ketika menerima gelombang maka akan high dan menghitung waktunya (s = v . t) 

distance = duration*0.034/2;

//Menampilkan jarak pada serial monitor
Serial.println("cm:"); 
Serial.println(distance);

//Menampilkan jarak pada LCD 16x2
lcd.setCursor(0,0);
  lcd.print("Jarak = ");
  lcd.print(distance);       
  lcd.print(" cm ");
  
if(  (distance > 0) && (distance <= 5)   ) //Batas Ambang
{
  digitalWrite(ledlow, HIGH); 
  digitalWrite(ledmiddle, LOW);
  digitalWrite(ledhigh, LOW);
  digitalWrite(relay, HIGH);
  digitalWrite(buzzer, HIGH);
  delay(1000);

  lcd.setCursor(0,1);
  lcd.print("Tangki Overload ");
} else
if(  (distance > 5) && (distance <= 10)  ) //Level High
{

  digitalWrite(ledlow, LOW); 
  digitalWrite(ledmiddle, LOW);
  digitalWrite(ledhigh, HIGH);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW); 
  lcd.setCursor(0,1);
  lcd.print("Tangki Penuh ");

} else

if(  (distance > 10) && (distance <= 40)  ) //Level Middle
{

  digitalWrite(ledlow, LOW); 
  digitalWrite(ledmiddle, HIGH);
  digitalWrite(ledhigh, LOW);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW);
  lcd.setCursor(0,1);
  lcd.print("Tangki Berisi ");
} else

if(  (distance > 40) && (distance <= 60)  ) //Level Low
{

  digitalWrite(ledlow, HIGH); 
  digitalWrite(ledmiddle, LOW);
  digitalWrite(ledhigh, LOW);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW);
  lcd.setCursor(0,1);
  lcd.print("Tangki Kosong  ");
}else

if(  (distance > 60)  ) //Level Low
{

  digitalWrite(ledlow, HIGH); 
  digitalWrite(ledmiddle, LOW);
  digitalWrite(ledhigh, LOW);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW);
  lcd.setCursor(0,1);
  lcd.print("Tangki Kosong  ");
}
delay(500);
}