2

I am using sevseg and i can't get more than 8 digits to work. I really need 15. Does somebody know what I can do to get this?

Shown is the original example

This is using Gabriel Staples example in the sevseg library:

#include <SevSeg.h>

SevSeg sevseg; //Instantiate a seven segment controller object

void setup()
{
  byte numDigits = 4; //<<---I am wanting 15, but doesn't work after 8------   
  byte digitPins[] = {2, 3, 4, 5}; //Digits: 1,2,3,4 <--put one resistor (ex: 220 Ohms, or 330 Ohms, etc, on each digit pin)
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; //Segments: A,B,C,D,E,F,G,Period

  sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
// Note: 100 brightness simply corresponds to a delay of 2000us after lighting each segment. A brightness of 0 
// is a delay of 1us; it doesn't really affect brightness as much as it affects update rate (frequency).
// Therefore, for a 4-digit 7-segment + pd, COMMON_ANODE display, the max update rate for a "brightness" of 100 is 1/(2000us*8) = 62.5Hz.
// I am choosing a "brightness" of 10 because it increases the max update rate to approx. 1/(200us*8) = 625Hz.
// This is preferable, as it decreases aliasing when recording the display with a video camera....I think.
  sevseg.setBrightness(10); 
}

void loop()
{
  //local vars
  static byte decPlace = 0;

  sevseg.setNumber(8888,decPlace);
  decPlace++;
  decPlace %= 4; //rollover back to 0 once variable gets to 4; To anyone wondering: the % is called the "modulo" operator; see here for explanation & example: https://www.arduino.cc/en/Reference/Modulo

  sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right
}

Using the following code, I can't get the 3rd set of 5 numbers to display. I have changed the sevseg.h file to 15, but that shouldn't matter:

#include <SevSeg.h>

SevSeg sevseg1;//Instantiate a seven segment controller object 1
SevSeg sevseg2;//Instantiate a seven segment controller object 2
SevSeg sevseg3;//Instantiate a seven segment controller object 3


void setup()
{

  Serial.begin(9600);

byte segmentPins[] = {22, 23, 24, 25, 26, 27, 28, 34}; //Segments:     A,B,C,D,E,F,G,Period

  byte digitPins1[] = {29, 30, 31, 32, 33};
  sevseg1.begin(COMMON_CATHODE, sizeof(digitPins1), digitPins1,  segmentPins);
  sevseg1.setBrightness(10);

  byte digitPins2[] = {35, 36, 37, 38, 39};
  sevseg2.begin(COMMON_CATHODE, sizeof(digitPins2), digitPins2, segmentPins);
  sevseg2.setBrightness(10);

  byte digitPins3[] = {40, 41, 42, 43, 44};
  sevseg3.begin(COMMON_CATHODE, sizeof(digitPins2), digitPins3, segmentPins);
  sevseg3.setBrightness(10);


}

void loop()
{


static byte decPlace = 2;

  sevseg1.setNumber( 12345, decPlace );
  sevseg2.setNumber( 12345, decPlace );
  sevseg3.setNumber( 12345, decPlace );


  sevseg1.refreshDisplay();
  sevseg2.refreshDisplay();
  sevseg2.refreshDisplay();
}
  • As per Dean Reading (programmer of sevseg.h) Using the example above, there will be a byte overflow and it is not possible for more than 8 digits. So to further my question, is it possible (and if so how) do I break it down to say 3 segments of 5 digits each? This would work for me as well, but it is over my head. Thanks for any help I can get – klarson18 Dec 6 '15 at 20:22
  • Answer edited to show multiple instances, one to handle 8 digits, the other to handle 7 digits. – slash-dev Dec 6 '15 at 23:13
  • What Arduino are you using? 15 digits + 8 segments requires 23 IO pins, so I hope you are using a Mega2560. – slash-dev Dec 6 '15 at 23:29
5

You can change the pre-defined maximum number of digits at the top of the SevSeg.h header file to as many as 10, if you use long or unsigned long when you call setNumber. It is defined here, on line 27:

#define MAXNUMDIGITS 8 //Increase this number to support larger displays

This the version referenced from the Arduino Playground SevSeg. Be sure to use the same number in your modulo, too:

void loop()
{
  //local vars
  static byte decPlace = 0;

  sevseg.setNumber(8888,decPlace);
  decPlace++;
  decPlace %= 10; //rollover back to 0 once variable gets to 10

decPlaces is mostly useful for floating-point numbers: it specifies how many digits after the decimal point should be displayed. For integer values, it specifies how many leading zeroes will be displayed.

However (as you discovered), you cannot call setNumber with anything larger than a long, so you are limited to 10 or fewer digits for MAXNUMDIGITS. (There is no byte overflow -- it's just limited to the number of decimal digits in 232-1.) This is a limit of one SevSeg instance. You could have two instances of SevSeg like this:

#include <SevSeg.h>
SevSeg sevseg1;
SevSeg sevseg2;

void setup() 
{
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; //Segments: A,B,C,D,E,F,G,Period

  byte digitPins1[] = {2, 3, 4, 5, 22, 23, 24};
  sevseg1.begin(COMMON_ANODE, sizeof(digitPins1), digitPins1, segmentPins);
  sevseg1.setBrightness(10);

  byte digitPins2[] = {14,15,16,17,18,19,20,21};
  sevseg2.begin(COMMON_ANODE, sizeof(digitPins2), digitPins2, segmentPins);
  sevseg2.setBrightness(10);

  sevseg1.setNumber(  7654321UL, 7 );
  sevseg2.setNumber( 87654321UL, 8 );
}

void loop() 
{ 
  sevseg1.refreshDisplay();
  sevseg2.refreshDisplay();
}

The first instance handles 7 digits, and the second instances handles 8 digits. Note the use of "UL" to force and unsigned long call.

Update 11-DEC-2015

Using the following code, I can't get the 3rd set of 5 numbers to display. I have changed the sevseg.h file to 15, but that shouldn't matter:

First, the MAXNUMDIGITS is the maximum per instance, so you could set it to 5 to save some precious RAM.

Second, you have a copy and paste error:

sevseg1.refreshDisplay();
sevseg2.refreshDisplay();
sevseg2.refreshDisplay();   <--  Oopsies!  :)

Third, I assume the setNumber calls are placeholders for what you're really going to use. Since they always set the same value, they could be in setup. In your final program, there must be some input that will be used to calculate a number to display with setNumber.

  • Good catch on the modulo. I'd suggest using numDigits instead of a fixed value though. – dlu Dec 5 '15 at 21:13
  • Sorry, new to this forum. See comment above. – klarson18 Dec 6 '15 at 20:28
  • using 3 instance of 5 digits each... The last 5 digits are blank. Is it not possible to use 3 instances? I am using a Mega 2560 – klarson18 Dec 11 '15 at 1:38
  • Add new code to your question? – slash-dev Dec 11 '15 at 3:37
  • Added. Thanks @slash-dev. I am starting to get a feel for stackexchange. Thanks for your patience – klarson18 Dec 11 '15 at 13:03
1

Looking at the sevseg README file on GitHub I find this:

If you wish to use more than 8 digits, increase MAXNUMDIGITS in SevSeg.h.

Have you done that? Seems like that might be the problem.

As @slash-dev said, be sure to also modify the loop() code as well:

void loop()
{
  //local vars
  static byte decPlace = 0;

  sevseg.setNumber(8888,decPlace);
  decPlace++;
  decPlace %= numDigits;

I think it makes more sense to use numDigits rather than a hard coded constant that you have to remember to change whenever you change the number of digits in your display. It would be worth checking the code for any other hard coded constants. If space is an issue, it might be worth checking to see if using a #define results in smaller code than using a variable – in any case do something to avoid needing to understand what "magic numbers" are doing. This is especially important when the same numeric value (e.g., 4) is being used for multiple reasons. Then it gets hard if you need to change the value as you have to understand the purpose behind each use.

  • See the comment I made above also. I am actually pulling numbers in from another program and can do that without an issue. It is a set number of numbers. The max I can get with the above code is 8 because of variables overflow. Is there a way to do 3 segments of 5 digits each? That will work as well. Thanks in advance – klarson18 Dec 6 '15 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.