/*
  NTC Temperature Sensor using Franzininho, NTC and SSD1306
  by Anderson Costa with ❤ for the Wokwi community
  Modify by Leko 7-seg with 74hc595 tabelado 20 a 45 graus
  Tabela site http://www.sebulli.com/ntc/index.php
  Visit https://wokwi.com to learn about the Wokwi Simulator
  Visit https://franzininho.com.br to learn about the Franzininho
*/
#include <ShiftDisplay.h>
//const int DISPLAY_SIZE = 5; // number of digits on display
#define latchPin PB0
#define clockPin PB1
#define dataPin  PB2
ShiftDisplay display(latchPin, clockPin, dataPin, COMMON_ANODE, 3);

#define NTC_PIN A3

/**
* The NTC table has 129 interpolation points.
* Unit:0.01 °C
*
*/
int NTC_table[129] = {
  23303, 19685, 16067, 14182, 12932, 12006, 
  11274, 10671, 10160, 9717, 9326, 8977, 8661, 
  8372, 8107, 7862, 7633, 7419, 7218, 7028, 
  6849, 6678, 6515, 6360, 6211, 6068, 5930, 
  5797, 5669, 5545, 5425, 5309, 5196, 5086, 
  4979, 4874, 4772, 4673, 4575, 4480, 4387, 
  4295, 4205, 4117, 4030, 3944, 3860, 3777, 
  3696, 3615, 3536, 3457, 3379, 3302, 3226, 
  3151, 3077, 3003, 2929, 2857, 2784, 2713, 
  2641, 2570, 2500, 2430, 2360, 2290, 2221, 
  2152, 2083, 2014, 1945, 1876, 1807, 1739, 
  1670, 1601, 1532, 1463, 1393, 1323, 1253, 
  1183, 1113, 1041, 970, 898, 825, 752, 678, 
  604, 528, 452, 375, 296, 217, 136, 54, -29, 
  -114, -200, -288, -379, -471, -566, -663, 
  -763, -867, -973, -1084, -1199, -1318, -1443, 
  -1575, -1713, -1859, -2015, -2182, -2363, 
  -2560, -2778, -3023, -3304, -3637, -4050, 
  -4603, -5483, -6363
};

void setup() {

}

void loop() {
  static unsigned long startTime = 0;
  unsigned long currentTime;
  // Retorna o número de milissegundos passados
  // desde que começou a executar o programa atual
  currentTime = millis();

  // Verifica se passou 1 segundo
  if ((currentTime - startTime) >= 1000) {
    // Reseta o temporizador
    startTime = currentTime;
    // Atualiza o display
    updateDisplay();
  }
}

void updateDisplay()
{
  // Atualiza a temperatura
  float temperature = getTemperature();
  //display.set(temperature, 1); // float with one decimal place
  display.show(temperature, 1000, 1, ALIGN_CENTER);
}


float getTemperature()
{
  unsigned int analogValue = analogRead(NTC_PIN);
  //float celsius = 1 / (log(1 / ((1023.0 / analogValue) - 1)) / BETA + 0.003354) - 273.15;
  //float celsius = BETA / (13.2483-log(1023.0 / analogValue -1)) - 273.15;
  float celsius=(NTC_ADC2Temperature(analogValue)/100.0)-0.1;
  return celsius;
}

/**
* \brief    Converts the ADC result into a temperature value.
*
*           P1 and p2 are the interpolating point just before and after the
*           ADC value. The function interpolates between these two points
*           The resulting code is very small and fast.
*           Only one integer multiplication is used.
*           The compiler can replace the division by a shift operation.
*
*           In the temperature range from 20°C to 45°C the error
*           caused by the usage of a table is 0.014°C
*
* \param    adc_value  The converted ADC result
* \return              The temperature in 0.01 °C
*
*/
int NTC_ADC2Temperature(unsigned int adc_value){
 
  int p1,p2;
  /* Estimate the interpolating point before and after the ADC value. */
  p1 = NTC_table[ (adc_value >> 3)  ];
  p2 = NTC_table[ (adc_value >> 3)+1];
 
  /* Interpolate between both points. */
  return p1 - ( (p1-p2) * (adc_value & 0x0007) ) / 8;
}
74HC595
74HC595