Advertisement
Guest User

Temperature on 7 segment common cathode arduino onu

a guest
Aug 30th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. /* Modified by Jeffrey@dutchunderground.nl
  2.  * For common cathode display and LM35 temp sensor. Smooth out display refreshing
  3.  *
  4.  * Original code by Rui Santos, http://randomnerdtutorials.com
  5.  * Temperature Sensor Displayed on 4 Digit 7 segment common anode
  6.  * 2013
  7.  */
  8.  
  9.  
  10. const int digitPins[4] = {10,8,9,7};                 //4 common anode pins of the display
  11. const int clockPin = 11;    //74HC595 Pin 11
  12. const int latchPin = 12;    //74HC595 Pin 12
  13. const int dataPin  = 13;     //74HC595 Pin 14
  14. const int tempPin  = A0;     //temperature sensor pin
  15. const byte digit[10] =      //seven segment digits in bits
  16. {
  17.   B11000000, //0
  18.   B11111001, //1
  19.   B10100100, //2
  20.   B10110000, //3
  21.   B10011001, //4
  22.   B10010010, //5
  23.   B10000010, //6
  24.   B11111000, //7
  25.   B10000000, //8
  26.   B10010000  //9
  27. };
  28. const byte digitdec[10] =      //seven segment digits in bits with decimal point
  29. {
  30.   B01000000, //0
  31.   B01111001, //1
  32.   B00100100, //2
  33.   B00110000, //3
  34.   B00011001, //4
  35.   B00010010, //5
  36.   B00000010, //6
  37.   B01111000, //7
  38.   B00000000, //8
  39.   B00010000  //9
  40. };
  41. int digitBuffer[4] = {
  42.   0};
  43. int digitScan = 0, flag=0,  soft_scaler = 0;
  44. ;
  45. float tempK, tempC, tempF, temp;
  46.  
  47. const int numReadings = 10;
  48.  int readings[numReadings];      // the readings from the analog input
  49. int index = 0;                  // the index of the current reading
  50.  
  51.  
  52. void setup(){                
  53.   for(int i=0;i<4;i++)
  54.   {
  55.     pinMode(digitPins[i],OUTPUT);
  56.   }
  57.   pinMode(tempPin, INPUT);
  58.   pinMode(latchPin, OUTPUT);
  59.   pinMode(clockPin, OUTPUT);
  60.   pinMode(dataPin, OUTPUT);  
  61.   pinMode(tempPin, INPUT);
  62.   analogReference(INTERNAL); // Set analog input pin to 1.1v for arduino Mega use INTERNAL1V1
  63. }
  64.  
  65. //writes the temperature on display
  66. void updateDisp(){
  67.   for(byte j=0; j<4; j++)  
  68.     digitalWrite(digitPins[j], HIGH);
  69.  
  70.   digitalWrite(latchPin, LOW);  
  71.   shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
  72.   digitalWrite(latchPin, HIGH);
  73.  
  74.   delayMicroseconds(10);
  75.   digitalWrite(digitPins[digitScan], LOW);
  76.  
  77.   digitalWrite(latchPin, LOW);  
  78.    if(digitScan==0)
  79.     shiftOut(dataPin, clockPin, MSBFIRST, ~B10011110);    // display small c on section 0
  80.    else
  81.    if(digitScan==2)
  82.     shiftOut(dataPin, clockPin, MSBFIRST, ~digitdec[digitBuffer[digitScan]]); // display decimal point at section 2
  83.   else
  84.     shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
  85.  
  86.   digitalWrite(latchPin, HIGH);
  87.   digitScan++;
  88.   if(digitScan>3) digitScan=0;
  89. }
  90.  
  91. void loop(){
  92.  
  93. index = index + 1;                     // add to counter
  94. if (index >= 500){                     // read sensor after 500 display updates to smooth out the readings
  95. tempC = analogRead(tempPin);           // read the value from the sensor
  96. index = 0;                             // reset counter
  97. tempC = tempC *10 / 9.31;              // convert sensor readings to celcius degrees
  98. }
  99.   digitBuffer[3] = (int(tempC)%1000)/100;  
  100.   digitBuffer[2] = (int(tempC)%100)/10;
  101.   digitBuffer[1] = (int(tempC)%100)%10;
  102.   updateDisp();
  103.   delay(2);
  104.            
  105.     // ...wrap around to the beginning:
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement