Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Code extracted from the Arduino Forum and mixed
- with Temperature wby colors on arduinoarts.com
- 4 Digit 7 Segment display from Sparkfun
- http://www.sparkfun.com/commerce/product_info.php?products_id=9480
- 1: Digit 1 16: B
- 2: Digit 2 15: G
- 3: D 14: A
- 4: Colon Anode 13: C
- 5: E 12: Colon Cathode
- 6: Digit 3 11: F
- 7: Decimal Point 10: Apostrophe Anode
- 8: Digit 4 9: Apostrophe Cathode
- *************
- Display's Cathode goes to ground via resistor
- Display's Anode goes to digital out
- Digit pins go to digital out via resistor
- Segment pins (A-G) go to digital out or shift register out (0 is on)
- Modified by @mrlndr for arduinoarts.com
- */
- int clockPin = 12; //Pin connected to SH_CP
- int dataPin = 11; //Pin connected to DS
- int digit1Pin = 5;
- int digit2Pin = 2;
- int digit3Pin = 3;
- int digit4Pin = 4;
- byte data;
- byte dataArray[13];
- int tempPin = 0; // the thermistor and 4,7k resistor
- int temp=0;
- int tempread;
- const int MINUS_IDX = 10;
- const int CELCIUS_IDX = 11;
- const int FARENHEIT_IDX = 12;
- void setup(){
- pinMode(digit1Pin, OUTPUT);
- pinMode(digit2Pin, OUTPUT);
- pinMode(digit3Pin, OUTPUT);
- pinMode(digit4Pin, OUTPUT);
- pinMode(latchPin, OUTPUT);
- Serial.begin(9600);
- dataArray[0] = B11000000;
- dataArray[1] = B11111001;
- dataArray[2] = B10100100;
- dataArray[3] = B10110000;
- dataArray[4] = B10011001;
- dataArray[5] = B10010010;
- dataArray[6] = B10000010;
- dataArray[7] = B11111000;
- dataArray[8] = B10000000;
- dataArray[9] = B10010000;
- //temperature specific characters
- dataArray[MINUS_IDX] = B10111111; // minus sign
- dataArray[CELCIUS_IDX] = B11000110; // C
- dataArray[FARENHEIT_IDX] = B10001110; // F
- }
- void loop(){
- tempread = analogRead(tempPin);
- temp=tempread/19;
- // Serial.print("Temp = ");
- //Serial.println(temp); // reading the values
- setTemp(temp, 'C');
- }
- void setTemp(int temp, char scale){
- //temp must be between -99 and 999 in either scale to fit the display
- //put in a check here later
- boolean negative = false;
- if (temp < 0)
- negative = true;
- temp = abs(temp);
- if (scale == 'F'){
- setDigit(digit4Pin, FARENHEIT_IDX);
- } else if (scale == 'C'){
- setDigit(digit4Pin, CELCIUS_IDX);
- }
- setDigit(digit3Pin, temp % 10);
- temp /= 10;
- if (temp >= 1){
- setDigit(digit2Pin, temp % 10);
- temp /= 10;
- if (temp >= 1){
- setDigit(digit1Pin, temp % 10);
- }
- }
- if (negative){
- setDigit(digit1Pin, MINUS_IDX);
- }
- }
- void setDigit(int digitPin, int value){
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, dataArray[value]);
- digitalWrite(latchPin, 1);
- digitalWrite(digitPin, HIGH);
- delay(1);
- digitalWrite(digitPin, LOW);
- }
- void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
- // This shifts 8 bits out MSB first,
- //on the rising edge of the clock,
- //clock idles low
- //internal function setup
- int i=0;
- int pinState;
- pinMode(myClockPin, OUTPUT);
- pinMode(myDataPin, OUTPUT);
- //clear everything out just in case to
- //prepare shift register for bit shifting
- digitalWrite(myDataPin, 0);
- digitalWrite(myClockPin, 0);
- for (i=7; i>=0; i--) {
- digitalWrite(myClockPin, 0);
- //if the value passed to myDataOut and a bitmask result
- // true then... so if we are at i=6 and our value is
- // %11010100 it would the code compares it to %01000000
- // and proceeds to set pinState to 1.
- if ( myDataOut & (1<<i) ) {
- pinState= 1;
- }
- else {
- pinState= 0;
- }
- //Sets the pin to HIGH or LOW depending on pinState
- digitalWrite(myDataPin, pinState);
- //register shifts bits on upstroke of clock pin
- digitalWrite(myClockPin, 1);
- //zero the data pin after shift to prevent bleed through
- digitalWrite(myDataPin, 0);
- }
- //stop shifting
- digitalWrite(myClockPin, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement