//NTC
#define ntc_pin A0         // Pin, to which the voltage divider is connected
#define vd_power_pin 2        // 5V for the voltage divider
#define nominal_resistance 10000       //Nominal resistance at 25⁰C
#define nominal_temeprature 25   // temperature for nominal resistance (almost always 25⁰ C)
#define samplingrate 5    // Number of samples
#define beta 3950  // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
#define Rref 10000   //Value of  resistor used for the voltage divider
//NTC


//  Display I2C
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   4

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

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

//NTC
int samples = 0;   //array to store the samples
int average = 0;   //array to store the samples
//NTC








/////////////////////////////////////////////////////////////////////////////
void setup()
{


// Init
  lcd.init();
  lcd.backlight();
 

//NTC
  pinMode(vd_power_pin,OUTPUT);
  Serial.begin(9600);
//NTC



}


///////////////////////////////////////////////////////////////////////
void loop()
{
 
//  int analogValue_GT1 = analogRead(A0);
//  float celsius_GT1 = 1 / (log(1 / (1023. / analogValue_GT1 - 1)) / BETA + 1.0 / 298.15) - 273.15;


//NTC
  uint8_t i;
  float average;
samples = 0;
  // take voltage readings from the voltage divider
digitalWrite(vd_power_pin,HIGH);
  for (i=0; i< samplingrate; i++) {
   samples += analogRead(ntc_pin);
   delay(10);
  }
digitalWrite(vd_power_pin,LOW);
// average = 0;
  average = samples / samplingrate;
//  Serial.print("ADC readings ");
//  Serial.println(average);
  // Calculate NTC resistance
  average = 1023 / average - 1;
  average = Rref / average;
//  Serial.print("Thermistor resistance ");
//  Serial.println(average);
  float temperature;
  temperature = average / nominal_resistance;     // (R/Ro)
  temperature = log(temperature);                  // ln(R/Ro)
  temperature /= beta;                   // 1/B * ln(R/Ro)
  temperature += 1.0 / (nominal_temeprature + 273.15); // + (1/To)
  temperature = 1.0 / temperature;                 // Invert
  temperature -= 273.15;                         // convert absolute temp to C
//  Serial.print("Temperature ");
//  Serial.print(temperature);
//  Serial.println(" *C");
//  delay(1000);
//NTC



  lcd.setCursor(0, 0);
  lcd.print("RAW ");
  lcd.println(samples); 
  lcd.setCursor(0, 1);
  lcd.print("ADC ");
  lcd.println(average, 1);
  lcd.setCursor(0, 2);
  lcd.print("Resist ");;
  lcd.println(average, 1);
  lcd.setCursor(0, 3);
  lcd.print("Temp ");
  lcd.print(temperature, 1);
  lcd.println(" *C");



  







}