Troubleshooting SevSeg Library with 8 Displays

I am looking for a good way to display numbers on a total of 8 seven-segment displays.
For my project, the displays are grouped as follows: Digits 1, 2, and 3 are one group (score) , digits 4, 5, and 6 (high score) are a second group, and the third group is digits 7 and 8 (time). I have taken some of the test code shown in the SevSeg Library and that makes my displays work. However, in the process of tweaking that code for my use, I am having problems. With the code shown below, all eight displays show a solid number "8". If I put a short delay in the code, I can see each digit simultaneously turn on and off segments A-G one at a time and repeat. The wiring is correct so my code is not.

Here's other information that might be helpful. I have a Mega Arduino. Here is my wiring schematic. Please note this circuit is working perfectly. This is a programming flaw. If you see something I'm missing, please let me know. Thanks in advance.

/*

Sample code to test how to get a total of eight 7-digit diplays working together using the SevSeg Library.
Note the eight digits will be split up as follows: [2 digit time][3 digit high score][3digit score]
e.g. if there is 87 seconds AND high score is 654 AND score is 321 then the resulting number array would be 87654321

*/

#include <SevSeg.h> // Library for seven segment display
SevSeg sevSeg;

// *********************************************************************** //
//                       Variables for set up
// *********************************************************************** //

//General variables
unsigned long numberToShow = 0;

// Variables for scoring
int Score = 0;  //0 to 990; //game score
int HighScore = 0;

// Variables for time
int TimeRemaining = 0; // Values 0 to 30

// *********************************************************************** //
void setup() {
// *********************************************************************** //

  //Set up for seven segment displays
  byte segDPins[] = {38, 39, 40, 41, 42, 43, 44}; // Segments A, B, C, D, E, F, G
  byte ccDPins[] = {45, 46, 47, 48, 49, 50, 51, 52};  // Common cathodes for dispays 1, 2, 3, 4, 5, 6, 7, 8
  sevSeg.begin(COMMON_CATHODE, 8, ccDPins, segDPins, false, false, false, true);
  sevSeg.setNumber (numberToShow, 0, LOW);
  sevSeg.setBrightness(120); // Can range -200 to +200 with normal range 0 to 100

  //Data output assignments of Arduino board pins
  pinMode(38, OUTPUT); //Seven segment display anode - segment A on all digits
  pinMode(39, OUTPUT); //Seven segment display anode - segment B on all digits
  pinMode(40, OUTPUT); //Seven segment display anode - segment C on all digits
  pinMode(41, OUTPUT); //Seven segment display anode - segment D on all digits
  pinMode(42, OUTPUT); //Seven segment display anode - segment E on all digits
  pinMode(43, OUTPUT); //Seven segment display anode - segment F on all digits
  pinMode(44, OUTPUT); //Seven segment display anode - segment G on all digits
  pinMode(45, OUTPUT); //Seven segment display cathode - digit 1 (1's digit of Score)
  pinMode(46, OUTPUT); //Seven segment display cathode - digit 2 (10's digit of Score)
  pinMode(47, OUTPUT); //Seven segment display cathode - digit 3 (100's digit of Score)
  pinMode(48, OUTPUT); //Seven segment display cathode - digit 4 (1's digit of HighScore)
  pinMode(49, OUTPUT); //Seven segment display cathode - digit 5 (10's digit of HighScore)
  pinMode(50, OUTPUT); //Seven segment display cathode - digit 6 (100's digit of HighScore)
  pinMode(51, OUTPUT); //Seven segment display cathode - digit 7 (1's digit of time)
  pinMode(52, OUTPUT); //Seven segment display cathode - digit 8 (10's digit of time)

  sevSeg.blank();

  int Score = 321; // values 0 to 990
  int HighScore = 654; // values 0 to 990
  int TimeRemaining = 87; // Values 0 to 120
  
}  //end set up

