// written by Dylon Jamna, modified by Micah Beeler
// include the library code
#include <LiquidCrystal.h>// include the library code
int tempPin = A0;  // make variables// thermistor is at A0
int tup = 7; //Temp up button
int tdown = 9; //Temp down button
int tcf = 8; //F to C toggle
boolean far = true; //Default F to true
boolean cel = false; //Default C to false
boolean lightMe; //Light up LED
char abbrv; //Shortcut for C or F abbreviation
float temp; //Raw temp value
float settemp; //Desired temp
int defF = 65; //Default Far.
int defC = 19; //Default Cel.
int upstate = 0; //Temp up button state
int downstate = 0; //Temp down button state 
int tcfstate = 0; //C to F button state
int ledPin = 13; //LED's pin
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd is at 12,11,5,4,3,2 on the breadboard

void setup() {
  Serial.begin (9600); // set the serial monitor tx and rx speed
  lcd.begin(16, 2); // set up all the "blocks" on the display
  lcd.setCursor(0, 0); // set the cursor to colum 0 row 0
  lcd.print("hello, world!"); // display hello world for 1 second
  lcd.clear(); // clear the lcd
  pinMode(tup, INPUT); //Set temp up button to input
  pinMode(tdown, INPUT); //Set temp down button to input
  pinMode(tcf, INPUT); //Set C to F button to input
  pinMode(ledPin, OUTPUT); //Set LED to output
}

void loop() {
  upstate = digitalRead(tup); //Read state of temp up
  downstate = digitalRead(tdown); //Read state of temp down
  tcfstate = digitalRead(tcf); //Read state of C to F button
  int tvalue = analogRead(tempPin);  // make tvalue what ever we read on the tempPin

  if (tcfstate == LOW) { //If the C to F button is pressed
    if (far == true) { //And Farenheit is true already
      far = false;
      cel = true; //Enable Celsius
    }
    else if (far == false) { //Otherwise if Farenheit isn't true
      far = false; //Enable Farenheit
      cel = true;
    }
  }

  if (far == true) { //If Farenheit is true
    temp = farenheit(tvalue); //Caclulate temp in F
    abbrv = 'F'; //Set label to F
  }
  if (cel == true) { //If Celsius is true
    temp = celsius(tvalue); //Calculate temp in C
    abbrv = 'C'; //Set label to C
  }

  if (upstate == LOW) { //if up button is pressed,
    defF = defF + 1; //raise desired F by 1
    defC = defC + 1; //Raise desired C by 1
  }
  if (downstate == LOW) { //if down button is pressed
    defF = defF - 1; //lower desired F by 1
    defC = defC - 1; //lower desired C by 1
  }

  lcd.setCursor(0, 0);
  lcd.print("Tek-aya ");
  lcd.print (temp);  // Print the current temp in f
  lcd.print (abbrv);
  delay(255);
  if (cel == true) {
    lcd.setCursor (0, 1); // set the cursor to 0,1
    lcd.print ("Ystavka "); // Print set to and your ideal temperature in f
    lcd.print (defC);
    lcd.print (abbrv);
  }
  if (far == true) {
    lcd.setCursor (0, 1); // set the cursor to 0,1
    lcd.print ("Desired "); // Print set to and your ideal temperature in f
    lcd.print (defF);
    lcd.print (abbrv);
  }

  lightUp(temp, defC, defF); //run custom light up command
  
  if (lightMe == true) { //if light up is true
    digitalWrite(ledPin, HIGH); //turn on LED
  } else if (lightMe == false) { //otherwise
    digitalWrite(ledPin, LOW); //turn off LED
  }
} // we're done

float farenheit(float raw) { //calculate far value
  float f; //create temporary variable F
  f = raw / 2.07; //calculate temperature from raw value
  return f; //return temperature
}

float celsius(float raw) { //calculate cel value
  float c; //create temp variable C
  c = raw / 7.1; //calculate temperature from raw value
  return c; //return temperature
}

boolean lightUp(float act, int desC, int desF) { //Decides if LED should light up, imports current temp, desired C, and desired F
  if (act < desC || act < desF) { //if the actual is less than the desired F or C value
    lightMe = true; //Turn on the LED
  } else { //otherwise
    lightMe = false; //Turn the LED off
  }
  return lightMe; //return that value
}