//OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width,  in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

//Set - bisa diatur
int ppm_maks = 5000;
int cm_trigger = 10;
int persen_redup = 40;
int persen_terang = 70;

//Rotary
#define DIR_PIN 13  
#define CLK_PIN 14 
#define SW_PIN 12 

//US
#include "Ultrasonic.h"
Ultrasonic ultrasonic(18, 19);
int cm;

//DHT
#include "DHT.h"
#define DHT22_PIN 17
DHT dht(DHT22_PIN, DHT22);
float celcius;

//LDR
int LDR_PIN = 34;
int persen;

//MQ
int MQ_PIN = 35;
int ppm;

//Buzzer
int BUZZER_PIN = 16;

//Relay
int RELAYFAN_PIN = 4;
int RELAYLED_PIN = 5;

//TCS
#define S2 26 
#define S3 27
#define sensorOut 25
int Red = 0;
int Green = 0;
int Blue = 0;
int Frequency = 0;

//Deklarasi
int view = 99; String text_menu; int menu = 1;
unsigned long millis1s;
int counter = 0; 
int currentStateCLK;
int lastStateCLK;
int currentSwitchState;
int lastSwitchState;
String hasilwarna;
String state_led;

void setup() {
  //Serial
  Serial.begin(115200);

  //OLED
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("OLED Tidak ada!"));
    while (1);
  }

  //DHT
  dht.begin();

  //Buzzer
  noTone(BUZZER_PIN);

  //Rotary
  pinMode(DIR_PIN, INPUT_PULLUP); 
  pinMode(CLK_PIN, INPUT_PULLUP);
  pinMode(SW_PIN, INPUT_PULLUP);
  lastStateCLK = digitalRead(CLK_PIN); 
  lastSwitchState = digitalRead(SW_PIN); 

  //Relay
  pinMode(RELAYFAN_PIN, OUTPUT);
  pinMode(RELAYLED_PIN, OUTPUT);
  //Off dulu
  digitalWrite(RELAYFAN_PIN, HIGH);
  digitalWrite(RELAYLED_PIN, HIGH);

  //TCS
  pinMode(S2, OUTPUT); 
  pinMode(S3, OUTPUT); 
  pinMode(sensorOut, INPUT);   
}