// *********************************************************************** //
void loop() {
  // *********************************************************************** //

  numberToShow = TimeRemaining * pow(10, 6) + HighScore * pow(10, 3) + Score; // [2 digts time remaining][3 digits high score][3 digits score] = 8 digit number
  sevSeg.refreshDisplay();

} // End of loop ()
/*

  Sample code to test how to get a total of eight 7-digit diplays working together using the SevSeg Library.
  Note the eight digits will be split up as follows: [2 digit time][3 digit high score][3digit score]
  e.g. if there is 87 seconds AND high score is 654 AND score is 321 then the resulting number array would be 87654321

*/

#include <SevSeg.h> // Library for seven segment display
SevSeg sevSeg;

//General variables
unsigned long numberToShow = 0;

// Variables for scoring
int Score = 0;  //0 to 990; //game score
int HighScore = 0;

// Variables for time
int TimeRemaining = 0; // Values 0 to 30

// *********************************************************************** //
void setup() {
  // *********************************************************************** //

  //Set up for seven segment displays
  byte segPins[] = {38, 39, 40, 41, 42, 43, 44}; // Segments A, B, C, D, E, F, G
  byte digitPins[] = {45, 46, 47, 48, 49, 50, 51, 52};  // Common cathodes for dispays 1, 2, 3, 4, 5, 6, 7, 8
  sevSeg.begin(COMMON_CATHODE, 8, digitPins, segPins, true, false, false, true);
  //sevSeg.setBrightness(100); // Can range -200 to +200 with normal range 0 to 100
  //
  //  //Data output assignments of Arduino board pins
  //  pinMode(38, OUTPUT); //Seven segment display anode - segment A on all digits
  //  pinMode(39, OUTPUT); //Seven segment display anode - segment B on all digits
  //  pinMode(40, OUTPUT); //Seven segment display anode - segment C on all digits
  //  pinMode(41, OUTPUT); //Seven segment display anode - segment D on all digits
  //  pinMode(42, OUTPUT); //Seven segment display anode - segment E on all digits
  //  pinMode(43, OUTPUT); //Seven segment display anode - segment F on all digits
  //  pinMode(44, OUTPUT); //Seven segment display anode - segment G on all digits
  //  pinMode(45, OUTPUT); //Seven segment display cathode - digit 1 (1's digit of Score)
  //  pinMode(46, OUTPUT); //Seven segment display cathode - digit 2 (10's digit of Score)
  //  pinMode(47, OUTPUT); //Seven segment display cathode - digit 3 (100's digit of Score)
  //  pinMode(48, OUTPUT); //Seven segment display cathode - digit 4 (1's digit of HighScore)
  //  pinMode(49, OUTPUT); //Seven segment display cathode - digit 5 (10's digit of HighScore)
  //  pinMode(50, OUTPUT); //Seven segment display cathode - digit 6 (100's digit of HighScore)
  //  pinMode(51, OUTPUT); //Seven segment display cathode - digit 7 (1's digit of time)
  //  pinMode(52, OUTPUT); //Seven segment display cathode - digit 8 (10's digit of time)

  sevSeg.blank();

  Score = 321; // values 0 to 990
  HighScore = 654; // values 0 to 990
  TimeRemaining = 87; // Values 0 to 120

}  //end set up

// *********************************************************************** //
void loop() {
  // *********************************************************************** //

  numberToShow = 1000000UL * (unsigned long )TimeRemaining + (unsigned long )HighScore * 1000 + Score; // [2 digts time remaining][3 digits high score][3 digits score] = 8 digit number
  sevSeg.setNumber (numberToShow, 0, LOW);
  sevSeg.refreshDisplay();

} // End of loop ()
wokwi diagram.json
{
  "version": 1,
  "author": "Uri Shaked",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-7segment",
      "id": "7segment",
      "top": -61.38,
      "left": 231.52,
      "attrs": { "digits": "4", "common": "cathode" }
    },
    {
      "type": "wokwi-arduino-mega",
      "id": "mega",
      "top": 84.8,
      "left": 170.2,
      "rotate": 180,
      "attrs": {}
    },
    {
      "type": "wokwi-7segment",
      "id": "7segment1",
      "top": -61.38,
      "left": 433.12,
      "attrs": { "digits": "4", "common": "cathode" }
    }
  ],
  "connections": [
    [ "7segment:DIG1", "arduino:2", "gold", [ "v-20", "h0" ] ],
    [ "7segment:DIG2", "arduino:3", "green", [ "v-24", "h0" ] ],
    [ "7segment:DIG3", "arduino:4", "orange", [ "v-28", "h0" ] ],
    [ "7segment:DIG4", "arduino:5", "purple", [ "v28", "h-150", "*", "v-16" ] ],
    [ "7segment:A", "arduino:6", "gray", [ "v-16", "h0" ] ],
    [ "7segment:B", "arduino:7", "purple", [ "v-36", "h0" ] ],
    [ "7segment:C", "arduino:8", "blue", [ "v20", "h-120", "*", "v-20" ] ],
    [ "7segment:D", "arduino:9", "cyan", [ "v16", "h-94", "*", "v-24" ] ],
    [ "7segment:E", "arduino:10", "green", [ "v12", "h-80", "*", "v-28" ] ],
    [ "7segment:F", "arduino:11", "brown", [ "v-40", "h0" ] ],
    [ "7segment:G", "arduino:12", "orange", [ "v24", "h-134", "*", "v-32" ] ],
    [ "7segment:CLN", "arduino:13", "cyan", [ "v-44", "h0" ] ],
    [ "7segment:COM", "arduino:5V", "red", [ "v300", "h0" ] ],
    [ "7segment:A", "7segment1:A", "green", [ "v-57.6", "h9.6" ] ],
    [ "7segment:F", "7segment1:F", "green", [ "v-67.2", "h182.4" ] ],
    [ "7segment:B", "7segment1:B", "green", [ "v-76.8", "h9.6" ] ],
    [ "7segment:E", "7segment1:E", "green", [ "v18.36", "h201.61", "v-9.6" ] ],
    [ "7segment1:D", "7segment:D", "green", [ "v27.96", "h-172.8" ] ],
    [ "7segment:C", "7segment1:C", "green", [ "v37.56", "h9.6" ] ],
    [ "7segment:G", "7segment1:G", "green", [ "v47.16", "h9.6" ] ],
    [ "mega:45", "7segment:DIG1", "green", [ "v-0.2", "h-24", "v-220.8", "h96" ] ],
    [ "mega:46", "7segment:DIG2", "green", [ "v-0.05", "h14", "v-220.8", "h76.8" ] ],
    [ "mega:47", "7segment:DIG3", "green", [ "v-0.05", "h-33.6", "v-230.4", "h144" ] ],
    [ "mega:48", "7segment:DIG4", "green", [ "v-0.15", "h23.6", "v-115.2", "h115.2" ] ],
    [ "mega:38", "7segment:A", "green", [ "v-0.15", "h33.2", "v-278.4", "h38.4" ] ],
    [ "7segment:B", "mega:39", "green", [ "v-76.8", "h-201.6", "v38.4" ] ],
    [ "mega:40", "7segment:C", "green", [ "v-0.25", "h42.8" ] ],
    [ "7segment:D", "mega:41", "green", [ "v0" ] ],
    [ "mega:42", "7segment:E", "green", [ "v-0.1", "h90.8" ] ],
    [ "mega:43", "7segment:F", "green", [ "v-0.1", "h-52.8", "v-288", "h163.2" ] ],
    [ "7segment:G", "mega:44", "green", [ "v0" ] ],
    [ "mega:49", "7segment1:DIG1", "green", [ "v-0.15", "h-62.4", "v-278.4", "h201.6" ] ],
    [ "7segment1:DIG2", "mega:50", "green", [ "v-19.2", "h-96", "v9.6" ] ],
    [ "mega:51", "7segment1:DIG3", "green", [ "v0", "h244.8", "v-211.2", "h48" ] ],
    [ "7segment1:DIG4", "mega:52", "green", [ "v75.96" ] ]
  ],
  "dependencies": {}
}

I'm trying to simulate your code in Wokwi.
but for now what I saw strange was that you declared the variations as global

