#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"

LiquidCrystal_I2C lcd(0x27, 20, 4);  // Alamat LCD I2C dan ukuran kolom dan baris
DHTesp dhtSensor;

const int DHT_PIN = 8;
const int relay = 3;
const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13;

int SettingFlowRate = A3;
float val_flow = 0;

int Settingtekanan = A1;
float val_tekanan = 0;

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

  // Inisialisasi LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("KELOMPOK 2");
  lcd.setCursor(2, 1);
  lcd.print("Kelas B - HW");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Anggota:");
  lcd.setCursor(0, 1);
  lcd.print("1. Siroj Al Quro");
  lcd.setCursor(0, 2);
  lcd.print("2. Azhani Syahputra");
  lcd.setCursor(0, 3);
  lcd.print("3. Dinda Rintic Rose");
  delay(2000);
  lcd.clear();

  // Setup pin untuk relay dan LED
  pinMode(relay, OUTPUT);

  // Setup pin untuk LED indikator tekanan
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

  // Setup sensor DHT22
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}

void loop() {
  // Baca nilai dari sensor flow rate (misalnya menggunakan potensiometer analog)
  int nilai_analog = analogRead(SettingFlowRate);
  int nilai_convert = map(nilai_analog, 0, 1023, 0, 255);
  val_flow = nilai_convert * 20.0 / 1023.0;
  Serial.println("Nilai FLOW = " + String(val_flow));
  delay(500);

  // Baca nilai dari sensor tekanan (misalnya menggunakan potensiometer analog)
  int nilai_analog_press = analogRead(Settingtekanan);
  float nilai_convert_press = map(nilai_analog_press, 0, 1023, 0, 255);
  val_tekanan = nilai_convert_press / 500 * 3;
  Serial.println("Nilai TEKANAN = " + String(val_tekanan));
  delay(500);

  // Baca suhu dan kelembaban dari sensor DHT22
  TempAndHumidity data = dhtSensor.getTempAndHumidity();
  Serial.println("Suhu: " + String(data.temperature, 2) + "°C - Kelembaban: " + String(data.humidity, 2) + "%");

  // Tampilkan informasi di LCD
  updateLCD(data.humidity, val_flow, val_tekanan);

  // Kontrol relay berdasarkan nilai tekanan
  if (val_tekanan > 1.5) {
    digitalWrite(relay, HIGH); // Aktifkan relay jika tekanan tinggi
  } else {
    digitalWrite(relay, LOW);  // Matikan relay jika tekanan rendah
  }

  // Kontrol LED indikator tekanan
  displayPressureIndicator();

  delay(1000);
}

void updateLCD(float humidity, float flow, float pressure) {
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print("NASAL CANULA");
  lcd.setCursor(0, 1);
  lcd.print("OKYGEN   =");
  lcd.setCursor(11, 1);
  lcd.print(humidity, 2);
  lcd.setCursor(16, 1);
  lcd.print("%");
  lcd.setCursor(0, 2);
  lcd.print("FLOW RATE=");
  lcd.setCursor(11, 2);
  lcd.print(flow);
  lcd.setCursor(16, 2);
  lcd.print("L/m");
  lcd.setCursor(0, 3);
  lcd.print("TEKANAN  =");
  lcd.setCursor(11, 3);
  lcd.print(pressure);
  lcd.setCursor(16, 3);
  lcd.print("bar");
}


void displayPressureIndicator() {
  if (val_tekanan <= 0) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  } else if (val_tekanan <= 1) {
    digitalWrite(led4, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led1, HIGH);
  } else if (val_tekanan <= 1.8) {
    digitalWrite(led4, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led1, HIGH);
  } else if (val_tekanan <= 3) {
    digitalWrite(led4, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led1, LOW);
  } else if (val_tekanan >= 4) {
    digitalWrite(led4, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led1, HIGH);
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module