4 Digit - 7 segment Multiplexing Help

I am trying to multiplex a 4 digit - 7 segment display. The problem is the "next digit" is still faintly glowing the lit segments of the "previous digit".
My hardware works fine with the Multiplex7Seg library and example but I wanted to learn more about it and come up with something a little simpler.

After some internet research i came up with this:

The code I'm using is a variation of work done here:
http://paulm.com/inchoate/2008/03/arduino_showing_multiplexed_led_mat.html

int DIGIT_PINS[] = {10,11,12,13};
int SEG_PINS[] = {2,3,4,5,6,7,8};
byte PATTERN[5][4]=
{{B1111111,B0000000,B0000000,B0000000},//first part of pattern
{B1111111,B0000000,B0000000,B0000000},//The code is set up to show animations but all frames are identical for testing
{B1111111,B0000000,B0000000,B0000000},
{B1111111,B0000000,B0000000,B0000000},
{B1111111,B0000000,B0000000,B0000000}};//last part of pattern
#define DIGIT_COUNT (sizeof(DIGIT_PINS)/sizeof(DIGIT_PINS[0]))
#define SEG_COUNT (sizeof(SEG_PINS)/sizeof(SEG_PINS[0]))
#define PATTERN_SIZE (5);
#define PATTERN_DELAY (1);

void setup(){
int pinset=0;
Serial.begin(9600);
for (pinset=0; pinset<4; pinset++){
pinMode (DIGIT_PINS[pinset],OUTPUT);
}
for (pinset=0; pinset<7; pinset++){
pinMode (SEG_PINS[pinset],OUTPUT);
}
}

void loop(){
int pS = PATTERN_SIZE;
int pD = PATTERN_DELAY;
//int loopControl = 0;
for (int loopControl =0; loopControl <pS; loopControl++){
long start = millis();
while (millis() - start < pD){
DispOut(PATTERN[loopControl]);
}
} 
}
void DispOut(byte currentPattern[]){
int pattern_index =0;
int last_digit=DIGIT_COUNT-1;
for (int digit_index = 0; digit_index < DIGIT_COUNT; last_digit=digit_index++){
digitalWrite(DIGIT_PINS[last_digit], LOW);
digitalWrite(DIGIT_PINS[digit_index], HIGH);
byte pat = currentPattern[pattern_index];
for (int seg_index = SEG_COUNT-1; seg_index >= 0; seg_index--, pat >>=1){
digitalWrite(SEG_PINS[seg_index], pat & 1 ? HIGH : LOW);
}
delay(3);

pattern_index++;
}
}

The hardware I am using (using 2N3904 transistors):

Any Ideas?

The code in of DispOut turns on the next digit while the old data is still on the segment pins. Try adding the following at the start of function DispOut, to turn off all the segments first:

for (int seg_index = 0; seg_index < SEG_COUNT; ++seg_index){
  digitalWrite(SEG_PINS[seg_index], LOW);
}

I look at the schematic, it a commun cathode configuration, so a 1 will turn on a segment and a 1 will activated the transistor connect to the digit. I did a code for a 4 digit multiplex. And it work, but my config is for a commun anode type, and I use a 7447 chip for the display the segment. And the "flickering" is fine in my code.

Thanks for your help dc42. For some reason the code you suggested didn't work at the start of the function. I had to put it in the digit count "for loop".

Here's where I'm at:
(works good so far!)

int DIGIT_PINS[] = {10,11,12,13};
int SEG_PINS[] = {2,3,4,5,6,7,8};
byte PATTERN[5][4]=
// Digit1 | Digit2 | Digit3 | Digit4
// abcdefg| abcdefg| abcdefg| abcdefg
//    t        E        S        t
{{B0001111,B1001111,B1011011,B0001111},//first part of pattern
 {B0001111,B1001111,B1011011,B0001111},
 {B0001111,B1001111,B1011011,B0001111},
 {B0000000,B0000000,B0000000,B0000000},
 {B0000000,B0000000,B0000000,B0000000}};//last part of pattern
#define DIGIT_COUNT (sizeof(DIGIT_PINS)/sizeof(DIGIT_PINS[0]))
#define SEG_COUNT (sizeof(SEG_PINS)/sizeof(SEG_PINS[0]))
#define PATTERN_SIZE (5);
#define PATTERN_DELAY (1000);

void setup(){
int pinset=0;
Serial.begin(9600);
for (pinset=0; pinset<4; pinset++){
pinMode (DIGIT_PINS[pinset],OUTPUT);
}
for (pinset=0; pinset<7; pinset++){
pinMode (SEG_PINS[pinset],OUTPUT);
}
}

void loop(){
int pS = PATTERN_SIZE;
int pD = PATTERN_DELAY;
//int loopControl = 0;
for (int loopControl =0; loopControl <pS; loopControl++){
long start = millis();
while (millis() - start < pD){
DispOut(PATTERN[loopControl]);
}
} 
}
void DispOut(byte currentPattern[]){
int pattern_index =0;
int last_digit=DIGIT_COUNT-1;

for (int digit_index = 0; digit_index < DIGIT_COUNT; last_digit=digit_index++){
  for (int seg_index = 0; seg_index < SEG_COUNT; ++seg_index){ //set all seg pin low before switching digit
  digitalWrite(SEG_PINS[seg_index], LOW);
  }
digitalWrite(DIGIT_PINS[last_digit], LOW);
digitalWrite(DIGIT_PINS[digit_index], HIGH);
byte pat = currentPattern[pattern_index];
for (int seg_index = SEG_COUNT-1; seg_index >= 0; seg_index--, pat >>=1){
digitalWrite(SEG_PINS[seg_index], pat & 1 ? HIGH : LOW);
}
delay(3);

pattern_index++;
}
}

@brobx

I don't know what was wrong with the code - the recent one or what is the issue you have.... I breadboard the circuit, ( I use 4 commun cathode 7 segment ), 4 2N3904, 1 resistor network of 330 ohms ( for my display led's ) and 4 1 K resistors for the base resistor. I did a "light" touch of the code ( the pins ) to fit my broadboard set-up and the pattern array to display A,B,C,D. If you connect everything properly, you should got the same thing of what I got. What the code do is display A,b,C,d leave on ( about 2 second ) and off ( about 2 second ). I will figure out the rest of the code so I can use for myself. Your pattern ( in the array ), it did display according to the array pattern. But not TEST, something else.

Anyway, here the code :

int DIGIT_PINS[] = {2,3,4,5}; // d1,d2,d3,d4 - I change the pins to fit my breadboard set-up
int SEG_PINS[] = {12,11,10,9,8,7,6}; // a,b,c,d,e,f,g - also I change the pins to fit my breadboard set-up
byte PATTERN[5][4]=
// Digit1 | Digit2 | Digit3 | Digit4
// abcdefg| abcdefg| abcdefg| abcdefg
//    A        b        C        d
// I modify the bits pattern to display A,b,C,d
{{B1110111,B0011111,B1001110,B0111101},//first part of pattern
 {B1110111,B0011111,B1001110,B0111101},
 {B1110111,B0011111,B1001110,B0111101},
 {B0000000,B0000000,B0000000,B0000000},
 {B0000000,B0000000,B0000000,B0000000}};//last part of pattern

// the rest of the code is "as is"

#define DIGIT_COUNT (sizeof(DIGIT_PINS)/sizeof(DIGIT_PINS[0]))
#define SEG_COUNT (sizeof(SEG_PINS)/sizeof(SEG_PINS[0]))
#define PATTERN_SIZE (5);
#define PATTERN_DELAY (1000);

void setup()
{
 int pinset=0;
 Serial.begin(9600);
 for (pinset=0; pinset<4; pinset++)
 {
  pinMode (DIGIT_PINS[pinset],OUTPUT);
 }
 for (pinset=0; pinset<7; pinset++)
 {
  pinMode (SEG_PINS[pinset],OUTPUT);
 }
}

void loop()
{
int pS = PATTERN_SIZE;
int pD = PATTERN_DELAY;
//int loopControl = 0;
for (int loopControl =0; loopControl <pS; loopControl++)
{
 long start = millis();
 while (millis() - start < pD)
  {
   DispOut(PATTERN[loopControl]);
  }
 } 
}

void DispOut(byte currentPattern[])
{
int pattern_index =0;
int last_digit=DIGIT_COUNT-1;

for (int digit_index = 0; digit_index < DIGIT_COUNT; last_digit=digit_index++)
 {
  for (int seg_index = 0; seg_index < SEG_COUNT; ++seg_index)
  { //set all seg pin low before switching digit
   digitalWrite(SEG_PINS[seg_index], LOW);
  }
  digitalWrite(DIGIT_PINS[last_digit], LOW);
  digitalWrite(DIGIT_PINS[digit_index], HIGH);
  byte pat = currentPattern[pattern_index];
  for (int seg_index = SEG_COUNT-1; seg_index >= 0; seg_index--, pat >>=1)
  {
   digitalWrite(SEG_PINS[seg_index], pat & 1 ? HIGH : LOW);
  }
  delay(3);

  pattern_index++;
 }
}