int Score =
int HighScore
int TimeRemaining

giving them a value of 0.

And then declared them again as locations within the setup and gave them some values.

And the pinmode for the pins used with the seven segments.
The SevSeg.h library already does this work.

Try this code:

/*

Sample code to test how to get a total of eight 7-digit diplays working together using the SevSeg Library.
Note the eight digits will be split up as follows: [2 digit time][3 digit high score][3digit score]
e.g. if there is 87 seconds AND high score is 654 AND score is 321 then the resulting number array would be 87654321

*/

#include <SevSeg.h> // Library for seven segment display
SevSeg sevSeg;

// *********************************************************************** //
//                       Variables for set up
// *********************************************************************** //

//General variables
unsigned long numberToShow = 0;

// Variables for scoring
//int Score = 0;  //0 to 990; //game score
//int HighScore = 0;

// Variables for time
//int TimeRemaining = 0; // Values 0 to 30

// *********************************************************************** //
void setup() {
// *********************************************************************** //

  //Set up for seven segment displays
  byte segDPins[] = {38, 39, 40, 41, 42, 43, 44}; // Segments A, B, C, D, E, F, G
  byte ccDPins[] = {45, 46, 47, 48, 49, 50, 51, 52};  // Common cathodes for dispays 1, 2, 3, 4, 5, 6, 7, 8
  sevSeg.begin(COMMON_CATHODE, 8, ccDPins, segDPins, false, false, false, true);
  //sevSeg.setNumber (numberToShow, 0, LOW);
  sevSeg.setBrightness(120); // Can range -200 to +200 with normal range 0 to 100

  sevSeg.blank();
}  //end set up

int Score = 321; // values 0 to 990
int HighScore = 654; // values 0 to 990
int TimeRemaining = 87; // Values 0 to 120

// *********************************************************************** //
void loop() {
  // *********************************************************************** //

  numberToShow = TimeRemaining * pow(10, 6) + HighScore * pow(10, 3) + Score; // [2 digts time remaining][3 digits high score][3 digits score] = 8 digit number
   sevSeg.setNumber(numberToShow);
  sevSeg.refreshDisplay();

} // End of loop ()

here

/*
  Sample code to test how to get a total of eight 7-digit diplays working together using the SevSeg Library.
  Note the eight digits will be split up as follows: [2 digit time][3 digit high score][3digit score]
  e.g. if there is 87 seconds AND high score is 654 AND score is 321 then the resulting number array would be 87654321

*/

#include <SevSeg.h> // Library for seven segment display
SevSeg sevSeg;

// Variables for scoring
uint16_t Score = 0;  //0 to 990; //game score
uint16_t HighScore = 0;

// Variables for time
byte TimeRemaining = 0; // Values 0 to 30

void setup() {
  byte segPins[] = {38, 39, 40, 41, 42, 43, 44}; // Segments A, B, C, D, E, F, G
  byte digitPins[] = {45, 46, 47, 48, 49, 50, 51, 52};  // Common cathodes for digits 1, 2, 3, 4, 5, 6, 7, 8
  sevSeg.begin(COMMON_CATHODE, 8, digitPins, segPins, true, false, false, true);

  Score = 0; // values 0 to 999
  HighScore = 0; // values 0 to 999
  TimeRemaining = 0; // Values 0 to 99

}  //end set up

void loop() {
  char numberToShow[] = "        ";
  sprintf(numberToShow, "%2u%3u%3u", TimeRemaining, HighScore, Score); // [2 digts time remaining][3 digits high score][3 digits score] = 8 digit number

  sevSeg.setChars (numberToShow);
  sevSeg.refreshDisplay();

} 

Even testing with:
numberToShow = TimeRemaining * 1000000UL.....
It still shows value error in multiplication:
Printout: 87654320.

PS: Work well this way:

numberToShow = TimeRemaining * 1000000UL + HighScore * 1000UL + Score;

Printout: 87654321.

it is no sense to multiply and add the numbers if we want not to have leading zeros.
just try

  Score = 0; 
  HighScore = 0; 
  TimeRemaining = 1; 

