//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
const int stepsPerRevolution = 50;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int power = 1;
int pwr = 0;
void setup()
{
  lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Temperatura:");
  pinMode(LED_BUILTIN, OUTPUT);
  lcd.setCursor(7,2);
  lcd.print("Inchis ");
  myStepper.setSpeed(90);

}

void Motor(int sens)
{
  pwr=0;
  if(sens==1)
  {while(pwr<=19)
    {
      lcd.setCursor(7,2);
      lcd.print("Inchis ");
      digitalWrite(LED_BUILTIN, HIGH);
      myStepper.step(-stepsPerRevolution);
      lcd.setCursor(19-pwr,3);
      lcd.print(" ");
      pwr++;
    }}
    else
    {while(pwr<=19)
    {
      lcd.setCursor(7,2);
      lcd.print("Deschis ");
      digitalWrite(LED_BUILTIN, HIGH);
      myStepper.step(stepsPerRevolution);
      lcd.setCursor(pwr,3);
      lcd.print("X");
      pwr++;
    }}
    digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
  int analogValue = analogRead(A0);
  
  float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
  if (celsius > 40)
    {
     if (power == 1)
         pwr = 1;
      power = 9;}
    else if (celsius <28)
         { 
          if (power == 9)
             pwr = 1;
           power = 1;}
  
  lcd.setCursor(13,0);
  lcd.print(celsius);
  if (power == 1 && pwr == 1){
    Motor(1);
    pwr = 0;      
    }
  
  else if (power == 9 && pwr == 1){
    Motor(0);
    pwr = 0;
    }
  
  
  
  delay(500);
}