I have connected 2 3-digit 7 segment displays with the hopes of trying to output 6 digits off of a counter.
each 7 segment has 12 pins and these are common cathodes.
connected the 7 led pins (a-g) of both displays together and then connected it to the arduino. and 6 wires for each digit connected to the arduino.
I used the SevenSeg.h library and when I set it for 5 digits or over it only displays "1073" :o
I found a different code from TJ Hunter, I got it to work for 5 digits. but when it passes the number "32767"
it acts all weird. TJ Hunter's code
I can't seem to find the problem.
any help will be greatly appreciated!
/*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 6 // How many digits I'm controlling
// The pins for each segment
// A, B, C, D, E, F, G
int segmentPins[SEGMENT_COUNT] = { 7, 6, 5, 4, 3, 2, 12 };
// The pins for each digit
int digitPins[] = {9,10,11,22,24,26};
// 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.
//for (int aa=1; aa<99999; aa++)
{showNumber(aa); }
}