7 segments display counter:How to tell Arduino not to display the 0 placeholder

Hello all! I’m working on a counter using 2 double digit 7 segments modules. It displays from 0 to 9999 and back again. I got it working, but I don’t know how to turn off specific digits if the number to be displayed hasn’t gotten to some of the place values (tens place, hundreds place, thousands place). For example: (tens place, hundreds place, thousands place). For example:

Display 1 : would display 0 0 0 1 , instead of just 1
Display 123: would display 0 1 2 3 , instead of 123

Below is my full code:

#define PATTERN_COUNT 11 //number of segment patterns, 0-9 and all off
#define SEGMENT_COUNT 7  //how many segments I'm controlling
#define DIGIT_COUNT  4   //how many digits I'm controlling



            //                    A,B,C,D,E,F,G
int segmentPins[SEGMENT_COUNT] = {2,3,4,5,6,7,8};

//pins for each digit
int digitPins[] = { 9, 10, 11, 12 };
//                                          0      1     2    3      4     5     6     7    8     9     OFF
byte digitPatternsHex[PATTERN_COUNT] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00 };

//counter
int counter = 0;

void setup(){
  
    //init segment pins to output
    for(int thisPin=0; thisPin<SEGMENT_COUNT; thisPin++){
      pinMode(segmentPins[thisPin], OUTPUT);
    }

    //init digit pins to output
    for(int thisPin=0; thisPin<DIGIT_COUNT; thisPin++){
      pinMode(digitPins[thisPin], OUTPUT);
    }
}
 
//turn all digits off, HIGH because common cathode
void allDigitsOff(){
  for(int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++){
    digitalWrite(digitPins[thisPin], HIGH);
  }
}

//turn specific digit on
void digitOn(int digitNum){
  
  digitalWrite(digitPins[digitNum], LOW);
  
  delay(2);
}


void setPattern(int pattern){
  allDigitsOff(); 
  
  //data pins, 8bits
    for(int thisPin=0; thisPin<SEGMENT_COUNT; thisPin++){
      if(bitRead(digitPatternsHex[pattern], thisPin) == 1){
        digitalWrite(segmentPins[thisPin], HIGH);
      }else{
        digitalWrite(segmentPins[thisPin], LOW);
      }
    }
  
}

void showNumber(int currentNumber){
  
  //display digit from right to left
  int digitInFront = currentNumber/10;
  for(int currentDigit = 0; currentDigit < DIGIT_COUNT; currentDigit++){
    
    //get number in the ones place
    int number = currentNumber % 10; //5
    
    currentNumber /= 10;

    setPattern(number); 
    
    digitOn(currentDigit);
        
  }
}

void loop(){
  
//return first 2 digits of ms, 1 sec = 1000 millisec, 1.5 sec = 1500 millisec
  
  int currentNumber = (millis()/1000); 
  
  showNumber(currentNumber);
}

They're called "leading zeros" FYI. Is this a school assignment?

Not a school assignment, I just want to learn about Arduino :)

Fundamentally, you just look at the value remaining in variable "currentnumber" and see whether it is greater than or equal to 10. If it is, you print a digit, if not, you print a space.

@phnguyen0912

You want to learn Arduino! But, how?

You should start with a simple one and then gradually moves to the complex one; instead, you have started with a 4-digit multiplexed type display unit, and you want to suppress the leading zeros.

1. Place only one display device (say, DP0 of the following circuit) on the breadboard. Connect it with the Arduino as per given diagram. Place current limiting resistors 220 - 680 ohm in series with each of the segment lines. These are not shown in the diagram for simplicity.

Figure-: Connection diagram of a 4-digit cc-type multiplexed display unit

2. Write code to show 2 at DP0 position.

3. Place DP1 item on the breadboard and connect it in parallel with DP0. Connect the cc1 (common cathode pin of DP1) with Arduino’s digital pin-9.

4. Write code to show 03 at DP0 and DP1 positions simultaneously.

5. Write code to suppress leading 0 from DP0 position.

6. Connect DP2 and DP3 to form a 4-digit display unit.

7. Define a 4-bye wide array with data items (cc codes) for the digits 0, 0, 1, 2.

8. Write program to show the contents of the array of Step-6 on the display unit.

9. Write program to suppress the leading zeros from DP0 and DP1 positions.

if (myArray[0] != 0x3F)
   goto     LX;
00h ----> myArray[0];
if(myArray[1] != 0x3F)
  goto  LX;
00h ----> myArray[1]

LX: keep refreshihing display with the contents of myArray[];

byte myArray[] = {0xnn, 0xnn, 0xnn, 0xnn};
byte x = 0b11111111;

for (int n =0; n<=3; n++)
{
     PORTD = myArray[n];
     PORTB = bitClear(x, n);
     delay(100);                            //if display flickers, vary the time delay.
}