void loop() {
  //Baca US
  cm = ultrasonic.read(CM);

  //Baca LDR
  persen = map(analogRead(LDR_PIN), 4096,0,1,100);

  //Baca MQ
  ppm = map(analogRead(MQ_PIN), 4096,0,10000,0);

  //Baca DHT
  celcius = dht.readTemperature();;
  
  //Baca TCS
  Red = getRed();
  Green = getGreen();
  Blue = getBlue();

  //Baca Rotary
  //Kanan Kiri
  currentStateCLK = digitalRead(CLK_PIN); 
  if (currentStateCLK != lastStateCLK) {
    //Jika di view 99
    if(view == 99){
    //Kanan
    if (digitalRead(DIR_PIN) != currentStateCLK) { 
      //Pilih Menu
      if(menu >= 5){
        menu = 1;
      } else {
        menu = menu + 1;
      }
    }
    //Kiri
    } else {
      //Pilih Menu
      if(menu <= 1){
        menu = 5;
      } else {
        menu = menu - 1;
      }
    }
    lastStateCLK = currentStateCLK;  
  }
  //Ditekan
  currentSwitchState = digitalRead(SW_PIN);
  if (currentSwitchState != lastSwitchState && currentSwitchState == LOW) {
    //Jika di view 99
    if(view == 99){
      //Dipilih US
      if(menu == 1){
        view = 1;
      }
      //Dipilih LDR
      else if(menu == 2){
        view = 2;
      }
      //Dipilih MQ
      else if(menu == 3){
        view = 3;
      }
      //Dipilih DHT
      else if(menu == 4){
        view = 4;
      }
      //Dipilih DHT
      else if(menu == 5){
        view = 5;
      }
    } else {
      view = 99;
    }
  }
  lastSwitchState = currentSwitchState; 
  
  //Deklarasi Tampil Menu
  if(menu == 1){
    text_menu = "ULTRASONIC";
  } else if(menu == 2){
    text_menu = "   LDR   ";
  } else if(menu == 3){
    text_menu = "   MQ    ";
  } else if(menu == 4){
    text_menu = "  DHT22  ";
  } else if(menu == 5){
    text_menu = " TCS3200 ";
  }

  //Validasi
  //Buzzer
  if(cm <= cm_trigger){
    //ON
    tone(BUZZER_PIN, 2000);
  } else {
    //OFF
    noTone(BUZZER_PIN);
  }
  //MQ
  //if(ppm >= ppm_maks){
    //FAN ON
   // digitalWrite(RELAYFAN_PIN, LOW);
  //} else {
    //FAN OFF
    //digitalWrite(RELAYFAN_PIN, HIGH);
  //}
  //LDR 
  if(persen > persen_terang){
    //LED OFF
    digitalWrite(RELAYLED_PIN, HIGH);
    digitalWrite(RELAYFAN_PIN, HIGH);
    state_led = "OFF";
  } else if (persen > persen_redup && persen < persen_terang){
    //LED ON
    digitalWrite(RELAYLED_PIN, LOW);
    digitalWrite(RELAYFAN_PIN, HIGH);
    state_led = "HALF ON";
  } else{
    //LED ON
    digitalWrite(RELAYLED_PIN, LOW);
    digitalWrite(RELAYFAN_PIN, LOW);
    state_led = "ALL ON";
  }

  //Tampil Oled dan validasi warna interval 1s
  if(millis() - millis1s >= 1000){
    //Reset
    millis1s = millis();
    //Validasi Warna
    //Merah
    if(Red >= 200 && Red <= 235 && Green >= 500 && Green <= 545 && Blue >= 300 && Blue <= 330){
      hasilwarna = " MERAH";
    //Kuning
    } else if(Red >= 140 && Red <= 175 && Green >= 175 && Green <= 205 && Blue >= 250 && Blue <= 275){
      hasilwarna = "KUNING";
    //Biru
    } else if(Red >= 525 && Red <= 625 && Green >= 275 && Green <= 315 && Blue >= 125 && Blue <= 155){
      hasilwarna = " BIRU";
    //Hijau
    } else if(Red >= 560 && Red <= 650 && Green >= 310 && Green <= 400 && Blue >= 360 && Blue <= 410){
      hasilwarna = " HIJAU";
    //Putih
    } else if(Red >= 145 && Red <= 180 && Green >= 145 && Green <= 190 && Blue >= 100 && Blue <= 135){
      hasilwarna = " PUTIH";
    } else {
      hasilwarna = " NULL ";
    }
    //OLED
    oled.clearDisplay();
    oled.setTextSize(2); 
    //US
    if(view == 1){
      oled.setTextColor(WHITE);    
      oled.setCursor(5, 10);      
      oled.println("ULTRASONIC");
      oled.setTextColor(WHITE);  
      oled.setCursor(25, 35);       
      oled.println(String(cm)+" CM"); 
    //LDR
    } else if(view == 2){
      oled.setTextColor(WHITE);    
      oled.setCursor(45, 10);      
      oled.println("LDR");
      oled.setTextColor(WHITE);  
      oled.setCursor(40, 35);       
      oled.println(String(persen)+" %"); 
    //MQ
    } else if(view == 3){
      oled.setTextColor(WHITE);    
      oled.setCursor(55, 10);      
      oled.println("MQ");
      oled.setTextColor(WHITE);  
      oled.setCursor(20, 35);       
      oled.println(String(ppm)+" ppm"); 
    //DHT
    } else if(view == 4){
      oled.setTextColor(WHITE);    
      oled.setCursor(45, 10);      
      oled.println("DHT");
      oled.setTextColor(WHITE);  
      oled.setCursor(25, 35);       
      oled.println(String(celcius)+" C"); 
    //TCS
    } else if(view == 5){
      oled.setTextColor(WHITE);    
      oled.setCursor(45, 10);      
      oled.println("TCS");
      oled.setTextColor(WHITE);  
      oled.setCursor(25, 35);       
      oled.println(String(hasilwarna)); 
    //Pilih Menu
    } else if(view == 99){
      oled.setTextColor(WHITE);    
      oled.setCursor(5, 10);      
      oled.println("PILIH MENU");
      oled.setTextColor(WHITE);  
      oled.setCursor(5, 35);       
      oled.println(String(text_menu)); 
    }
    oled.display();  
    //Print
    Serial.println("Pilih Menu "+String(menu));
    Serial.println("US : "+String(cm)+" cm");
    Serial.println("LDR: "+String(persen)+" %");
    Serial.println("MQ : "+String(ppm)+" ppm");
    Serial.println("DHT: "+String(celcius)+" C");
    Serial.println("TCS: "+String(hasilwarna));
    Serial.println("");
  }    
}

//**** LANJUTAN TCS3200 ****
int getRed() {
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  Frequency = pulseIn(sensorOut, LOW); 
  return Frequency;
}

int getGreen() {
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  Frequency = pulseIn(sensorOut, LOW);
  return Frequency;
}

int getBlue() {
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  Frequency = pulseIn(sensorOut, LOW);
  return Frequency;
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
ldr1:VCC
ldr1:GND
ldr1:DO
ldr1:AO
pot1:GND
pot1:SIG
pot1:VCC
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
bz1:1
bz1:2
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay2:VCC
relay2:GND
relay2:IN
relay2:NC
relay2:COM
relay2:NO
encoder1:CLK
encoder1:DT
encoder1:SW
encoder1:VCC
encoder1:GND
led1:A
led1:C
r1:1
r1:2
led2:A
led2:C
r2:1
r2:2