//Perdana Wira A
#include <Keypad.h>
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
#include <ESP32Servo.h>
RTC_DS1307 rtc;
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 20, 4);
char customKey, mulaiKey;
//char stringAngka[2];
int indexKeypad = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
const int servoPin = 14;
Servo mekanik;
int x = 0;
const int pot1Pin = 12;
const int Heater = 25; // alamat Pin DIgital LED C
const int Cooler = 33; // alamat Pin Digital LED D
const int pH_UP = 26;
const int pH_Down =27;
float SPsuhu;
int pot1Value = analogRead(pot1Pin);
float diffpH;
float pH_now = pot1Value * (10.0 / 1023.0);
float SPpH;
float diffsuhu, temp_now;
float T_atas, T_bawah, P_atas, P_bawah, nilaisppH, nilaispSuhu, nilaidiff;
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
mekanik.attach(servoPin);
mekanik.write(0);
pinMode(Heater, OUTPUT);
pinMode(Cooler, OUTPUT);
pinMode(pH_UP, OUTPUT);
pinMode(pH_Down, OUTPUT);
SPsuhu = 0;
SPpH = 0;
diffsuhu = 0;
diffpH = 0;
nilaispSuhu = SPsuhu;
nilaisppH= SPpH;
nilaidiff = diffsuhu, diffpH;
T_atas = SPsuhu + (diffsuhu/2);
T_bawah = SPsuhu - (diffsuhu/2);
P_atas = SPpH + (diffpH/2);
P_bawah = SPpH - (diffpH/2);
if (!rtc.begin()){
Serial.println("RTC Tidak Terhubung");
lcd.setCursor(0,0);
lcd.print("RTC Tidak Konek");
while(1);
}
delay(50);
lcd.clear();
}
void loop()
{
customKey = customKeypad.getKey();
if(x == 0){
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("1.SET suhu ");
}
if(x == 1){
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("2.SET pH ");
}
if(x == 2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("3.MULAI");
}
if(x == 3){
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("4.CEK ON-OFF");
} switch(customKey)
{
case '0' ... '9':
break;
case '#':
break;
case '*':
break;
case 'A':
x++;
break;
case 'B':
x--;
break;
case 'C':
break;
case 'D':
if(x == 0){
lcd.clear();
//analogWrite(ssr,0);
setsp_suhu();
setdiff_suhu();
}
if(x == 1){
lcd.clear();
setsppH();
setdiff_pH();
}
if(x == 2){
lcd.clear();
mulai();
}
if(x == 3){
lcd.clear();
//analogWrite(ssr,0);
cekonoff();
}
break;
}
if(x > 3){
x = 0;
}
if(x < 0){
x = 3;
}
}
void setdiff_suhu(){
lcd.setCursor(0,0);
lcd.print("SET DIFF ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
diffsuhu = diffsuhu * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(diffsuhu);
T_atas = SPsuhu + (diffsuhu/2);
T_bawah = SPsuhu - (diffsuhu/2);
}
if(customKey == '*'){
lcd.clear();
delay(1000);
nilaidiff = diffsuhu;
return;
}
setdiff_suhu();
}
void setdiff_pH(){
lcd.setCursor(0,0);
lcd.print("SET DIFF ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
diffpH = diffpH * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(diffpH);
P_atas = SPpH + (diffpH/2);
P_bawah = SPpH - (diffpH/2);
}
if(customKey == '*'){
lcd.clear();
delay(1000);
nilaidiff = diffpH;
return;
}
setdiff_pH();
}
void setsp_suhu(){
lcd.setCursor(0,0);
lcd.print("SET SP ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
SPsuhu = SPsuhu * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(SPsuhu);
//lcd.print(customKey);
//sp = customKey;
}
if(customKey == '*'){
lcd.clear();
delay(1000);
nilaispSuhu = SPsuhu;
return;
}
setsp_suhu();
}
void setsppH(){
lcd.setCursor(0,0);
lcd.print("SET Ph ");
bool decimalAdded = false; // Tambahkan flag untuk desimal
float decimalFactor = 0.1;
while(true) {
customKey = customKeypad.getKey();
if(customKey != NO_KEY){
if(customKey >= '0' && customKey <= '9'){
if (!decimalAdded) {
SPpH = SPpH * 10 + (customKey - '0');
} else {
SPpH += (customKey - '0') * decimalFactor;
decimalFactor /= 10; // Kurangi faktor desimal
}
lcd.setCursor(0,1);
lcd.print(SPpH, 2); // Menampilkan dengan dua angka desimal
}
else if (customKey == '#' && !decimalAdded) { // Gunakan '#' untuk memulai input desimal
decimalAdded = true; // Tandai bahwa titik desimal telah ditambahkan
lcd.print(","); // Tampilkan koma pada LCD
}
else if (customKey == '*'){
lcd.clear();
delay(1000);
nilaisppH = SPpH; // Simpan nilai SP
return; // Keluar dari fungsi
}
}
}
}
void cekonoff(){
lcd.setCursor(0,0);
lcd.print("Set Suhu = ");
lcd.print(nilaispSuhu);
lcd.setCursor(0,1);
lcd.print("Set pH ");
lcd.print(nilaisppH);
lcd.setCursor(0, 2);
lcd.print("Control Diff= ");
lcd.print(nilaidiff);
customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
delay(1000);
return;
}
cekonoff();
}
void mulai(){
if (SPsuhu==0, SPpH== 0) {
lcd.setCursor(0, 0);
lcd.print("Mohon Set SP");
lcd.setCursor(0, 1);
lcd.print("dan Diff Dahulu");
delay(2000);
return;
}
sensors.requestTemperatures();
T_atas = SPsuhu + (diffsuhu/2);
T_bawah = SPsuhu - (diffsuhu/2);
temp_now = sensors.getTempCByIndex(0);
if (temp_now >= T_atas) {
digitalWrite(Heater, LOW); // Turn on LED A
digitalWrite(Cooler, HIGH); // Turn off LED B
}
else if (temp_now <= T_bawah) {
digitalWrite(Heater, HIGH); // Turn off LED A
digitalWrite(Cooler, LOW); // Turn on LED B
}
else {
digitalWrite(Heater, LOW);
digitalWrite(Cooler, LOW);
}
pot1Value = analogRead(pot1Pin);
P_atas = SPpH + (diffpH/2);
P_bawah = SPpH - (diffpH/2);
pH_now = pot1Value * (10.0 / 1023.0);
if (pH_now >= P_atas) {
digitalWrite(pH_UP, LOW); // Turn on LED A
digitalWrite(pH_Down, HIGH); // Turn off LED B
}
else if (pH_now <= P_bawah) {
digitalWrite(pH_UP, HIGH); // Turn off LED A
digitalWrite(pH_Down, LOW); // Turn on LED B
}
else {
digitalWrite(pH_UP, LOW);
digitalWrite(pH_Down, LOW);
}
// program kekeruhan
//pakan otomatis
DateTime now = rtc.now();
int tahun = now.year();
int bulan = now.month();
int tanggal = now.day();
int jam = now.hour();
int menit = now.minute();
int detik = now.second();
if((jam == 8) && (menit == 00) && (detik == 1)){
mekanik.write(180);
delay(3000);
mekanik.write(0);
}
// Jadwal Kedua
if((jam == 15) && (menit == 00) && (detik == 1)){
mekanik.write(180);
delay(3000);
mekanik.write(0);
}
//program hubung ke web
//Serial.print("DATA,TIME,");
Serial.print("L1");
Serial.println(temp_now);
Serial.println(pH_now);
lcd.setCursor(0, 0);
lcd.print(" MONITORING ");
lcd.setCursor(0, 1);
lcd.print("Suhu ");
lcd.print(temp_now);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("pH ");
lcd.print(pH_now);
lcd.setCursor(0, 3);
lcd.print("Kekeruhan");
delay(200);customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
digitalWrite(Heater, LOW);
digitalWrite(Cooler, LOW);
digitalWrite(pH_UP, LOW);
digitalWrite(pH_Down, LOW);
resetFunc(); //call reset
delay(800);
return;
}
mulai();
}