Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 5 digit 7 segment LED display control
- By: TJ Hunter (tjhunter@gmail.com)
- // See the project for this source code at:
- http://www.youtube.com/watch?v=-3w7FMUC28M
- This example code is in the public domain.
- ******************************************
- This is how I labeled each segment: (this is probably not the standard way 7 segments are labeled)
- _____
- | F |
- A| |E
- |_____|
- | G |
- B| |D
- |_____|
- C
- */
- #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
- #define SEGMENT_COUNT 7 // How many segments I'm controlling
- #define DIGIT_COUNT 5 // How many digits I'm controlling
- // The pins for each segment
- // A, B, C, D, E, F, G
- int segmentPins[SEGMENT_COUNT] = { 2, 3, 4, 5, 6, 7, 8 };
- int decimal_pin = A1;
- // The pins for each digit
- int digitPins[] = { 9, 10, 11, 12, A0 };
- // This array defines the pattern for each digit. It tells which LEDs to turn off and on for a certain number
- // LOW = OFF, HIGH = ON
- int digitPatterns[PATTERN_COUNT][SEGMENT_COUNT] = {
- // A B C D E F G
- { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW }, // 0
- { LOW, LOW, LOW, HIGH, HIGH, LOW, LOW }, // 1
- { LOW, HIGH, HIGH, LOW, HIGH, HIGH, HIGH }, // 2
- { LOW, LOW, HIGH, HIGH, HIGH, HIGH, HIGH }, // 3
- { HIGH, LOW, LOW, HIGH, HIGH, LOW, HIGH }, // 4
- { HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH }, // 5
- { HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH }, // 6
- { LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW }, // 7
- { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH }, // 8
- { HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH }, // 9
- { LOW, LOW, LOW, LOW, LOW, LOW, LOW }, // All OFF (blank)
- };
- void setup() {
- // initialize the 7 segment pins as output
- for (int thisPin = 0; thisPin < SEGMENT_COUNT; thisPin++) {
- pinMode(segmentPins[thisPin], OUTPUT);
- }
- // pinMode(decimal_pin, OUTPUT); // Not using it right now
- // initialize the 5 digit ground pins as output
- for (int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++) {
- pinMode(digitPins[thisPin], OUTPUT);
- }
- }
- // Turns all the digits off
- void allDigitsOff() {
- for (int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++) {
- digitalWrite(digitPins[thisPin], HIGH);
- }
- }
- // This turns on a specific digit and will display the number that was set using setPattern()
- // This function expects that all digits are off, which would be the case if you ran setPattern() first.
- void digitOn(int digitNum) {
- digitalWrite(digitPins[digitNum-1], LOW);
- // 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
- // This delay helps with evening out the brightness between all the digits
- delay(2);
- }
- // This is the function that sets the number to show on the next digit that gets turned on by digitOn()
- void setPattern(int pattern) {
- 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
- 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
- digitalWrite(segmentPins[thisPin], digitPatterns[pattern][thisPin]);
- }
- }
- // This function takes a number and splits it up into separate digits then shows each digit in the right place.
- void showNumber(int currentNumber) {
- // Loop through all the digits of this number and extract the last digit each time
- for (int currentDigit = DIGIT_COUNT; currentDigit > 0; currentDigit--) {
- // To get the number in the ones place, do a mod ten.
- int number = currentNumber % 10;
- // 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
- currentNumber /= 10;
- // Now set the pattern to the digit we just got
- setPattern(number);
- // Now turn on the digit in the right place
- digitOn(currentDigit);
- }
- }
- void loop() {
- // This loop figures out what number to show, then prints it once to the display.
- // This loop needs to run very quickly, otherwise the display will flicker
- // The number I want to show on the display is a count since the program started
- // You could get this number from a reading of an analog device or anything else really.
- int currentNumber = (millis() / 100);
- showNumber(currentNumber);
- // Keep going!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement