Extending shift register code -

Hello everyone

I’m working on driving four 7 segment displays with four 74HC595 shift registers powerd by an Arduino Uno R3. I want the display to count to 9’999. For this I first tried this tutorial for two shift registers and it’s working fine. I slightly modified the given example code, that I can use it with four shift registers, but it’s not working. Can someone help me out, out there?

here is my code:

int dataPin = 4;
int latchPin = 5;
int clockPin = 6;
byte dataOne;
byte dataTwo;
byte dataThree;
byte dataFour;
byte dataArray[10];

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  Serial.begin(9600);

  //Arduino doesn't seem to have a way to write binary straight into the code 
  //so these values are in HEX.  Decimal would have been fine, too. 
  dataArray[0] = 0xE7; //zero
  dataArray[1] = 0x21; //one
  dataArray[2] = 0xCB; //two
  dataArray[3] = 0x6B; //three
  dataArray[4] = 0x2D; //four
  dataArray[5] = 0x6E; //five
  dataArray[6] = 0xEE; //six
  dataArray[7] = 0x23; //seven
  dataArray[8] = 0xEF; //eight
  dataArray[9] = 0x6F; //nine
  //blinkAll_2Bytes(2,500); 
}

void loop() {
  for (int j = 0; j < 10000; j++) {
    dataOne = dataArray[(int)j/1000];
    dataTwo = dataArray[(int)j/100];
    dataThree = dataArray[(int)j/10];  
    dataFour = dataArray[j%10];
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, dataOne);   
    shiftOut(dataPin, clockPin, dataTwo);
    shiftOut(dataPin, clockPin, dataThree);
    shiftOut(dataPin, clockPin, dataFour);
    digitalWrite(latchPin, 1);
    delay(1000);
  }
}



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {  
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


//blinks the whole register based on the number of times you want to 
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll_2Bytes(int n, int d) {
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, 0);
  shiftOut(dataPin, clockPin, 0);
  digitalWrite(latchPin, 1);
  delay(200);
  for (int x = 0; x < n; x++) {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 255);
    shiftOut(dataPin, clockPin, 255);
    digitalWrite(latchPin, 1);
    delay(d);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 0);
    shiftOut(dataPin, clockPin, 0);
    digitalWrite(latchPin, 1);
    delay(d);
  }
}

sorry for my bad English :confused:

I’m new here but I hope someone can help me, to get it running
here is a link of a picture of my setup where you also can see the problem with the first two segments

//Arduino doesn't seem to have a way to write binary straight into the code //so these values are in HEX. Decimal would have been fine, too. dataArray[0] = 0xE7; //zero

how about

dataArray[0] = B11100111;

not your issue, but you should know that Arduino can handle binary notation...

Hello and welcome,

The way you are trying to split the digits of 'j' is wrong.

One way to do it would be:

int n = j;
dataFour = dataArray[ n % 10 ];
n /= 10;
dataThree = dataArray[ n % 10 ];
n /= 10;
dataTwo = dataArray[ n % 10 ];
n /= 10;
dataOne = dataArray[ n % 10 ];

thank you two for helping me - I made the change with splitting the J, but I still have the same issue - the first two 7 segment displays are lighting up all the time - with the Hex number I'm happy but anyway it's good to know!

I'm sorry guys - feeling so dumb I just found out, thad a connection wasn't right - thank anyway :-)