#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD(0x27, 16, 2);
//Push Button Pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
//Push Button Millis
unsigned long millisButton1 = 0;
unsigned long millisButton2 = 0;
unsigned long millisButton3 = 0;
unsigned long millisButton4 = 0;
//Shift Register Pins
const int data = 8;
const int latch = 9;
const int clock = 10;
int blokData[8] = {false, false, false, false, false, false, false, false};
//Mux Pins
const int selector1 = 7; //LSB
const int selector2 = 6;
const int muxSignal = A3;
//Sensor Value
float sensor_ph = 7.6;
float sensor_nutrisi = 350.0;
float sensor_suhu = 24.5;
float sensor_cahaya = 20;
float target_ph = 7;
float target_nutrisi = 850;
//LCD
int halaman = 1;
bool edit = false;
void setup() {
LCD.init();
LCD.backlight();
LCD.home();
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(data, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(selector1, OUTPUT);
pinMode(selector2, OUTPUT);
pinMode(muxSignal, INPUT);
}
void loop() {
periksaPushButton();
bacaSeluruhSensor();
perbaruiLogikaRelay();
nyalakanRelay();
resetRelay();
if(halaman == 1){
LCD.setCursor(0,0);
LCD.print("Temperature ");
LCD.setCursor(10,1);
LCD.print(sensor_suhu, 1);
LCD.setCursor(15,1);
LCD.print("C");
}else if(halaman == 2){
LCD.setCursor(0,0);
LCD.print("Soil Nutrition ");
if(edit == true){
LCD.setCursor(0,1);
LCD.print("Target-");
LCD.print(target_nutrisi, 1);
}else{
LCD.setCursor(7,1);
LCD.print(sensor_nutrisi, 1);
}
LCD.setCursor(13,1);
LCD.print("PPM");
}else if(halaman == 3){
LCD.setCursor(0,0);
LCD.print("Acid-Base Level ");
if(edit == true){
LCD.setCursor(0,1);
LCD.print("Target-");
LCD.print(target_ph, 1);
}else{
LCD.setCursor(10,1);
LCD.print(sensor_ph, 1);
}
LCD.setCursor(14,1);
LCD.print("pH");
}else if(halaman == 4){
LCD.setCursor(0,0);
LCD.print("Light Intensity ");
LCD.setCursor(9,1);
LCD.print(sensor_cahaya, 1);
LCD.setCursor(14,1);
LCD.print("cd");
}
}
void gantiSaluranMUX(int saluran){
if(saluran == 0){
digitalWrite(selector1,0);
digitalWrite(selector2,0);
}else if(saluran == 1){
digitalWrite(selector1,1);
digitalWrite(selector2,0);
}else if(saluran == 2){
digitalWrite(selector1,0);
digitalWrite(selector2,1);
}else if(saluran == 3){
digitalWrite(selector1,1);
digitalWrite(selector2,1);
}
}
void nyalakanRelay(){
digitalWrite(latch,LOW);
for(int i = 0; i < 8; i++){
digitalWrite(clock, LOW);
digitalWrite(data, !blokData[i]);
digitalWrite(clock, HIGH);
}
digitalWrite(latch,HIGH);
}
void perbaruiLogikaRelay(){
if(sensor_ph > target_ph){
// 8 9 10 11 12
blokData[0] = true;
}else if(sensor_ph < target_ph){
// 1 2 3 4 5
blokData[1] = true;
}
if(sensor_nutrisi < target_nutrisi){
blokData[2] = true;
blokData[3] = true;
}
}
void resetRelay(){
for(int i = 0; i < 8; i++){
blokData[i] = false;
}
}
void periksaPushButton(){
if(digitalRead(button1) == HIGH){
if(millis() - millisButton1 > 150){
LCD.clear();
edit = false;
millisButton1 = millis();
}
}
if(digitalRead(button2) == HIGH){
if(millis() - millisButton2 > 150){
LCD.clear();
if(edit == true){
if(halaman == 2){
target_nutrisi += 10;
if(target_nutrisi > 900){
target_nutrisi = 900;
}
}else if(halaman == 3){
target_ph += 1;
if(target_ph > 14){
target_ph = 14;
}
}
}else{
halaman -= 1;
if(halaman < 1){
halaman = 4;
}
}
millisButton2 = millis();
}
}
if(digitalRead(button3) == HIGH){
if(millis() - millisButton3 > 150){
LCD.clear();
if(edit == true){
if(halaman == 2){
target_nutrisi -= 10;
if(target_nutrisi < 100){
target_nutrisi = 100;
}
}else if(halaman == 3){
target_ph -= 1;
if(target_ph < 1){
target_ph = 1;
}
}
}else{
halaman += 1;
if(halaman > 4){
halaman = 1;
}
}
millisButton3 = millis();
}
}
if(digitalRead(button4) == HIGH){
if(millis() - millisButton4 > 150){
LCD.clear();
edit = true;
millisButton4 = millis();
}
}
}
void bacaSeluruhSensor(){
// sensor suhu
gantiSaluranMUX(0);
delay(10);
sensor_suhu = map(analogRead(muxSignal), 0, 1023, 22.5, 30.5);
// sensor nutrisi
gantiSaluranMUX(1);
delay(10);
sensor_nutrisi = map(analogRead(muxSignal), 0, 1023, 100, 900);
//sensor ph
gantiSaluranMUX(2);
delay(10);
sensor_ph = map(analogRead(muxSignal), 0, 1023, 1, 14);
//sensor cahaya
gantiSaluranMUX(3);
delay(10);
sensor_cahaya = map(analogRead(muxSignal), 0, 1023, 10, 90);
}
// Yang belum di kerjakan:
// Firebase
// Relay mati habis 2 detik lewat
// buzzer habis 1 minggu ?
Loading
cd74hc4067
cd74hc4067
Temperatur
Nutrisi
pH
Intensitas Cahaya
Enter
Down
Up
Back
pH > 7
pH < 7
Micro
Macro