#include <LiquidCrystal.h>
#define THERMISTOR_1_PIN 2
#define THERMISTOR_2_PIN 1
#define NUMTEMPS1 20
#define NUMTEMPS2 20

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

byte degr[8] = {
 0b01110,
 0b10001,
  0b10001,
  0b01110,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};


int hysteresis = 0;
int pot1 = 0;
int pot2 = 0;

short temptable1[NUMTEMPS1][2] = {
   {1, 841},
   {54, 255},
   {107, 209},
   {160, 184},
   {213, 166},
   {266, 153},
   {319, 142},
   {372, 132},
   {425, 124},
   {478, 116},
   {531, 108},
   {584, 101},
   {637, 93},
   {690, 86},
   {743, 78},
   {796, 70},
   {849, 61},
   {902, 50},
   {955, 34},
   {1008, 3}
};

short temptable2[NUMTEMPS2][2] = {
   {1, 841},
   {54, 255},
   {107, 209},
   {160, 184},
   {213, 166},
   {266, 153},
   {319, 142},
   {372, 132},
   {425, 124},
   {478, 116},
   {531, 108},
   {584, 101},
   {637, 93},
   {690, 86},
   {743, 78},
   {796, 70},
   {849, 61},
   {902, 50},
   {955, 34},
   {1008, 3}
};

void setup()
{
   Serial.begin(9600);
   
     
   pinMode(9, OUTPUT);    
   
   pinMode(8,OUTPUT);      
  
   lcd.createChar(0, degr);     
 

 
  lcd.begin(16, 2);
 
  
}

void loop()
{
  
  
  pot1 = analogRead(A5);   
  pot1 = map(pot1, 0, 1023, 0, 255);
  
  pot2 = analogRead(A3);  
  pot2 = map(pot2, 0, 1023, 0, 255);

  hysteresis = analogRead(A4); 
  hysteresis = map(hysteresis, 0, 1023, 0, 9);
  
   int rawvalue1 = analogRead(THERMISTOR_1_PIN);
   int celsius1 = read_temp1();
   int fahrenheit1 = (((celsius1 * 9) / 5) + 32);

   Serial.print("Current temp1: ");
   Serial.print(celsius1);
   Serial.print("C / ");
   Serial.print(fahrenheit1);
   Serial.println("F");
   
   Serial.print("Raw value1: ");
   Serial.println(rawvalue1);
   Serial.println(" ");

  
   int rawvalue2 = analogRead(THERMISTOR_2_PIN);
   int celsius2 = read_temp2();
   int fahrenheit2 = (((celsius2 * 9) / 5) + 32);

   Serial.print("Current temp2: ");
   Serial.print(celsius2);
   Serial.print("C / ");
   Serial.print(fahrenheit2);
   Serial.println("F");
   
   Serial.print("Raw value2: ");
   Serial.println(rawvalue2);
   Serial.println(" ");



 
 
 

 
 
  lcd.setCursor(0,0);     
 lcd.print("E");
 lcd.print(celsius1);
 lcd.print ((char)0);
 lcd.print("C");
 
 

lcd.setCursor(7,0);       
 lcd.print("S");
 lcd.print ((pot1),1);
 lcd.print ((char)0);
 lcd.print("C");
 
              
 
 
   lcd.setCursor(0,1);    
 lcd.print("T");
 lcd.print(celsius2);
 lcd.print ((char)0);
 lcd.print("C");
 



lcd.setCursor(7,1);       
 lcd.print("S");
 lcd.print ((pot2),1);
 lcd.print ((char)0);
 lcd.print("C");
 
  lcd.setCursor(14,0);     
 lcd.print("H");
 lcd.print(hysteresis);
 
 

  delay(50);
 
 
  if (celsius2 < 100){
 lcd.setCursor(5,1);
 lcd.print(" ");
  }
 else{}
 
   if (celsius2 < 10){
 lcd.setCursor(4,1);
 lcd.print(" ");
  }
 else{}



 if (pot2 < 100){
 lcd.setCursor(12,1);
 lcd.print(" ");
  }
 else{}
 
   if (pot2 < 10){
 lcd.setCursor(11,1);
 lcd.print(" ");
  }
 else{}
 
 

  if (pot1 < 100){
 lcd.setCursor(12,0);
 lcd.print(" ");
  }
 else{}
 
   if (pot1 < 10){
 lcd.setCursor(11,0);
 lcd.print(" ");
  }
 else{}
 
 

 
 if (celsius1 < 100){
 lcd.setCursor(5,0);
 lcd.print(" ");
  }
 else{}
 
   if (celsius1 < 10){
 lcd.setCursor(4,0);
 lcd.print(" ");
  }
 else{}
 
 
 

 
 
  if (pot1 > celsius1 + (hysteresis/2)){  
       digitalWrite(8,HIGH);
  }
    else{}
    
  if (pot1 < celsius1 - (hysteresis/2)){
   
    digitalWrite(8,LOW);
  }
    else{}
    
    
    
    
    if (pot2 > celsius2+(hysteresis/2)){  
       digitalWrite(9,HIGH);
  }
    else{}
    
  if (pot2 < celsius2-(hysteresis/2)){
    
    digitalWrite(9,LOW);
  }
    else{}
}

int read_temp1()
{
   int rawtemp1 = analogRead(THERMISTOR_1_PIN);
   int current_celsius1 = 0;

   byte i;
   for (i=1; i<NUMTEMPS1; i++)
   {
      if (temptable1[i][0] > rawtemp1)
      {
         int realtemp1  = temptable1[i-1][1] + (rawtemp1 - temptable1[i-1][0]) * (temptable1[i][1] - temptable1[i-1][1]) / (temptable1[i][0] - temptable1[i-1][0]);

         if (realtemp1 > 255)
            realtemp1 = 255;

         current_celsius1 = realtemp1;

         break;
      }
   }

   
   if (i == NUMTEMPS1)
   current_celsius1 = 0;

   return current_celsius1;
}


   

int read_temp2()
{
   int rawtemp2 = analogRead(THERMISTOR_2_PIN);
   int current_celsius2 = 0;

   byte i;
   for (i=1; i<NUMTEMPS2; i++)
   {
      if (temptable2[i][0] > rawtemp2)
      {
         int realtemp2  = temptable2[i-1][1] + (rawtemp2 - temptable2[i-1][0]) * (temptable2[i][1] - temptable2[i-1][1]) / (temptable2[i][0] - temptable2[i-1][0]);

         if (realtemp2 > 255)
            realtemp2 = 255;

         current_celsius2 = realtemp2;

         break;
      }
   }

   
   if (i == NUMTEMPS2)
   current_celsius2 = 0;

   return current_celsius2;
}