#include <Wire.h> // Library untuk I2C
#include <LiquidCrystal_I2C.h>
#include <DHT.h> // Library untuk sensor DHT22
#include <RTClib.h> // Library untuk RTC DS3231
#include <SD.h>
// menentukan sensor dan pin LCD
#define DHTPIN 7 // Pin data DHT 22 terhubung ke pin 7 arduino
#define DHTTYPE DHT22 // Jenis sensor DHT22
#define CS_PIN A0
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Alamat I2C LCD
int relayPin = 8; // Pin kontrol Relay CH1
int indicatorPin = 11; // Pin lampu indikator L1
int relayPin2 = 9; // Pin relay CH2
int indicatorPin2 = 12; // Pin lampu indikator L2
RTC_DS1307 rtc;
File dataFile;
const int chipSelect = 53; // Sesuaikan dengan pin CS pada modul MicroSD
void setup() {
Serial.begin(9600);;
dht.begin();
lcd.init();
lcd.backlight();
rtc.begin();
pinMode(relayPin, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(indicatorPin, OUTPUT);
pinMode(indicatorPin2, OUTPUT);
digitalWrite(relayPin2, LOW); // Matikan relay awalnya
if (!rtc.isrunning()) {
Serial.println("RTC tidak berjalan! Atur waktu pada RTC terlebih dahulu.");
}
if (!rtc.begin()) {
Serial.println("Gagal menginisialisasi modul RTC.");
while (1);
}
// Inisialisasi kartu MicroSD
if (!SD.begin(chipSelect)) {
Serial.println("Gagal menginisialisasi kartu MicroSD.");
return;
}
// Inisialisasi RTC DS1307
Wire.begin();
if (!rtc.begin()) {
Serial.println("Gagal menginisialisasi modul RTC.");
while (1);
}
// Cek apakah RTC berjalan
if (!rtc.isrunning()) {
Serial.println("RTC tidak berjalan! Atur waktu pada RTC terlebih dahulu.");
}
// Buka atau buat file untuk ditulis
dataFile = SD.open("data.csv", FILE_WRITE);
// Cek apakah file berhasil dibuka
if (dataFile) {
Serial.println("File berhasil dibuka.");
// Menulis header ke file
dataFile.println("Waktu");
dataFile.close(); // Menutup file
} else {
Serial.println("Gagal membuka file.");
}
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
DateTime now = rtc.now();
lcd.setCursor(0, 2);
lcd.print("Date: ");
lcd.print(now.year(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.day(), DEC);
lcd.setCursor(0, 3);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
delay(2000); // Delay selama 2 detik sebelum pembacaan berikutnya
// Cek apakah sensor membaca data yang valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Gagal membaca sensor DHT22!");
return;
}
Serial.print("Suhu: ");
Serial.print(temperature);
Serial.println("°C");
Serial.print("Kelembaban: ");
Serial.print(humidity);
Serial.println("%");
// Atur kondisi untuk mengaktifkan atau menonaktifkan relay dan lampu indikator pada Shoes dryer deodorizing
if (temperature >= 25.0) {
digitalWrite(relayPin, HIGH); // Relay aktif
digitalWrite(indicatorPin, HIGH); // Lampu indikator menyala
}
else {
digitalWrite(relayPin, LOW); // Relay non-aktif
digitalWrite(indicatorPin, LOW); // Lampu indikator mati
}
delay(1000); // Interval pembacaan sensor (1 detik)
// Kontrol kipas DC dan indikator berdasarkan suhu dan kelembaban
if (temperature > 40 && humidity > 50) {
digitalWrite(relayPin2, HIGH); // Hidupkan relay (nyalakan kipas DC)
digitalWrite(indicatorPin2, HIGH); // Nyalakan LED indikator
} else {
digitalWrite(relayPin2, LOW); // Matikan relay (matikan kipas DC)
digitalWrite(indicatorPin2, LOW); // Matikan LED indikator
}
delay(1000); // Baca suhu dan kelembaban setiap 1 detik
Serial.print("Tanggal dan Waktu: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
if (dataFile) {
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(" ");
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.println();
}
delay(1000);
Serial.print("Waktu: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Menulis data waktu ke file di kartu MicroSD
dataFile = SD.open("data.csv", FILE_WRITE);
if (dataFile) {
// Format data waktu sebagai CSV
String waktu = String(now.year()) + "-" + String(now.month()) + "-" + String(now.day()) + " " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
dataFile.println(waktu);
dataFile.close();
Serial.println("Data waktu telah disimpan di kartu MicroSD.");
} else {
Serial.println("Gagal membuka file.");
}
delay(1000); // Tunggu 1 detik sebelum membaca waktu lagi
}