/*
https://forum.arduino.cc/t/i-need-to-make-a-6-hour-countdown-i-need-help/1278130
*/


#include <LiquidCrystal_I2C.h>

#define BTN_PIN 7

LiquidCrystal_I2C lcd(0x20, 16, 2);

const float BETA = 3950; // should match the Beta Coefficient of the thermistor

float tempValue;
float voltage;
float tempC;
float tempF;
int buzzer = 8;

int hours = 6;
int minutes = 0;
int seconds = 0;

bool countStarted = false;
bool countFin = false;

unsigned long lastCount = 0;
unsigned long intervalCount = 1000;

unsigned long lastBuzz = 0;
int intervalBuzz = 1000;
byte buzzState = 0;
bool buzzFin = false;

unsigned long lastDebounce = 0;
int intervalDebounce = 50;
byte lastBtn = 1;


void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  pinMode(buzzer, OUTPUT);
  pinMode(BTN_PIN, INPUT_PULLUP);

  lcd.setCursor(3, 0);
  lcd.print("TECH FAIR");
  lcd.setCursor(5, 1);
  lcd.print("NOMBRE");
  delay(2000);
  lcd.clear();

}

void loop() {

  unsigned long now = millis();

  if (millis() - lastDebounce >= intervalDebounce) {
    byte state = digitalRead(BTN_PIN);
    if (state != lastBtn) {
      lastBtn = state;
      lastDebounce = millis();
      if (state == HIGH) {
        //button pressed and released
        if (!countStarted) {
          StartCount();
        }
      }
    }
  }


  tempValue = analogRead(A0);
 // voltage = (tempValue * 5) / 1024;
 // tempC = (voltage - 0.5) * 100;
 // tempF = (tempC * 1.8) + 32;

  tempC = 1 / (log(1 / (1023. / tempValue - 1)) / BETA + 1.0 / 298.15) - 273.15;


  if (tempC >= 28 && !countStarted) {
    Serial.print("TempC:");
    Serial.print(tempC);
    StartCount();
  }

  if (countStarted && !countFin) {
    if (now - lastCount >= intervalCount) {
      lastCount = now;
      countFin = timer();
    }
  } else if (countStarted && countFin) {
    if (!buzzFin) {
      buzz();
    } else {
      //finsihed the buzzing..
      //reset everythings..
      buzzFin = false;
      countFin = false;
      countStarted = false;
      lcd.clear();
    }

  }
}

//starts the count down
void StartCount() {
  countStarted = true;
  lastCount = millis() - 1001;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Countdown:");
  hours = 6;
  minutes = 0;
  seconds = 0;
  intervalBuzz = 1000;
  buzzState = 0;
}

bool timer() {
  bool result = false;
  // formato HH:MM:SS
  lcd.setCursor(0, 1);
  lcd.print(hours);
  lcd.print(":");
  if (minutes < 10) {
    lcd.print("0"); // zero para los digitos de 1
  }
  lcd.print(minutes);
  lcd.print(":");
  if (seconds < 10) {
    lcd.print("0");
  }
  lcd.print(seconds);
  int left = hours + minutes + seconds;
  if (left == 0) {
    //all done
    result = true;
  }

  // quitale al countdown
  if (seconds > 0) {
    seconds--;
  } else {
    if (minutes > 0) {
      minutes--;
      seconds = 59;
    } else {
      if (hours > 0) {
        hours--;
        minutes = 59;
        seconds = 59;
      }
    }
  }

  return result;
}

void buzz() {

  switch (buzzState) {
    case 0:  tone(buzzer, 392); lastBuzz = millis(); buzzState++; break;
    case 1: if (millis() - lastBuzz >= intervalBuzz) {
        buzzState++;
        noTone(buzzer);
        intervalBuzz = 500;
        lastBuzz = millis();
      }
      break;
    case 2: if (millis() - lastBuzz >= intervalBuzz) {
        buzzState++;
        intervalBuzz = 1000;
        tone(buzzer, 175);
        lastBuzz = millis();
      }
      break;
    case 3: if (millis() - lastBuzz >= intervalBuzz) {
        buzzState = 0;
        noTone(buzzer);
        buzzFin = true;
      }
      break;
  }
}