/* thermometer_NTC_ino */


/* --- Temperature calculation  --- */
float temperatureNTC( unsigned int ADvalue );

/* --- serial shift 3 digit seven-segment display  --- */
#define SDI_PIN  4
#define SCLK_PIN 3
#define LOAD_PIN 2
#define DIGITS   3
void sevseg_temperature(unsigned char *, unsigned int);
void ShiftOut_array(unsigned char *, unsigned char length);



void setup() {

  Serial.begin(9600);
  pinMode(LOAD_PIN, OUTPUT);
  pinMode(SDI_PIN, OUTPUT);
  pinMode(SCLK_PIN, OUTPUT);
  delay(500);
}

void loop() {

  unsigned int ADvalue = analogRead(A0);
  unsigned char sevseg_array[ DIGITS ];
  float celsius;
  
  celsius = temperatureNTC( ADvalue ) ;
  
  Serial.print("Temperature: ");
  Serial.print(celsius);
  Serial.println(" ℃");

  sevseg_temperature( sevseg_array, celsius *10.0 );
  ShiftOut_array( sevseg_array, DIGITS );

  delay(1000);
}







/* *********************************** */
/*            FUNCTIONS                */
/* *********************************** */

/* ------------------------ */
/*  Temperature conversion  */
/* ------------------------ */

float temperatureNTC( unsigned int ADvalue ) //  using Steinhart-Hart 
{
const float BETA = 3950; 
// should match the Beta Coefficient of the thermistor

float celsius = 1 / (log(1 / (1023. / ADvalue - 1)) / BETA + 1.0 / 298.15) - 273.15;
if(celsius < 0) celsius = 0;
return celsius;
}


/* ----------------------------- */
/*  Shiftregister 7-seg display  */
/* ----------------------------- */

void sevseg_temperature(unsigned char sevseg_array[], unsigned int temperature)
{
  unsigned char data;
  unsigned int temp;

  unsigned char NUMBERS[] = {
  // "0123465789 -" for active Low display
   0x03,  // 0000 0011  "0"
   0x9F,  // 1001 1111  "1"
   0x25,  // 0010 0101  "2"
   0x0D,  // 0000 1101  "3"
   0x99,  // 1001 1001  "4"
   0x49,  // 0100 1001  "5"
   0x41,  // 0100 0001  "6"
   0x1F,  // 0001 1111  "7"
   0x01,  // 0000 0001  "8"
   0x19,  // 0001 1001  "9"
   0xFF,  // 1111 1111  "Blank"
   0xFD   // 1111 1101  "Minus"
  };
  
  temp = temperature % 10;  // x--
  data = (unsigned char) temp;
  data = NUMBERS[data];
  // data &= 0xFE;  // insert decimal point x.--
  sevseg_array[0] = data;

  temperature = temperature / 10; 
  temp = temperature % 10;   // -x-
  data = (unsigned char) temp;
  data = NUMBERS[data];
  data &= 0xFE;  // insert decimal point -x.-
  sevseg_array[1] = data; 

  temperature = temperature / 10; 
  temp = temperature % 10;  // --x
  data = (unsigned char) temp;
  data = NUMBERS[data];
  // data &= 0xFE;  // insert decimal point --x.
  sevseg_array[2] = data;
}


void ShiftOut_array(unsigned char sevseg_array[], unsigned char length)
{
  digitalWrite(LOAD_PIN,LOW);
  for (int i=0; i< length; i++){
    shiftOut(SDI_PIN, SCLK_PIN, LSBFIRST , sevseg_array[i]); 
  }
  digitalWrite(LOAD_PIN,HIGH);
}



/* *********************************** */
/*            HARDWARE                 */
/* *********************************** */

/*                     Chip ATMega328                          Arduino Uno R3 stackable header
                                                                              _______
                                                              Digital:  _____/       \__  Analog:
               ______________  ______________                   txd ->-|D00 >RXD      A5|-
              |              \/              |                  rxd -<-|D01 <TXD      A4|-
        Res---|01 PC6/RES' ATM328  SCL/PC5 28|-(A5)-      SEG7_Load -<-|D02           A3|-
 txd ->-(D00)-|02 PD0/RXD          SDA/PC4 27|-(A4)-      SEG7_SCLK -<-|D03~          A2|-
 rxd -<-(D01)-|03 PD1/TXD              PC3 26|-(A3)-       SEG7_SDI -<-|D04           A1|-
   Load-(D02)-|04 PD2/INT0             PC2 25|-(A2)-                  -|D05~          A0|-<-NTC_OUT
   SCLK-(D03)-|05 PD3/INT1/PWM         PC1 24|-(A1)-                  -|D06~            | Power:      
    SDI-(D04)-|06 PD4                  PC0 23|-(A0)-<-NTC_OUT         -|D07          Vin|-
       +5V ---|07 VCC                  GND 22|--- Gnd                  |             GND|--- GND
       Gnd ---|08 GND                 AREF 21|--- Vin                 -|D08          GND|- 
    Xtal |X|--|09 PB6/OSC1            AVCC 20|--- +5V                 -|D09~         +5V|--- +5V
   16MHz |X|--|10 PB7/OSC2         SCK/PB5 19|-(D13)-                 -|D10~       +3.3V|-
       -(D05)-|11 PD5/PWM         MISO/PB4 18|-(D12)-                 -|D11~         Res|-
       -(D06)-|12 PD6/PWM     PWM/MOSI/PB3 17|-(D11)-                 -|D12        IOREF|-
       -(D07)-|13 PD7          PWM/SS'/PB2 16|-(D10)-                 -|D13 LED     --- |
       -(D08)-|14 PB0              PWM/PB1 15|-                       -|GND             | 
              |______________________________|                        -|AREF            |
                                                                      -|SCL             |
                                                                      -|SDA             |
                                                                       |________________|
*/


/*
  74HC595 Shiftregister for 3 digit 7-segment display       
         ______________________________________
        |  SEG7  Active low '0' LED on         |
 +5V ---|Vcc      /-a-\                     Vcc|---
 GND ---|Gnd      f   b                     Gnd|---
 D04 ->-|SDI      |-g-|   a.b.c.d.e.f.g.dp  SDO|---
 D03 ->-|SCLK     e   c   lsb first        SCLK|---
 D02 ->-|Load     \-d-/ dp                 Load|---
        |______________________________________|
*/

/*
            ________________
           | NTC Thermistor |
  NTC_OUT -| NTC 10k        |
      VCC -| R 10k          |
      GND -|                |
           |________________|

*/

74HC595
74HC595
74HC595