#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Servo.h>
#include <DHT.h>
#define DHTPIN 38
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Inisialisasi pin untuk perangkat
const int potensio_ph = A3;
const int potensio_DO = A4;
const int ultrasonicTriggerPin = 1;
const int ultrasonicEchoPin = 0;
const int led1 = 34;
const int led2 = 35;
const int buzzer1 = 32;
const int buzzer2 = 33;
const int ledCount = 10;
int ledPins[] = {
22, 23, 24, 25, 26, 27, 28, 29, 30, 31
};
Servo servoMotor;
// Variabel untuk nilai sensor
int phValue;
int doValue;
float distance;
void setup() {
// Inisialisasi perangkat
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzzer1, OUTPUT);
pinMode(buzzer2, OUTPUT);
pinMode(ultrasonicTriggerPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
servoMotor.attach(36);
// Inisialisasi LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SYSTEM KONTROL DAN");
delay(0);
lcd.setCursor(0, 1);
lcd.print("MONITORING KUALITAS");
delay(0);
lcd.setCursor(0, 2);
lcd.print("AIR OTOMATIS");
delay(5000);
lcd.clear();
dht.begin();
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// Membaca nilai potensiometer sebagai simulasi sensor
phValue = analogRead(potensio_ph);
doValue = analogRead(potensio_DO);
// Konversi nilai sensor ke rentang float (1.0 - 10.0)
float phDisplay = ((float)phValue / 1200.0) * 10;
float doDisplay = ((float)doValue / 1200.0) * 10;
// Buffer untuk menyimpan nilai float sebagai string
char phBuffer[8];
char doBuffer[8];
// Konversi float ke string dengan 2 angka desimal
dtostrf(phDisplay, 4, 2, phBuffer);
dtostrf(doDisplay, 4, 2, doBuffer);
//Logika sensor PH dan DO
if (phDisplay < 4) {
servoMotor.write(180);
digitalWrite(led1, HIGH);
tone(buzzer1, 1000, 200);
digitalWrite(led2, LOW);
} else if (phDisplay > 7) {
servoMotor.write(0);
digitalWrite(led1, HIGH);
tone(buzzer1, 1000, 200);
digitalWrite(led2, HIGH);
tone(buzzer2, 1500, 400);
} else if (doDisplay < 3 || doDisplay > 5) {
servoMotor.write(0); // PH dalam rentang normal, hanya kontrol DO
digitalWrite(led1, HIGH);
tone(buzzer1, 1000, 200);
digitalWrite(led2, LOW);
} else {
servoMotor.write(0); // Semua dalam rentang normal
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
// Membaca sensor ultrasonic
digitalWrite(ultrasonicTriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTriggerPin, LOW);
float duration = pulseIn(ultrasonicEchoPin, HIGH);
distance = (duration * 0.034) / 2;
// Indikasi stok pupuk habis
String fertilizerStatus = (distance > 10) ? "HABIS" : "TERSEDIA";
// Membaca suhu
float temp = dht.readTemperature();
int ledLevel = map(temp, 20, 50, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
// Menampilkan data di LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PH Air : ");
lcd.print(phBuffer);
lcd.setCursor(0, 1);
lcd.print("Oxygen : ");
lcd.print(doBuffer);
lcd.print(" mg");
lcd.setCursor(0, 2);
lcd.print("Suhu : ");
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(0, 3);
lcd.print("Pupuk : ");
lcd.print(fertilizerStatus);
delay(1000);
}