Advertisement
myapit

Arduino Temp Monitor

Jun 13th, 2013
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. /*
  2.  
  3. Code extracted from the Arduino Forum and mixed
  4. with Temperature wby colors on arduinoarts.com
  5.  
  6. 4 Digit 7 Segment display from Sparkfun
  7. http://www.sparkfun.com/commerce/product_info.php?products_id=9480
  8.  1: Digit 1    16: B
  9.  2: Digit 2    15: G
  10.  3: D      14: A
  11.  4: Colon Anode    13: C
  12.  5: E      12: Colon Cathode
  13.  6: Digit 3    11: F
  14.  7: Decimal Point  10: Apostrophe Anode
  15.  8: Digit 4    9:  Apostrophe Cathode
  16.  
  17.  *************
  18.  Display's Cathode goes to ground via resistor
  19.  Display's Anode goes to digital out
  20.  Digit pins go to digital out via resistor
  21.  Segment pins (A-G) go to digital out or shift register out (0 is on)
  22.  
  23. Modified by @mrlndr for arduinoarts.com
  24.  
  25. */
  26.  
  27. int clockPin = 12;  //Pin connected to SH_CP
  28. int dataPin = 11;  //Pin connected to DS
  29.  
  30. int digit1Pin = 5;
  31. int digit2Pin = 2;
  32. int digit3Pin = 3;
  33. int digit4Pin = 4;
  34.  
  35. byte data;
  36. byte dataArray[13];
  37.  
  38. int tempPin = 0;     // the thermistor and 4,7k resistor
  39.  
  40. int temp=0;
  41. int tempread;
  42.  
  43. const int MINUS_IDX = 10;
  44. const int CELCIUS_IDX = 11;
  45. const int FARENHEIT_IDX = 12;
  46.  
  47. void setup(){
  48.   pinMode(digit1Pin, OUTPUT);
  49.   pinMode(digit2Pin, OUTPUT);
  50.   pinMode(digit3Pin, OUTPUT);
  51.   pinMode(digit4Pin, OUTPUT);
  52.  
  53.   pinMode(latchPin, OUTPUT);
  54.  
  55.   Serial.begin(9600);
  56.  
  57.   dataArray[0] = B11000000;
  58.   dataArray[1] = B11111001;
  59.   dataArray[2] = B10100100;
  60.   dataArray[3] = B10110000;
  61.   dataArray[4] = B10011001;
  62.   dataArray[5] = B10010010;
  63.   dataArray[6] = B10000010;
  64.   dataArray[7] = B11111000;
  65.   dataArray[8] = B10000000;
  66.   dataArray[9] = B10010000;
  67.  
  68.   //temperature specific characters
  69.   dataArray[MINUS_IDX] = B10111111;  // minus sign
  70.   dataArray[CELCIUS_IDX] = B11000110;  // C
  71.   dataArray[FARENHEIT_IDX] = B10001110;  // F
  72.  
  73. }
  74.  
  75. void loop(){
  76.   tempread = analogRead(tempPin);
  77.   temp=tempread/19;
  78.   // Serial.print("Temp = ");
  79.   //Serial.println(temp); // reading the values
  80.   setTemp(temp, 'C');
  81. }
  82.  
  83. void setTemp(int temp, char scale){
  84.   //temp must be between -99 and 999 in either scale to fit the display
  85.   //put in a check here later
  86.   boolean negative = false;
  87.   if (temp < 0)
  88.     negative = true;
  89.   temp = abs(temp);
  90.  
  91.   if (scale == 'F'){
  92.     setDigit(digit4Pin, FARENHEIT_IDX);
  93.   } else if (scale == 'C'){
  94.     setDigit(digit4Pin, CELCIUS_IDX);
  95.   }
  96.  
  97.   setDigit(digit3Pin, temp % 10);
  98.   temp /= 10;
  99.   if (temp >= 1){
  100.     setDigit(digit2Pin, temp % 10);
  101.     temp /= 10;
  102.     if (temp >= 1){
  103.     setDigit(digit1Pin, temp % 10);
  104.     }
  105.   }
  106.   if (negative){
  107.     setDigit(digit1Pin, MINUS_IDX);
  108.   }
  109. }
  110.  
  111. void setDigit(int digitPin, int value){
  112.  
  113.     digitalWrite(latchPin, 0);
  114.     shiftOut(dataPin, clockPin, dataArray[value]);
  115.     digitalWrite(latchPin, 1);
  116.  
  117.     digitalWrite(digitPin, HIGH);
  118.     delay(1);
  119.     digitalWrite(digitPin, LOW);
  120. }
  121.  
  122. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  123.   // This shifts 8 bits out MSB first,
  124.   //on the rising edge of the clock,
  125.   //clock idles low
  126.  
  127.   //internal function setup
  128.   int i=0;
  129.   int pinState;
  130.   pinMode(myClockPin, OUTPUT);
  131.   pinMode(myDataPin, OUTPUT);
  132.  
  133.   //clear everything out just in case to
  134.   //prepare shift register for bit shifting
  135.   digitalWrite(myDataPin, 0);
  136.   digitalWrite(myClockPin, 0);
  137.  
  138.   for (i=7; i>=0; i--)  {
  139.     digitalWrite(myClockPin, 0);
  140.  
  141.     //if the value passed to myDataOut and a bitmask result
  142.     // true then... so if we are at i=6 and our value is
  143.     // %11010100 it would the code compares it to %01000000
  144.     // and proceeds to set pinState to 1.
  145.     if ( myDataOut & (1<<i) ) {
  146.     pinState= 1;
  147.     }
  148.     else {
  149.     pinState= 0;
  150.     }
  151.  
  152.     //Sets the pin to HIGH or LOW depending on pinState
  153.     digitalWrite(myDataPin, pinState);
  154.     //register shifts bits on upstroke of clock pin
  155.     digitalWrite(myClockPin, 1);
  156.     //zero the data pin after shift to prevent bleed through
  157.     digitalWrite(myDataPin, 0);
  158.   }
  159.  
  160.   //stop shifting
  161.   digitalWrite(myClockPin, 0);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement