#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Servo.h>
Servo myservo;
#include <RTClib.h>
RTC_DS1307 rtc;
#define ECHO_PIN 3
#define TRIG_PIN 4
#define vibrator 2
#define buzzer 5
int a = 0;
int tombol = A2;
int reset ;
byte detik = 0, menit = 0, jam = 0, flag = 0;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(vibrator, OUTPUT);
pinMode(tombol, INPUT_PULLUP);
myservo.attach(11);
myservo.write(0);
rtc.begin();
// Atur waktu RTC jika belum diset
if (!rtc.begin()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// delay(800);
bacaSensor();
baca_tombol();
}
void bacaSensor(){
// Baca waktu saat ini dari RTC
DateTime now = rtc.now();
lcd.setCursor(0,0);
lcd.print("Waktu: ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.println(now.second(), DEC);
// Kondisi Pakan Penuh
if (a == LOW){
// Cek kondisi waktu dan atur pengendalian LED
if (now.hour() == 01 && now.minute() == 59 && now.second() == 0){
// Aktif Memberi Makan Ikan
flag = 1;
}
if (flag == 1){
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
lcd.setCursor(0,1);
lcd.print("Pakan: ");
lcd.print(distance);
lcd.print("cm");
if (distance >= 0 && distance <= 100){
myservo.write(90);
delay(15);
digitalWrite (vibrator, HIGH);
lcd.setCursor(0,2);
lcd.print("Pakan masih ada");
}
if (distance > 100 && distance <= 130){
myservo.write(30);
delay(15);
digitalWrite (vibrator, HIGH);
lcd.setCursor(0,2);
lcd.print("Pakan mau habis");
}
if (distance >130){
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
tone (buzzer, 120);
delay(500);
noTone(buzzer);
lcd.setCursor(0,2);
lcd.print("Pakan habis");
lcd.setCursor(0,3);
delay(1000);
lcd.print("Segera isi pakan ");
delay(1000);
lcd.setCursor(0,3);
lcd.print("Geser tombol riset");
delay(1000);
lcd.clear();
}
}
if (now.hour() == 01 && now.minute() >= 60 && now.second() <= 00){
flag = 0;
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
tone (buzzer, 120);
delay(500);
noTone(buzzer);
lcd.setCursor(0,2);
lcd.print("Sudah diberi makan ");
delay(1000);
lcd.setCursor(0,1);
lcd.print(" ");
delay(1000);
}
if (a == HIGH){
noTone(buzzer);
lcd.setCursor(0,1);
lcd.print("Sistem Standby");
}
delay(1000);
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
}
}
void baca_tombol(){
reset = digitalRead(tombol);
if (reset == LOW) {
a = 0;
}
if (reset == HIGH){
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
a = 1;
}
}