#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
#define btnA 0
#define buzz 33
#define vrPin 36
#define redPin 25
#define greenPin 26
#define bluePin 27
int PWM_FREQUENCY = 5000;
int PWM_CHANNEL = 0;
int PWM_CHANNEL_1 = 1;
int PWM_CHANNEL_2 = 2;
int PWM_CHANNEL_4 = 4;
int PWM_RESOUTION = 8;
int GPIOPINRED = 25;
int GPIOPINGREEN = 26;
int GPIOPINBLUE = 27;
int MIN = -40; // Set default value for temp.
int MAX = 125;
void setup() {
  Serial.begin(9600);
  pinMode(btnA, INPUT);
  pinMode(vrPin, INPUT);
  // ledcSetup(channel, frequency, resolution)
  ledcSetup(PWM_CHANNEL, PWM_FREQUENCY, PWM_RESOUTION);
  ledcSetup(PWM_CHANNEL_1, PWM_FREQUENCY, PWM_RESOUTION);
  ledcSetup(PWM_CHANNEL_2, PWM_FREQUENCY, PWM_RESOUTION);
  //ledcAttachPin(pin, channel)
  ledcAttachPin(GPIOPINRED, PWM_CHANNEL);
  ledcAttachPin(GPIOPINGREEN, PWM_CHANNEL_1);
  ledcAttachPin(GPIOPINBLUE, PWM_CHANNEL_2);
  ledcAttachPin(buzz, PWM_CHANNEL_4);
  ledcWriteNote(PWM_CHANNEL_4, NOTE_C, 5);
  delay(100);
  ledcWriteNote(PWM_CHANNEL_4, NOTE_E, 5);
  delay(100);
  ledcWriteNote(PWM_CHANNEL_4, NOTE_G, 5);
  delay(500);
  ledcDetachPin(buzz);

  if (!sht31.begin(0x44)) { // Set to 0x45 foralternate I2C address
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
  Serial.println("Setup Completed. . .");

}
void loop() {

  float t = sht31.readTemperature();
  if (!isnan(t)) { // check if 'is not a number'
    Serial.print("Temp : ");
    Serial.print(t);
    Serial.println(" °C");
  } else {
    Serial.println("Failed to read temperature");
  }

  delay(200);

  /*
    int t;
    int vr = map(analogRead(vrPin), 0, 4095, -40, 125);
    t = vr;
    Serial.print("Temp : ");Serial.print(t);Serial.println(" °C");
  */

  if (t <= 32) { // ฟ้าเข้ม
    ctrLEDRGB(18, 45, 255);
  } else if (t > 32 && t < 36) { // สม้อ่อน
    ctrLEDRGB(255, 108, 0);
  } else if (t >= 36) { // แดงอ่อน
    ctrLEDRGB(250, 30, 30);
  }
  if (t < MIN || t > MAX) {
    Serial.println("!! Temperature Warning !!!");
    ledcAttachPin(buzz, PWM_CHANNEL_4);
    ledcWriteNote(PWM_CHANNEL_4, NOTE_G, 6);
    delay(100);
    ledcDetachPin(buzz);
  }
  // loop for set MIN MAX by VResistor and button.
  int btnVal = digitalRead(btnA);
  int state = 0;
  if (btnVal == 0) {

    state = state + 1;
    ledcAttachPin(buzz, PWM_CHANNEL_4);
    ledcWriteNote(PWM_CHANNEL_4, NOTE_C, 7);
    delay(100);
    ledcWriteNote(PWM_CHANNEL_4, NOTE_E, 7);
    delay(100);
    ledcDetachPin(buzz);
  }
  while (state == 1) {
    ctrLEDRGB(0, 0, 20);
    int vr = map(analogRead(vrPin), 0, 4095, -40, 125);
    Serial.print("Set Minimum temp. : ");
    Serial.print(vr);
    Serial.println("°C");
    btnVal = digitalRead(btnA);
    if (btnVal == 0) state = state + 1;
    if (state == 2) {
      delay(200);
      ledcAttachPin(buzz, PWM_CHANNEL_4);
      ledcWriteNote(PWM_CHANNEL_4, NOTE_E, 7);
      delay(200);
      ledcDetachPin(buzz);
      MIN = vr;
      while (state == 2) {
        ctrLEDRGB(20, 0, 0);
        int vr = map(analogRead(vrPin), 0, 4095, -40,

                     125);

        Serial.print("Set Maximum temp. : ");
        Serial.print(vr);
        Serial.println("°C");
        btnVal = digitalRead(btnA);
        delay(100);

        if (btnVal == 0) state = state + 1;
        if (state == 3) {
          MAX = vr;
          Serial.print("- Minimum temp. set to ");
          Serial.print(MIN);
          Serial.println(" °C -");
          Serial.print("- Maximum temp. set to ");
          Serial.print(MAX);
          Serial.println(" °C -");
          state = 0;
          ledcAttachPin(buzz, PWM_CHANNEL_4);
          ledcWriteNote(PWM_CHANNEL_4, NOTE_E, 7);
          delay(100);
          ledcWriteNote(PWM_CHANNEL_4, NOTE_C, 7);
          delay(100);
          ledcDetachPin(buzz);
          break;
        }
      }
    }
  }
  /* for dev. serial monitor -
    Serial.print("Min value : "); Serial.println(MIN);
    Serial.print("Max value : "); Serial.println(MAX);
    Serial.println("- - - - -");
  */
}
void ctrLEDRGB(int R, int G, int B) {

  // ledcWrite(channel, duty )
  ledcWrite(PWM_CHANNEL, map(R, 0, 255, 255, 0));
  ledcWrite(PWM_CHANNEL_1, map(G, 0, 255, 255, 0));
  ledcWrite(PWM_CHANNEL_2, map(B, 0, 255, 255, 0));
}