Code for adding an additional shift register

Hi, i have made a circuit for 8 LED that uses a shift register and works (Not my code by the way) What i want to do is add an additional 8 LED on a second shift register and i have built the circuit after finding out how to physically add the second shift register. What i have no idea about is what changes to the code below i need to make to 'access' the second shift register. I'm guessing that even if i get the new code the second set of LED will flash in the same sequences as the first?

My next step once i get both sets of LED working off the 2 registers would be to have a second matrix for the second set of LED, is that possible so that i get two different arrays going?

thanks in anticipation for your help:

// The pin configuration for the shift register.
int  data = 2;
int clock = 3;
int latch = 4;

// the animation sequence for the LED display
// first column is the LED status in binary form, second column is the timing in milliseconds
byte patterns[48] = {
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00011000, 200,
  B00100100, 200,
  B01000010, 200,
  B10000001, 200,
  B01000010, 200,
  B10100101, 200,
  B01011010, 200,
  B00100100, 200,
  B00011000, 200
};

// variables used for status
int pattern_index = 0;
int pattern_count = sizeof(patterns) / 2;

void setup() {
  // setup the serial output if needed
  Serial.begin(9600);

  // define the pin modes
  pinMode( data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
}

void loop() {
  // activate the patterns
  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, patterns[pattern_index*2]);
  digitalWrite(latch, HIGH);

  // delay for the timing
  delay(patterns[(pattern_index*2) + 1]);

  // move to the next animation step
  pattern_index ++;

  // if we're at the end of the animation loop, reset and start again
  if (pattern_index > pattern_count) pattern_index = 0;
}

Assuming it is all wired correctly, you just have to send another 8 bits out. To make both registers/displays show the same pattern simply change this:

digitalWrite(latch, LOW);
shiftOut(data, clock, MSBFIRST, patterns[pattern_index*2]);
digitalWrite(latch, HIGH);

to this:

digitalWrite(latch, LOW);
shiftOut(data, clock, MSBFIRST, patterns[pattern_index*2]);
shiftOut(data, clock, MSBFIRST, patterns[pattern_index*2]);
digitalWrite(latch, HIGH);

Yes, it is possible to have two different animations. You would probably want to make another array (call it patterns2) with the same length as patterns but a different animation and then change the code to this:

digitalWrite(latch, LOW);
shiftOut(data, clock, MSBFIRST, patterns2[pattern_index*2]);
shiftOut(data, clock, MSBFIRST, patterns[pattern_index*2]);
digitalWrite(latch, HIGH);

The code, as written, will follow the delays specified in patterns, not patterns2. You can have the animations have a different number of "frames" and follow different delays, but this will complicate the code just a little. Get it working with two different patterns of the same length first.

I assume that you have added the second shift register in series with the first one. Is that the case ?

If so, then when you shift in the second set of data to the first shift register the first set of data will be shifted into the second shift register and will be displayed there. Is that what is happening ? It may help to temporarily add a delay(1000); to the start of the loop() function to help see what is happening.

If you have not added the second shift register in series with the first then how have you connected it ? If you have and the patterns do not appear as described above then check your wiring and describe what is happening.

This page has an excellent description of adding a second shift register http://arduino.cc/en/tutorial/ShiftOut and has example code to drive them.

Joen

you are a star, thank you very much. I will give this a go and let you know how i got on.

Thank you again.

Jim

UKhelibob

I have connected the second register in series. I haven't tested all of the LED yet as i had no idea of how to initialise the second register in code. Once i have had a play later on i will be able to see how it all works and try to add the additional code i need for two separate alternate flashing LED outsieof these chaser sequences! The whole circuit is for a model i am building so im hoping to get it going so that i can start the build. The ultimate aim is to run everything off an ATTiny85 which i think i can manage if i can keep the pin count down so wish me luck!

Joen

Im a little confused, although i havent tried this yet. I understand your first example for adding an additional register and display the same pattern on both sets of LED. For adding an additional pattern i cant see where the pattern names have been added or called by the code. In your second example the code is exactly the same as the additions for the first and i don't know how to name or call the second pattern. Could you help me out with an explanation of how to name that second pattern and where and how to differentiate between the two patterns in the code. Please don't get me wrong, if it work ill be chuffed to bits but it would also be nice to know why and how it actually works as i will be using this same process quite a lot over the course of the next year or so. :slight_smile:

Thank you

JoeN

My mistake on the naming, i see where that has happened now. Ill give it a go. Note self, LOOK!