digit shadowing in 7 seg display

hi guys!
i have tried coding a 7 seg 4 digit CA display but shadowing of digits occurs
so inresing refresh rates solves it but digits start blinking

i will be reallu obliged if someone points out my mistake.

here is the code :

//  74hc595 pn Q7,Q6,Q5,Q4,Q3,Q2,Q1,Q0
// mapping to  DP, G, F, E, D, C, B, A
// 595 ip as per 595.xls spreadsheet
// 595 op fed to uln 2003 ip therefore CC bit format. 
// digit shadowing occurs at 5ms refresh rate
int refresh=5;
byte dig[4]= {B00111111,
              B00000110,
              B01011011,
              B01001111, //o
};
int latchpin=2;
int clockpin=3;
int datapin=4;
int dig1=5;
int dig2=6;
int dig3=7;
int dig4=8;

void setup() {
  // put your setup code here, to run once:
 for (int i=2;i<9;i++)
 pinMode(i,OUTPUT);
}
void displaydigit(byte digit)
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin,clockpin,MSBFIRST,digit);
  digitalWrite(latchpin,HIGH);
}
void digit_pos()
{
  {
  digitalWrite(dig1, LOW); // digit 1 on for CC type display
  digitalWrite(dig2, HIGH); // digit 2 off
  digitalWrite(dig3, HIGH); // digit 3 off
  digitalWrite(dig4, HIGH); // digit 4 off
  displaydigit(dig[0]);           
  delay(refresh);      // display hold
  digitalWrite(dig1, HIGH);
  digitalWrite(dig2, LOW);
  digitalWrite(dig3, HIGH);
  digitalWrite(dig4, HIGH);
  displaydigit(dig[1]);
  delay(refresh);
   digitalWrite(dig1, HIGH);
  digitalWrite(dig2, HIGH);
  digitalWrite(dig3, LOW);
  digitalWrite(dig4, HIGH);
  displaydigit(dig[2]);
  delay(refresh);
   digitalWrite(dig1, HIGH);
  digitalWrite(dig2, HIGH);
  digitalWrite(dig3, HIGH);
  digitalWrite(dig4, LOW);
  displaydigit(dig[3]);
  delay(refresh);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}
}

void loop() {
  // put your main code here, to run repeatedly:
digit_pos();
}

cheers,
marrc

Do not leave the previous digit pin LOW from the previous digit while you write the next digits.
Set it HIGH immediately after delay.

Sequence should be...

displaydigit
Specific digit pin LOW
delay(refresh);
Specific digit pin HIGH

Yes, don't turn the digit ON until the segments are set and don't set the segments until the previous digit is off.

void digit_pos()
{
  displaydigit(dig[0]);
  digitalWrite(dig1, LOW); // digit 1 on for CC type display
  delay(refresh);      // display hold
  digitalWrite(dig1, HIGH);


  displaydigit(dig[1]);
  digitalWrite(dig2, LOW);
  delay(refresh);
  digitalWrite(dig1, HIGH);


  displaydigit(dig[2]);
  digitalWrite(dig3, LOW);
  delay(refresh);
  digitalWrite(dig3, HIGH);


  displaydigit(dig[3]);
  digitalWrite(dig4, LOW);
  delay(refresh);
  digitalWrite(dig4, HIGH);
}

This may also be a problem. You are turning off digits using the wrong pin numbers:

int dig1 = 5;
int dig2 = 6;
int dig3 = 7;
int dig4 = 8;

  displaydigit(dig[3]);
  delay(refresh);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
}

clocking out to a 595 using digitalWrite() and a for() loop can take a long time, especially if you are clocking both segments and commons. Using direct port manipulation is much, much, much faster. The code isn’t as portable (to other pin numbers) and more lines, but in my experience, what took 65 microseconds, was reduced to 16 microseconds.

//Pxn = Latch Pxn = Clock Pxn = Segment Pxn = Digit
//PORTx |= (1 << Pxn);// digitalWrite(pin, HIGH);
//PORTx &= ~(1 << Pxn);// digitalWrite(pin, LOW);

johnwasser:
Yes, don't turn the digit ON until the segments are set and don't set the segments until the previous digit is off.

void digit_pos()

{
 displaydigit(dig[0]);
 digitalWrite(dig1, LOW); // digit 1 on for CC type display
 delay(refresh);      // display hold
 digitalWrite(dig1, HIGH);

displaydigit(dig[1]);
 digitalWrite(dig2, LOW);
 delay(refresh);
 digitalWrite(dig1, HIGH);

displaydigit(dig[2]);
 digitalWrite(dig3, LOW);
 delay(refresh);
 digitalWrite(dig3, HIGH);

displaydigit(dig[3]);
 digitalWrite(dig4, LOW);
 delay(refresh);
 digitalWrite(dig4, HIGH);
}




This may also be a problem. You are turning off digits using the wrong pin numbers:


int dig1 = 5;
int dig2 = 6;
int dig3 = 7;
int dig4 = 8;

displaydigit(dig[3]);
 delay(refresh);
 digitalWrite(2, HIGH);
 digitalWrite(3, HIGH);
 digitalWrite(4, HIGH);
 digitalWrite(5, HIGH);
}

thanks a ton...it worked!

I doubt it...

displaydigit(dig[1]);
  digitalWrite(dig2, LOW);
  delay(refresh);
  digitalWrite(dig1, HIGH);

Edit: ignore me. I see you’ve just superfluously included all of johns post in your reply, and weren’t posting you actual completed code.

Really you should learn to use an array for your pin numbers.

hi guys!

i have modified previous code so that the digit select pins are also activated by 74hc595. unfortunately i am not getting "helo"pattern on display insted it displays "6666"constantly.
my code is as follows:

//  74hc595 pn Q7,Q6,Q5,Q4,Q3,Q2,Q1,Q0,Q7,Q6,Q5,Q4,Q3,  Q2,  Q1,  Q0
// mapping to  DP, G, F, E, D, C, B, A,NC,NC,NC,NC,dig4,dig3,dig2,dig1
// 595 ip as per 595.xls spreadsheet
// 1st 595 op fed to uln 2003 ip therefore CC bit format.
//2nd 595 op fed to uln2003 ip, whose op in turn to pnp transistor input. 

int refresh=1;
byte digit[4]= { B01110100,//h
              B01111001,//e
              B00111000,//l
              B11011100, //o
};
byte digitPort[4]={B00000001,// DIG1 ENABLE
                    B00000010,//DIG2 ENABLE
                    B00000100,//DIG3 ENABLE
                    B00001000,//DIG4 ENABLE

};

int latchpin=2;
int clockpin=3;
int datapin=4;


void setup() {
  // put your setup code here, to run once:
 for (int i=2;i<5;i++)
 pinMode(i,OUTPUT);
}
void displaydigit(byte digit, byte digitPort)
{
  shiftOut(datapin,clockpin,LSBFIRST,digitPort);
  shiftOut(datapin,clockpin,MSBFIRST,digit);
  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin,LOW);
}


void loop() {
  // put your main code here, to run repeatedly:
for (int j=0;j<4;j++)
displaydigit(digit[j],digitPort[j]);
}

i would be very thankful if someone can point out my mistake.

cheers,
marrc

sorry there is a typo, the first shiftout is:

shiftOut(datapin,clockpin,MSBFIRST,digitPort);