UNO, one 74HC595 and four 8-segment displays, just querying

Just a general query as got it all running although I expect my coding could be better. It all runs and flickers but I am assuming that is from the switching and can it be got rid off, or am I stuck with it?

I could use 3 more 74HC595 but was just seeing if I could get it working without. So really just after some feedback. Here is my code. I have 2 timing values one for overall, and one for switching between the 8-segment displays. Am only starting out so go easy :slight_smile:

const int SER     = 8;
const int LATCH   = 9;
const int CLK     =10;
const int LED1    =2; //on off for 7-segment one
const int LED2    =3; //on off for 7-segment two
const int LED3    =4; //on off for 7-segment three
const int LED4    =5; //on off for 7-segment four



int interval = 200; //how many millis to cycle between numbers 
int interval2 = 11; //delay between switching between 7-segment displays

int firstdigit = 0; //integers to hold individual digit values
int seconddigit = 0;
int thirddigit = 0;
int fourthdigit = 0;

//sequence of 0-10
int sequence[10] = {63,6,91,79,102,109,124,7,127,111}; //numbers 0-9 for segment

void showvalues(int one,int two, int three, int four) //function to display values
{
  unsigned long currentmillis = millis(); //store current millis
  do
  {
    digitalWrite(LATCH, LOW);
    shiftOut(SER, CLK, MSBFIRST, sequence[one]);
    digitalWrite(LATCH, HIGH);
    digitalWrite (LED1, HIGH);
    delay(interval2);
    digitalWrite (LED1, LOW);
    
    digitalWrite(LATCH, LOW);
    shiftOut(SER, CLK, MSBFIRST, sequence[two]);
    digitalWrite(LATCH, HIGH);
    digitalWrite (LED2, HIGH);
    delay(interval2);
    digitalWrite (LED2, LOW);
    
    digitalWrite(LATCH, LOW);
    shiftOut(SER, CLK, MSBFIRST, sequence[three]);
    digitalWrite(LATCH, HIGH);
    digitalWrite (LED3, HIGH);
    delay(interval2);
    digitalWrite (LED3, LOW);
    
    digitalWrite(LATCH, LOW);
    shiftOut(SER, CLK, MSBFIRST, sequence[four]);
    digitalWrite(LATCH, HIGH);
    digitalWrite (LED4, HIGH);
    delay(interval2);
    digitalWrite (LED4, LOW);
    
    Serial.print(one);
    Serial.print("  ");
    Serial.print(two);
    Serial.print("  ");
    Serial.print(three);
    Serial.print("  ");
    Serial.print(four);
    Serial.print("  currentmillis: ");
    Serial.print(currentmillis);
    Serial.print(" actual millis: ");
    Serial.println(millis());
    
    unsigned long int x = millis() - currentmillis;
    
  } while ((millis()-currentmillis) < interval);
}

void setup()
{
  Serial.begin(9600); //start serial
  
  pinMode(SER, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop()
{
  for (int i = 0; i < 9999; i++)
  {
    int b = i;
    firstdigit = b % 10;
    b = b/10;
    seconddigit = b % 10;
    b=b/10;
    thirddigit = b % 10;
    b=b/10;
    fourthdigit = b % 10;
    showvalues(firstdigit, seconddigit, thirddigit, fourthdigit);
  }
}

WP_20140622_001[1].jpg

Does the flickering improve if you speed up the serial speed, try 115200, or comment out all the serial prints?

Many thanks, at first neither worked. But then remembered that I had tried using delaymicroseconds instead of delay (didn't work). But with the serial print commented out I tried tweaking the timing for interval2 (timing between switching the segment displays)again. This resulted in a nice display when set at 5 or below.

Have also moved the serial print lines in to the actual void loop () and display is still good. I should of considered the impact of the serial printing embedded in the function especially if cycling so fast.

Many thanks again for the pointer and help.

Glad to help.