Advertisement
Guest User

digit 7 segment LED display control for arduino

a guest
Jan 2nd, 2011
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 KB | None | 0 0
  1. /*
  2.   5 digit 7 segment LED display control
  3.   By: TJ Hunter (tjhunter@gmail.com)
  4.  
  5.   // See the project for this source code at:
  6.   http://www.youtube.com/watch?v=-3w7FMUC28M
  7.  
  8.   This example code is in the public domain.
  9.  
  10.   ******************************************
  11.  
  12.  
  13.   This is how I labeled each segment: (this is probably not the standard way 7 segments are labeled)
  14.   _____
  15.  |  F  |
  16. A|     |E
  17.  |_____|
  18.  |  G  |
  19. B|     |D
  20.  |_____|
  21.     C
  22.  
  23. */
  24.  
  25. #define PATTERN_COUNT 11 // How many different segment patterns I have. For now I have representations of the number 0 through 9 and all off
  26. #define SEGMENT_COUNT 7 // How many segments I'm controlling
  27. #define DIGIT_COUNT 5 // How many digits I'm controlling
  28.  
  29. // The pins for each segment
  30. //                                 A, B, C, D, E, F, G
  31. int segmentPins[SEGMENT_COUNT] = { 2, 3, 4, 5, 6, 7, 8 };
  32. int decimal_pin = A1;
  33.  
  34. // The pins for each digit
  35. int digitPins[] = { 9, 10, 11, 12, A0 };
  36.  
  37. // This array defines the pattern for each digit. It tells which LEDs to turn off and on for a certain number
  38. // LOW = OFF, HIGH = ON
  39. int digitPatterns[PATTERN_COUNT][SEGMENT_COUNT] = {
  40. //    A     B     C     D     E     F     G
  41.     { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW  }, // 0
  42.     { LOW,  LOW,  LOW,  HIGH, HIGH, LOW,  LOW  }, // 1
  43.     { LOW,  HIGH, HIGH, LOW,  HIGH, HIGH, HIGH }, // 2
  44.     { LOW,  LOW,  HIGH, HIGH, HIGH, HIGH, HIGH }, // 3
  45.     { HIGH, LOW,  LOW,  HIGH, HIGH, LOW,  HIGH }, // 4
  46.     { HIGH, LOW,  HIGH, HIGH, LOW,  HIGH, HIGH }, // 5
  47.     { HIGH, HIGH, HIGH, HIGH, LOW,  HIGH, HIGH }, // 6
  48.     { LOW,  LOW,  LOW,  HIGH, HIGH, HIGH, LOW  }, // 7
  49.     { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH }, // 8
  50.     { HIGH, LOW,  LOW,  HIGH, HIGH, HIGH, HIGH }, // 9
  51.     { LOW,  LOW,  LOW,  LOW,  LOW,  LOW,  LOW  }, // All OFF (blank)
  52. };
  53.  
  54.  
  55.  
  56. void setup() {
  57.   // initialize the 7 segment pins as output
  58.   for (int thisPin = 0; thisPin < SEGMENT_COUNT; thisPin++) {
  59.     pinMode(segmentPins[thisPin], OUTPUT);
  60.   }
  61. //  pinMode(decimal_pin, OUTPUT); // Not using it right now
  62.  
  63.   // initialize the 5 digit ground pins as output
  64.   for (int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++) {
  65.     pinMode(digitPins[thisPin], OUTPUT);
  66.   }
  67. }
  68.  
  69. // Turns all the digits off
  70. void allDigitsOff() {
  71.   for (int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++) {
  72.     digitalWrite(digitPins[thisPin], HIGH);
  73.   }
  74. }
  75.  
  76. // This turns on a specific digit and will display the number that was set using setPattern()
  77. // This function expects that all digits are off, which would be the case if you ran setPattern() first.
  78. void digitOn(int digitNum) {
  79.   digitalWrite(digitPins[digitNum-1], LOW);
  80.  
  81.   // Yes, we want to cycle through the digits quickly so that we can't tell they're not on all at once, but if we cycle too quickly, the digits aren't evenly lit
  82.   // This delay helps with evening out the brightness between all the digits
  83.   delay(2);
  84. }
  85.  
  86. // This is the function that sets the number to show on the next digit that gets turned on by digitOn()
  87. void setPattern(int pattern) {
  88.   allDigitsOff(); // Make sure all the digits are turned off while we do this, or the last digit will look funny while we modify the pattern
  89.  
  90.   for (int thisPin = 0; thisPin < SEGMENT_COUNT; thisPin++) { // Loop through all the segment pins and set them to the pattern that corresponds to the digit in the digitPattern array
  91.     digitalWrite(segmentPins[thisPin], digitPatterns[pattern][thisPin]);
  92.   }
  93. }
  94.  
  95. // This function takes a number and splits it up into separate digits then shows each digit in the right place.
  96. void showNumber(int currentNumber) {
  97.   // Loop through all the digits of this number and extract the last digit each time
  98.   for (int currentDigit = DIGIT_COUNT; currentDigit > 0; currentDigit--) {
  99.     // To get the number in the ones place, do a mod ten.
  100.     int number = currentNumber % 10;
  101.     // Now chop off the last digit so the digit in the tens place now becomes the digit on the ones places for the next iteration of the loop
  102.     currentNumber /= 10;
  103.     // Now set the pattern to the digit we just got
  104.     setPattern(number);
  105.     // Now turn on the digit in the right place
  106.     digitOn(currentDigit);
  107.   }
  108. }
  109.  
  110. void loop() {
  111.   // This loop figures out what number to show, then prints it once to the display.
  112.   // This loop needs to run very quickly, otherwise the display will flicker
  113.  
  114.   // The number I want to show on the display is a count since the program started
  115.   // You could get this number from a reading of an analog device or anything else really.
  116.  
  117.   int currentNumber = (millis() / 100);
  118.   showNumber(currentNumber);
  119.  
  120.   // Keep going!
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement