Why is my code for the four digits seven segment display not working?

I want to display two digits on my four digit seven segment display. I already made a bunch of one digits, and then I tried doing this for two digits:

 for(int i = 0; i < 1000; i+=1)
  {
    digitalWrite(D1, LOW);
  digitalWrite(D2, LOW);
  digitalWrite(D3, LOW);
  digitalWrite(D4, HIGH); 
  
  digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, LOW);   
  delay(1);
  digitalWrite(D1, LOW);
  digitalWrite(D2, LOW);
  digitalWrite(D3, HIGH);
  digitalWrite(D4, LOW); 
  //0
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  delay(1); 
  }

I want to display the number 10. But when I run it, there's a faint zero on both digits but a strong 1 on the second digit. Why is this happening? Thanks.

1mS delay is barely enough time to see the LED turn on.
Try increasing it to 8mS, that would give a refresh rate of about 30 Hz across the 4 digits.

No, actually it is not so much the delay, but you have the sequence totally wrong! :astonished:

Turn the current digit off
Set up the segments for the new digit
Turn the new digit on
delay (1 should actually be sufficient!, but 5 may be better)
Turn the new digit off
Set up the segments for the following digit
Turn this next digit on
delay (1 should actually be sufficient!, but 5 may be better)
complete loop.

Why is all this in a "for" loop? The loop() function does all the looping for you!

In summary:

For each digit,
Set up the segments for the new digit
Turn the new digit on
delay (1 should actually be sufficient!, but 5 may be better)
Turn the new digit off

1 Like

Because I first show the number zero for one second (using the for loop) then the number one, then two, three, four...