#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include "DHT.h"
//#include <EEPROM.h>
//#include <Servo.h>
Adafruit_SSD1306 lcd(128, 64, &Wire, 4);
#define btnUp 9
#define btnOk 5
#define btnDown 8
#define btnBack 6
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define led_kuning 13
#define led_biru 12
#define buzzer 7
#define lamp 10
#define spray 3
boolean statusBtnUp = false;
boolean statusBtnOk = false;
boolean statusBtnDown = false;
boolean statusBtnBack = false;
boolean statusAkhirBtnUp = false;
boolean statusAkhirBtnOk = false;
boolean statusAkhirBtnDown = false;
boolean statusAkhirBtnBack = false;
boolean UP = false;
boolean OK = false;
boolean DOWN = false;
boolean BACK = false;
int halaman = 0;
int menuItem = 1;
//#define EE_MAX 1
//#define EE_MIN 0
//#define EE_HMAX 2
//#define EE_HMIN 3
float tmax = 39.00;
float tmin = 38.00;
float hmax = 58.00;
float hmin = 56.00;
int w = 1;
//Servo arm;
//float pos = 0.0; // Variable where the arm's position will be stored (in degrees)
//float step = 1.0; // Variable used for the arm's position step
void setup() {
Serial.begin(115200);
//Serial.begin(9600);
lcd.begin(SSD1306_SWITCHCAPVCC, 0x3C);
lcd.clearDisplay();
dht.begin();
// arm.attach(A0);
// arm.write(pos);
//EEPROM.write(EE_MIN,37);
//EEPROM.write(EE_HMAX,60);
//EEPROM.write(EE_HMIN,55);
//tmax = EEPROM.read(EE_MAX);
// tmin = EEPROM.read(EE_MIN);
//hmax = EEPROM.read(EE_HMAX);
//hmin = EEPROM.read(EE_HMIN);
pinMode(btnUp, INPUT_PULLUP);
pinMode(btnOk, INPUT_PULLUP);
pinMode(btnDown, INPUT_PULLUP);
pinMode(btnBack, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode(led_kuning, OUTPUT);
pinMode(led_biru, OUTPUT);
pinMode(lamp, OUTPUT);
pinMode(spray, OUTPUT);
}
void loop() {
// delay(300);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if(h<hmin){
tone(buzzer,LOW);
delay(100);
if(w == 30){
w = 1;
}
if(w == 1){
digitalWrite(spray,HIGH);
delay(1000);
digitalWrite(spray, LOW);
}
w = w+1;
noTone(buzzer);
}else if( h>= hmin && h<= hmax){
noTone(buzzer);
}else if(h > hmax){
tone(buzzer,HIGH);
delay(100);
noTone(buzzer);
}
if (t <tmin) {
digitalWrite(lamp, HIGH);
digitalWrite(led_kuning, LOW);
digitalWrite(led_biru, HIGH);
tone(buzzer, LOW);
delay(100);
noTone(7);
delay(100);
}
else if (t >=tmin && t <=tmax){
digitalWrite(led_kuning, HIGH);
digitalWrite(led_biru, LOW);
noTone(7);
}
else if (t >tmax){
digitalWrite(lamp, LOW);
digitalWrite(led_kuning, LOW);
digitalWrite(led_biru, HIGH);
tone(buzzer, 1000);
delay(100);
noTone(7);
delay(10);
}
// if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println(F("Failed to read from DHT sensor!"));
// return;
// }
// Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
tampil();
statusBtnUp = digitalRead(btnUp);
statusBtnOk = digitalRead(btnOk);
statusBtnDown = digitalRead(btnDown);
statusBtnBack = digitalRead(btnBack);
saatUpDitekan();
saatOkDitekan();
saatDownDitekan();
saatBackDitekan();
// untuk button up
if (UP && halaman == 1) {
UP = false;
menuItem --;
if (menuItem < 1)menuItem = 5;
}else if(UP && halaman == 2){
tmax = tmax +1;
// EEPROM.write(EE_MAX,tmax);
UP = false;
}else if(UP && halaman == 3){
tmin = tmin + 1;
//EEPROM.write(EE_MIN,tmin);
UP = false;
}else if(UP && halaman == 4){
hmax = hmax + 1;
UP = false;
}else if(UP && halaman == 5){
UP = false;
// hmin = hmin + 1;
}
//untuk button down
if (DOWN && halaman == 1) {
DOWN = false;
menuItem ++;
if (menuItem > 5)menuItem = 1;
}else if(DOWN && halaman == 2){
tmax = tmax - 1;
if(tmax == tmin) tmax = tmin + 1;
//EEPROM.write(EE_MAX,tmax);
DOWN = false;
}else if(DOWN && halaman == 3){
tmin = tmin - 1;
//EEPROM.write(EE_MIN,tmin);
DOWN = false;
}else if(DOWN && halaman == 4){
hmax = hmax - 1;
DOWN = false;
}else if(DOWN && halaman == 5){
// hmin = hmin - 1;
DOWN = false;
}
// untuk button ok
if (OK) {
OK = false;
if (halaman == 1 && menuItem == 1) {
halaman = 2;
} else if (halaman == 1 && menuItem == 2) {
halaman = 3;
} else if (halaman == 1 && menuItem == 3) {
halaman = 4;
} else if (halaman == 1 && menuItem == 4) {
halaman = 5;
} else if (halaman == 1 && menuItem == 5) {
halaman = 6;
} else if (halaman == 0) {
halaman = 1;
}
}
// untuk button back
if (BACK) {
BACK = false;
if (halaman == 2 || halaman == 3 || halaman == 4 || halaman == 5 || halaman == 6 ) {
halaman = 1;
} else if (halaman == 1){
halaman = 0;
}
}
// if(halaman == 1){
//Serial.println(menuItem);
// delay(100);
// }
}
//--------------------------------------------------------------------------------
void saatUpDitekan() {
if (statusBtnUp != statusAkhirBtnUp) {
if (statusBtnUp == 0) {
UP = true;
}
delay(50);
}
statusAkhirBtnUp = statusBtnUp;
}
void saatOkDitekan() {
if (statusBtnOk != statusAkhirBtnOk) {
if (statusBtnOk == 0) {
OK = true;
}
delay(50);
}
statusAkhirBtnOk = statusBtnOk;
}
void saatDownDitekan() {
if (statusBtnDown != statusAkhirBtnDown) {
if (statusBtnDown == 0) {
DOWN = true;
}
delay(50);
}
statusAkhirBtnDown = statusBtnDown;
}
void saatBackDitekan() {
if (statusBtnBack != statusAkhirBtnBack) {
if (statusBtnBack == 0) {
BACK = true;
}
delay(50);
}
statusAkhirBtnBack = statusBtnBack;
}
//----------------------------------------------------------------------------
//semua yang tampil di lcd ada di fungsi ini
void tampil() {
if (halaman == 1) {
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(10, 0);
lcd.print("SETTING PARAMETER");
if (menuItem == 1) {
lcd.setCursor(10, 10);
lcd.setTextColor(WHITE);
lcd.print("> Set Suhu Max ");
} else {
lcd.setCursor(10, 10);
lcd.setTextColor(WHITE);
lcd.print(" Set Suhu Max");
}
if (menuItem == 2) {
lcd.setCursor(10, 20);
lcd.setTextColor(WHITE);
lcd.print("> Set Suhu Min");
} else {
lcd.setCursor(10, 20);
lcd.setTextColor(WHITE);
lcd.print(" Set Suhu Min");
}
if (menuItem == 3) {
lcd.setCursor(10, 30);
lcd.setTextColor(WHITE);
lcd.print("> Set Spray On");
} else {
lcd.setCursor(10, 30);
lcd.setTextColor(WHITE);
lcd.print(" Set Spray On");
}
if (menuItem == 4) {
lcd.setCursor(10, 40);
lcd.setTextColor(WHITE);
lcd.print("> Set Spray Off");
} else {
lcd.setCursor(10, 40);
lcd.setTextColor(WHITE);
lcd.print(" Set Spray Off");
}
if (menuItem == 5) {
lcd.setCursor(10, 50);
lcd.setTextColor(WHITE);
lcd.print("> Roller");
} else {
lcd.setCursor(10, 50);
lcd.setTextColor(WHITE);
lcd.print(" Roller");
}
} else if (halaman == 2) {
lcd.clearDisplay();
lcd.setTextColor(WHITE);
lcd.setTextSize(1);
lcd.setCursor(16, 10);
lcd.print("SET SUHU MAXIMAL");
lcd.setCursor(40, 30);
lcd.print(tmax);
} else if (halaman == 3) {
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(16, 15);
lcd.print("SET SUHU MINIMAL");
lcd.setCursor(40, 35);
lcd.print(tmin);
} else if (halaman == 4) {
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(25, 10);
lcd.print("SET SPRAY MAX");
lcd.setCursor(20, 35);
lcd.setTextSize(1);
lcd.print("Lembab :");
lcd.setCursor(70, 35);
lcd.print(hmax);
lcd.setCursor(105, 35);
lcd.print("%");
} else if (halaman == 5) {
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(25, 10);
lcd.print("SET SPRAY MIN");
lcd.setCursor(20, 35);
lcd.print("Lembab :");
lcd.setCursor(70, 35);
lcd.print(hmin);
lcd.setCursor(105, 35);
lcd.print("%");
} else if (halaman == 6) {
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(25, 15);
lcd.print("SETTING ROLLER");
} else{
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
lcd.clearDisplay();
lcd.setTextSize(1);
lcd.setTextColor(WHITE);
lcd.setCursor(0, 0);
lcd.print("AKTUALISASI PENETASAN");
lcd.setCursor(5, 15);
lcd.print("Suhu");
lcd.setCursor(5, 28);
lcd.print("Kelembaban");
lcd.setCursor(110, 28);
lcd.print("%");
lcd.setCursor(70, 15);
lcd.print(":");
lcd.setCursor(70, 28);
lcd.print(":");
lcd.setCursor(77, 15);
lcd.print(t);
lcd.setCursor(77, 28);
lcd.print(h);
}
lcd.display();
}