and

  Score = 0; 
  HighScore = 0; 
  TimeRemaining = 0; 

@deronbutler are leading zeros acceptable for you?

@ruilviana I tried the code from post 5 with the multiplication changes and unfortunately my displays are not cooperating. The good news is that the displays no longer all show "8"s. The bad news is each display now has a odd arrangement of segments lit up none of which remotely resemble the intended number. I'll try to make some rhyme or reason for this tomorrow night and report back. I do appreciate the suggestion!

/*

Sample code to test how to get a total of eight 7-digit diplays working together using the SevSeg Library.
Note the eight digits will be split up as follows: [2 digit time][3 digit high score][3digit score]
e.g. if there is 87 seconds AND high score is 654 AND score is 321 then the resulting number array would be 87654321

*/

#include <SevSeg.h> // Library for seven segment display
SevSeg sevSeg;

// *********************************************************************** //
//                       Variables for set up
// *********************************************************************** //

//General variables
unsigned long numberToShow = 0;

// Variables for scoring
//int Score = 0;  //0 to 990; //game score
//int HighScore = 0;

// Variables for time
//int TimeRemaining = 0; // Values 0 to 30

// *********************************************************************** //
void setup() {
// *********************************************************************** //

  //Set up for seven segment displays
  byte segDPins[] = {38, 39, 40, 41, 42, 43, 44}; // Segments A, B, C, D, E, F, G
  byte ccDPins[] = {45, 46, 47, 48, 49, 50, 51, 52};  // Common cathodes for dispays 1, 2, 3, 4, 5, 6, 7, 8
  sevSeg.begin(COMMON_CATHODE, 8, ccDPins, segDPins, false, false, false, true);
  //sevSeg.setNumber (numberToShow, 0, LOW);
  sevSeg.setBrightness(120); // Can range -200 to +200 with normal range 0 to 100

  sevSeg.blank();
}  //end set up

int Score = 321; // values 0 to 990
int HighScore = 654; // values 0 to 990
int TimeRemaining = 87; // Values 0 to 120

// *********************************************************************** //
void loop() {
  // *********************************************************************** //

  numberToShow = TimeRemaining * 1000000UL + HighScore * 1000 + Score; // [2 digts time remaining][3 digits high score][3 digits score] = 8 digit number
  sevSeg.setNumber(numberToShow);
  sevSeg.refreshDisplay();

} // End of loop ()

Hi,
Slow down guys, this is a cross post.

Tom... :grinning: :+1: :coffee: :australia:

@kolaha I also tried your code from post 6 and unfortunately all the displays still show "8"s as before. I understand the intent of the code and that it would eliminate the proceeding zeroes which I would prefer. I'll play around with it again tomorrow. Thanks!

which one? first? with multiplying? dump it, the solution is in post #6

@TomGeorge That's also my post. I was really struggling with using 7 segment displays that needed > 5 volts. I built a pcb and now have power to the displays. I ran a simple sketch that turns on and off all the segments one by one and that works great. Now the challenge is to display the actual numbers i'd like to have displayed.

@kolaha yes I also tried your code from post 6 and unfortunately all the displays still show "8"s as before. I'll play around with your sketch tomorrow. I'll revisit the sketch I thought was working earlier. I'm now suspecting the pcb is printing the "negative" image of the intended digit. I'll report back.

are you upload my sketch or simulate it?

it is not possible because

Did you see the simulator link I posted?
Go there and run the simulator and you will see the code I posted working.

@kolaha @ruilviana I just looked at the simulator and i see that both your codes works. So this leads me to believe that my pcb is NOT working like I thought and that the simple sketch i used earlier to test it was misleading. Back to the drawing board on the hardware then... I'll report back.

yes, but my get rid of leading zeros.
anyway, your question is solved. mark proper answer as solution and the PCB you can discus in your previous topic

I'm opening up this forum thread only to share a link to the DIY "Down the Clown" arcade game which is now completed. Thanks for all your help. Here's a link to a new forum that shares the entire project. link to completed project