Arduino program to display temperature in seven segment displays (Part 2 of 2)

<<< Read previous part (part 1)

After completing the circuit given in my previous part, upload the following program to your arduino board. If uploading is successful, temperature will be displayed in common cathode seven segment displays.

//Pin connected to ST_CP of 74595
int latchPin = 12;
//Pin connected to SH_CP of 74595
int clockPin = 11;
////Pin connected to DS of 74595
int dataPin = 13;

int temp_value;
int temp_read_pin = 1;

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

int number_to_display_in_SSD(int convert_value) 
{  
  if(convert_value == 0)
  {
    return (63);
  }
  
  if(convert_value == 1)
  {
    return (6);
  }
  
  if(convert_value == 2)
  {
    return (91);
  }
  
  if(convert_value == 3)
  {
    return (79);
  }
  
  if(convert_value == 4)
  {
    return (102);
  }
  
  if(convert_value == 5)
  {
    return (109);
  }
  if(convert_value == 6)
  {
    return (125);
  }
  
  if(convert_value == 7)
  {
    return (7);
  }
  
  if(convert_value == 8)
  {
    return (127);
  }

  if(convert_value == 9)
  {
    return (111);
  }
}

void shiftout_function(int first_digit, int second_digit, int third_digit, int forth_digit, int fifth_digit)
{
  // take the latchPin low so 
  // the LEDs don't change while you're sending in bits:
  digitalWrite(latchPin, LOW);
  // shift out the bits:
  shiftOut(dataPin, clockPin, MSBFIRST, number_to_display_in_SSD(first_digit));  
  // shift out the bits:
  shiftOut(dataPin, clockPin, MSBFIRST, 254);
  //take the latch pin high so the LEDs will light up:
  digitalWrite(latchPin, HIGH);
        
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, number_to_display_in_SSD(second_digit));  
  shiftOut(dataPin, clockPin, MSBFIRST, 253);
  digitalWrite(latchPin, HIGH);   
   
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, number_to_display_in_SSD(third_digit) + 128);  
  shiftOut(dataPin, clockPin, MSBFIRST, 251);
  digitalWrite(latchPin, HIGH);      
   
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, number_to_display_in_SSD(forth_digit));  
  shiftOut(dataPin, clockPin, MSBFIRST, 247);
  digitalWrite(latchPin, HIGH);   
   
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, number_to_display_in_SSD(fifth_digit));  
  shiftOut(dataPin, clockPin, MSBFIRST, 239);
  digitalWrite(latchPin, HIGH); 
}

void loop() {
  
  temp_value = analogRead(temp_read_pin);
  float mv = ( temp_value/1024.0)*5000; 
  float celsius = mv/10;

  float value = celsius;
  
  int temp_mult_100 = value * 100;
 
  int first_digit = temp_mult_100 / 10000;
  int rem_after_first = temp_mult_100 % 10000;
 
  int second_digit = rem_after_first / 1000;
  int rem_after_second = rem_after_first % 1000; 
 
  int third_digit = rem_after_second / 100;
  int rem_after_third = rem_after_second % 100; 
 
  int forth_digit = rem_after_third / 10;
  int rem_after_forth = rem_after_third % 10; 
 
  int fifth_digit = rem_after_forth / 1;
  int rem_after_fifth = rem_after_forth % 1; 
  
  shiftout_function(first_digit, second_digit, third_digit, forth_digit, fifth_digit);         
}

Video demonstration of working of circuit is given below.

Next : Motion detection circuit using PIR motion sensor and arduino >>>

(Visited 2,322 times, 1 visits today)
paulpp644
A deep electronic enthusiast who spent most of my time for electronic experiments. I am also interested in publishing my experiments in my blog for helping other electronics enthusiasts.