#include <Wire.h>              // include Arduino wire library (required for I2C devices)
#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_SSD1306.h>  // include Adafruit SSD1306 OLED display driver

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int ThermistorPin = 10;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;


void setup() {
  
 delay(1000);
 
 Serial.begin(9600);
 
  //analogReference(INTERNAL);  // set positive reference voltage to 1.1V
 
  // initialize the SSD1306 OLED display with I2C address = 0x3D
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
 
  // clear the display buffer.
  display.clearDisplay();
 
  display.setTextSize(1);   // text size = 1
  display.setTextColor(WHITE, BLACK);  // set text color to white and black background
  display.setCursor(5, 0);            // move cursor to position (15, 0) pixel
  display.print("CHAMBER TEMPERATURE:");
  display.display();        // update the display
  display.setTextSize(3);   // text size = 2
}


void loop() {

  Vo = analogRead(ThermistorPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  Tc = T - 273.15;

  Serial.print("Teplota = ");
  Serial.print(Tc);
  Serial.print("\n");
 
if ((Tc >= 100)||(Tc < 0)){  //pokud je cislo vetsi jak 100 nebo jde do minusu tak tiskni od zacatku
   
  display.setCursor(1, 20);
  display.print(Tc, 1);
  
  if ((Tc < 0) && (Tc > -10)){ //pokud je mezi 0 a -9.99 tak dve desetinna a od zacatku
  display.setCursor(1, 20);
  display.print(Tc, 2);
  }
 }

else {
   if ((Tc < 10) && (Tc >= 0)){ //pokud je mensi nez 9.99 do 0 tak udela mezeru a 2 desetinna
 
  display.setCursor(1,20);
  display.print("   ");
  display.setCursor(22, 20);
  display.print(Tc, 2);
  }
  
  display.setCursor(1,20); //pokud neni vesti jak 100 a nejde do minusu a neni v -9.99... 9.99 intervalu tak udelej mezeru a jen jedno desetinny
  display.print("   ");
  display.setCursor(22, 20);
  display.print(Tc, 1);
  }
 
  // print ( °C )
  display.drawCircle(100, 24, 4, WHITE);
  display.setCursor(110,20);
  display.print("C");
 
  // update the display
  display.display();

  delay(500);
 
}
