/*
  Average NTC Thermistor

  Reads a temperature from the NTC 3950 thermistor,
  averages and displays it in the default Serial.

  https://github.com/YuriiSalimov/NTC_Thermistor

  Created by Yurii Salimov, May, 2019.
  Released into the public domain.
*/
#include <U8g2lib.h>
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
#include <Thermistor.h>
#include <NTC_Thermistor.h>
#include <AverageThermistor.h>

#define SENSOR_PIN             A1
#define REFERENCE_RESISTANCE   8000
#define NOMINAL_RESISTANCE     100000
#define NOMINAL_TEMPERATURE    25
#define B_VALUE                3950

/**
  How many readings are taken to determine a mean temperature.
  The more values, the longer a calibration is performed,
  but the readings will be more accurate.
*/
#define READINGS_NUMBER 10

/**
  Delay time between a temperature readings
  from the temperature sensor (ms).
*/
#define DELAY_TIME 10

Thermistor* thermistor = NULL;

#define pa 5
#define pb 6
#define bt 8
#define led 10
int dem=0; int hientai; int bandau;

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
pinMode(bt, INPUT_PULLUP);
pinMode(led , OUTPUT);
pinMode(pa , INPUT);
pinMode(pb , INPUT);
bandau= digitalRead(pa);
  Thermistor* originThermistor = new NTC_Thermistor(
    SENSOR_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE
  );
  thermistor = new AverageThermistor(
    originThermistor,
    READINGS_NUMBER,
    DELAY_TIME
  );

  /* OR
    thermistor = new AverageThermistor(
      new NTC_Thermistor(
      SENSOR_PIN,
      REFERENCE_RESISTANCE,
      NOMINAL_RESISTANCE,
      NOMINAL_TEMPERATURE,
      B_VALUE
    ),
    READINGS_NUMBER,
    DELAY_TIME
  );
  */u8g2.begin();
}

// the loop function runs over and over again forever
void loop() {
  // Reads temperature
  const double celsius = thermistor->readCelsius();
  const double kelvin = thermistor->readKelvin();
  const double fahrenheit = thermistor->readFahrenheit();
 hientai = digitalRead(pa);   
   if (hientai != bandau)
   {     
     if (digitalRead(pb) != hientai) 
     { 
       dem ++;
     } 
     else 
     {
       dem --;
     }
   } 
   bandau = hientai;
  // Output of information
  Serial.print("Temperature: ");
  Serial.print(celsius);
  Serial.print(" C, ");
  Serial.print(kelvin);
  Serial.print(" K, ");
  Serial.print(fahrenheit);
  Serial.println(" F  ");
  u8g2.clearBuffer();					// clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
  u8g2.drawStr(0,10,"Hello World!");
  u8g2.setCursor(0, 25);
  u8g2.print(celsius);	// write something to the internal memory
  u8g2.setCursor(0, 40);
  u8g2.print(dem);	// write something to the internal memory
  u8g2.sendBuffer();	
  if(digitalRead(bt) == LOW){
  digitalWrite(led, HIGH);
} else {
  digitalWrite(led, LOW);
} // optionally, only to delay the output of information in the example.
}