/*
 Temperatur
 
 shows temperature (input is TMP36 seinsor) on 7-Segment x4 display
 
 The circuit:
 * 1x TMP36 attached to pin 0 (analog in)
 * 2x 74HC_HCT4094 in line
 * 1x TDCR1050m (7-Segment x4)
 * 10x 760 Ohm resistors for TDCR1050m

 created 2012
 by hennisoft <http://www.hennisoft.de>
 modified 11 Nov 2012 by
 Henning Buchholz
 
 This example code is in the public domain.
 
*/

int latchpin = 8; // --> Pin 1 (STR)
int clockpin = 9; // --> Pin 3 (CP)
int datapin = 10; // --> Pin 2 (D)

int temperaturPin = 0;

int del = 100; // used to control speed of counting
int sensorvalue = 0; // value from analogue sensor. 

int lhd = 0;
int mhd = 0;
int mhd2 = 0;
int rhd = 0;
float a = 0;
int b = 0;
int c= 0;
float d = 0;
int leadingzero = 0; // 0 for no leading zereos, 1 for leading zeroes

int segdisp[12] = {192,249,164,176,153,146,130,248,128,144,167,198};
int posdisp[8] = {49,50,52,56,32,16,0,48}; // base 10 equivalents to close cathodes on display 0~3 on module

void setup()
{
  //Serial.begin(9600);
  
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
}

void digitdisplay(int digit, int location, boolean punkt)
// displays "digit" on display "location" 0~3
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, posdisp[location]); // sets the digit to address
  if (punkt){
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit] & 127); // sets the digit with dot
  } else {
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit]); // sets the digit
  }
  digitalWrite(latchpin, HIGH);
}

void onedigitnumber(int subject)
// displays a one-digit number on the display module with leading zeroes
{
  if (leadingzero==1)
  {
    digitdisplay(0,0,false);
    digitdisplay(0,1,false);
    digitdisplay(0,2,true);
  }
  digitdisplay(subject,3,false);
}

void twodigitnumber(int subject)
// displays a two-digit number on the display module with leading zeroes
{
  rhd = subject % 10;
  a = subject/10;
  lhd = int(a);
  if (leadingzero==1)
  {
    digitdisplay(0,0,false);
    digitdisplay(0,1,false);
  }
  digitdisplay(lhd,1,true);
  digitdisplay(rhd,2,false);
}

void threedigitnumber(int subject)
// displays a three-digit number on the display module with leading zeroes
{
  a = subject/100;
  lhd = int(a);
  a = subject/10;
  b = int(a); 
  mhd = b % 10;
  b=subject%100;
  rhd=b%10;
  if (leadingzero==1)
  {
    digitdisplay(0,0,false);
  }
  digitdisplay(lhd,0,false); 
  digitdisplay(mhd,1,true);
  digitdisplay(rhd,2,false);
}

void fourdigitnumber(int subject)
// displays a four-digit number on the display module with leading zeros
{
  a = subject/1000;
  lhd = int(a);
  b=lhd*1000;
  c=subject-b;
  a = c/100;
  mhd = int(a);
  a = c/10;
  b = int(a); 
  mhd2 = b % 10;
  b=subject%1000;
  c=b%100;
  rhd=c%10;
  digitdisplay(lhd,0,false);
  digitdisplay(mhd,1,false);
  digitdisplay(mhd2,2,true);
  digitdisplay(rhd,3,false);
}

void displaynumber(int rawnumber, int cycles)
// takes an integer and displays it on our 4-digit LED display module and HOLDS it on the display for 'cycles' number of cycles
{
  for (int q=1; q<=cycles; q++)
  {

    if (rawnumber>=0 && rawnumber<10)
    {
      onedigitnumber(rawnumber);
    } 
    else if (rawnumber>=10 && rawnumber<100)
    {
      twodigitnumber(rawnumber);
      
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, posdisp[5]); // sets ':'
      shiftOut(datapin, clockpin, MSBFIRST, 0); 
      digitalWrite(latchpin, HIGH);
      
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, posdisp[3]); // sets the digit to address
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[10]); // sets 'c'
      digitalWrite(latchpin, HIGH);
    } 
    else if (rawnumber>=100 && rawnumber<1000)
    {
      threedigitnumber(rawnumber);
      
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, posdisp[5]); // sets ':'
      shiftOut(datapin, clockpin, MSBFIRST, 0); 
      digitalWrite(latchpin, HIGH);
      
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, posdisp[3]); // sets the digit to address
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[10]); // sets 'c'
      digitalWrite(latchpin, HIGH);
    } 
    else if (rawnumber>=1000)
    {
      fourdigitnumber(rawnumber);
    }
  }
}

/*
 * getVoltage() - returns the voltage on the analog input defined by
 * pin
 */
float getVoltage(int pin){
 return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

void loop()
{
  float temperatur = getVoltage(temperaturPin);  //getting the voltage reading from the temperature sensor
  temperatur = (temperatur - .5) * 1000;
  sensorvalue = int(temperatur);
  
  displaynumber(sensorvalue,100);
  //Serial.println(sensorvalue);
}