Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Modified by Jeffrey@dutchunderground.nl
- * For common cathode display and LM35 temp sensor. Smooth out display refreshing
- *
- * Original code by Rui Santos, http://randomnerdtutorials.com
- * Temperature Sensor Displayed on 4 Digit 7 segment common anode
- * 2013
- */
- const int digitPins[4] = {10,8,9,7}; //4 common anode pins of the display
- const int clockPin = 11; //74HC595 Pin 11
- const int latchPin = 12; //74HC595 Pin 12
- const int dataPin = 13; //74HC595 Pin 14
- const int tempPin = A0; //temperature sensor pin
- const byte digit[10] = //seven segment digits in bits
- {
- B11000000, //0
- B11111001, //1
- B10100100, //2
- B10110000, //3
- B10011001, //4
- B10010010, //5
- B10000010, //6
- B11111000, //7
- B10000000, //8
- B10010000 //9
- };
- const byte digitdec[10] = //seven segment digits in bits with decimal point
- {
- B01000000, //0
- B01111001, //1
- B00100100, //2
- B00110000, //3
- B00011001, //4
- B00010010, //5
- B00000010, //6
- B01111000, //7
- B00000000, //8
- B00010000 //9
- };
- int digitBuffer[4] = {
- 0};
- int digitScan = 0, flag=0, soft_scaler = 0;
- ;
- float tempK, tempC, tempF, temp;
- const int numReadings = 10;
- int readings[numReadings]; // the readings from the analog input
- int index = 0; // the index of the current reading
- void setup(){
- for(int i=0;i<4;i++)
- {
- pinMode(digitPins[i],OUTPUT);
- }
- pinMode(tempPin, INPUT);
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(tempPin, INPUT);
- analogReference(INTERNAL); // Set analog input pin to 1.1v for arduino Mega use INTERNAL1V1
- }
- //writes the temperature on display
- void updateDisp(){
- for(byte j=0; j<4; j++)
- digitalWrite(digitPins[j], HIGH);
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
- digitalWrite(latchPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(digitPins[digitScan], LOW);
- digitalWrite(latchPin, LOW);
- if(digitScan==0)
- shiftOut(dataPin, clockPin, MSBFIRST, ~B10011110); // display small c on section 0
- else
- if(digitScan==2)
- shiftOut(dataPin, clockPin, MSBFIRST, ~digitdec[digitBuffer[digitScan]]); // display decimal point at section 2
- else
- shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
- digitalWrite(latchPin, HIGH);
- digitScan++;
- if(digitScan>3) digitScan=0;
- }
- void loop(){
- index = index + 1; // add to counter
- if (index >= 500){ // read sensor after 500 display updates to smooth out the readings
- tempC = analogRead(tempPin); // read the value from the sensor
- index = 0; // reset counter
- tempC = tempC *10 / 9.31; // convert sensor readings to celcius degrees
- }
- digitBuffer[3] = (int(tempC)%1000)/100;
- digitBuffer[2] = (int(tempC)%100)/10;
- digitBuffer[1] = (int(tempC)%100)%10;
- updateDisp();
- delay(2);
- // ...wrap around to the beginning:
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement