Help with programming with a 74HC595N to control leds

I am still starting out with arduino and I'm trying to pick up the programming language. I have made a 3x3 matrix and using the digitalWrite() function, I was able to program some simple animations. Anyway, I would like to make an 8x8 led matrix using shift registers to control the leds. I would like to use shift registers because it seems like the easiest way to get more outputs using less pins. I did see a video on youtube that showed a way to multiplex an 8x8 matrix that only used 12 pins, but it used more transistors, resistors, wires, and soldering than I would like to do.
After watching this video on youtube; How Shift Registers Work! - YouTube I am fairly certain I have an understanding for how the shift registers physically work, but the programming part is still questionable. I have scoured the internet for a good tutorial and I haven't really found any that are clear, I really only found the a tutorial, to light up 8 leds in a pattern, many many times, so I decided to give it a go.
I have set up the circuit with only one shift register from this page; http://arduino.cc/en/tutorial/ShiftOut and I got some sample code, I believe from the adafruit tutorials, and modified it slightly to fit my set up. This is what the code looks like;

int latchPin = 8;
int dataPin = 11;
int clockPin = 12;

byte leds = 0;

void setup(){
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop(){
leds = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(500);
}
}

void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}

This just makes the leds light up one by one until they are all on, then turns them all off and repeats. I have two questions about this code; for i it goes from 0 to 8, I'm assuming this is for the 8 outputs there are coming from the shift register. However I have seen other codes using shift registers that have this same set up, and the value goes from 0 to 255 so I'm not really sure what this means/does. My other question is that in this code they have "byte leds = 0" this is the only code with shift registers that I've seen that uses this byte function in their code, and I don't know why this is so.

Also, my other concerns are that this is the only way I can program the leds. I would like to program them to light up in a sequence that I can customize, but I'm not sure how to do this. So, is this code similar to the way I would set up my code to light up any led I want? Or is the code set up in a totally different way? And my understanding is that to make an 8x8 matrix, I would need 2 shift registers, one for the cathodes and one for the anodes, and activate them corresponding to which led I want to light up, is this correct?
I apologize for this being so lengthy, but thanks in advance!

Welcome!

trendz3:
My other question is that in this code they have "byte leds = 0" this is the only code with shift registers that I've seen that uses this byte function in their code, and I don't know why this is so.

The 74HC595 is an 8-bit shift register. Since the Arduino Uno uses an 8-bit microprocessor, under it's architecture there are 8 bits to one byte. Because of this, a byte is the perfect way to store information that you will shift out to the 74HC595! Both being 8-bit representations, they require no further manipulation.

trendz3:
However I have seen other codes using shift registers that have this same set up, and the value goes from 0 to 255 so I'm not really sure what this means/does.

An 8-bit binary number can represent numerical values up to 2^8, or 0-255. Hence this setup. Those loops are just using the integer representation of the byte and are likely cycling through all of its possible binary permutations.

trendz3:
Also, my other concerns are that this is the only way I can program the leds. I would like to program them to light up in a sequence that I can customize, but I'm not sure how to do this. So, is this code similar to the way I would set up my code to light up any led I want? Or is the code set up in a totally different way? And my understanding is that to make an 8x8 matrix, I would need 2 shift registers, one for the cathodes and one for the anodes, and activate them corresponding to which led I want to light up, is this correct?

You are correct. By writing a '1' to a bit of your Anode register, you turn "on" that row of LEDs. By writing a '0' to a bit of your Cathode register, you ground that column (I am assuming you connect your anodes as rows and your cathodes as columns in the matrix), and the LED turns on. However, a problem arises when you want to turn on multiple rows and multiple columns at the same time. It you drive multiple rows HIGH, and ground a column, all the LEDs in that column whose rows are driven high will light. Therefore, you have to multiplex the matrix. If you haven't heard the term, this is a technique by which you "scan" through each column of LEDs, grounding each in sequence, and each time shifting out a new byte to your Anode (row) register. By doing this very very quickly, it gives the illusion that all the LEDs that you specify are on.

I'll leave you with this: Multiplexing with the Arduino and 74HC595.
This was my first tutorial with the 595 and multiplexing with Arduino, and it explained a lot of the basics to me. I'll never profess to be nearly as accomplished as half the people on this forum, but I'll be happy to help you out with this!

Thank you! I have never come across this instructable before, which is strange because i have searched pretty much everywhere. Also, a question for you and everyone in general, do you know how to program with individual binary units? I saw it on a couple of instructables, but I cannot find it right now. For instance, if it was an 8-bit shift register and the code would be something like: B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001 and the 1 would correspond to the led that was lit. it was something along these lines. This seems like a much easier way to program this but I'm not sure how to do it.
Another general question are led drivers programmed the same way as shift registers? I was also considering learning to use them because they are made specifically for driving leds, but I haven't been able to find much information about them.

trendz3:
Another general question are led drivers programmed the same way as shift registers? I was also considering learning to use them because they are made specifically for driving leds, but I haven't been able to find much information about them.

Hi trendsz3, sounds like the first thing you need to work on is your Google skills! There is lots of stuff around about led driver chips. Two of interest would be saa1064 and, particularly for you, max7219.

In general, they do work differently to shift registers, and differently to each other. They are usually easier to work with. When using shift registers, you have to get your sketch to do the multiplexing. LED driver chips often do this for you. Multiplexing involves lighting only some of your leds at any instant, then moving on to the next group until they have all been lit, but doing it so fast that to the eye it appears they are all lit together. So when using shift registers, your sketch must light each group of leds in sequence, quickly, and keep doing this over and over. With led driver chips, your sketch just has to tell the driver chip which leds should be lit, once, and it then takes care of repeating the sequence.

trendz3:
Also, a question for you and everyone in general, do you know how to program with individual binary units? I saw it on a couple of instructables, but I cannot find it right now. For instance, if it was an 8-bit shift register and the code would be something like: B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001 and the 1 would correspond to the led that was lit. it was something along these lines. This seems like a much easier way to program this but I'm not sure how to do it.

Not 100% clear what you are asking here, but it sounds like you want to send that sequence of binary values to your shift register. That's easy. Just shift a "1" in, then keep shifting "0"s in and that's exactly what will happen naturally. However, if you have 2 shift registers chained together, this may not be an option. In that case, have a look at the ">>" operator in the Reference section.

trendz3:
And my understanding is that to make an 8x8 matrix, I would need 2 shift registers, one for the cathodes and one for the anodes, and activate them corresponding to which led I want to light up, is this correct?
I apologize for this being so lengthy, but thanks in advance!

You are going to have some problems with that unless you are careful. Each LED will need a minimum of, say, 5mA to light it reasonably brightly. But the 74hc595 can only provide around 20mA per output, and a total of 70mA per chip. Imagine you want to light all 64 of your leds. You use multipexing so that 8 are lit at a time, then the next 8 and so on, but do this fast enough to fool the eye. Problem is that a single output from one of the '595s will have to provide 8 x 5mA = 40mA, so it will be overloaded. If you reduce this down to 2mA per led, you avoid overloading the chip, but the leds will be very dim. There are 3 (at least) ways around this: 1. use 8 transistors to boost the current that each '595 output can provide; 2. use a single ULN2803 chip to do the same thing; 3. use a high-current version of the '595, such as tpic6c595. But the easiest way, by far, is to use that max7219 instead of the shift registers.

Paul

for (int i = 0; i < 8; i++)
This counts from 0 to 7, 0-1-2-3-4-5-6-7, for 8 total.

Setting these up in an array:
B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001
would be one way to select say the one layer that would be turned on at any one time:
byte cathodeArray[] = {B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001, };
If each layer was controlled by an NPN or N-channel MOSFET and the 8 transistors were then driven by a 74HC595 output, that would work great.
This example assumes the cathode '595 has a seperate latch pin from the 8 anode 595's.
SPI.transfer is used to send the data out.
SCK connects to SRCLK
MOSI connects to Ser Data In
there are seperate SS pins for the cathode '595 and daisychained 595s, connected to RCLK.
SRCLR is connected to +5
OE is connected to GND, or to an Arduino PWM pin for brightness control.

// check time using millis(), when it's time for the next layer:
currentTime =  millis();
elapsedTime = currentTime - previousTime;
if (elapsedTime >= layerTime){ // layerTime = 2mS, 31Hz refresh rate for cube
previousTime = previousTime + layerTime; // set up for next time check

// x used to track layer count
x=x+1;
if (x==8){x=0;} // reset after do all 8

// turn off any cathodes
digitalWrite (cathodeLatch, LOW);
SPI.transfer(0); // shift in 0s
digitalWrite (cathodeLatch, HIGH); // rising edge updates the output register

// send out anode data from an array, each 8 bytes represents 1 layer of data
digitalWrite (anodeLatch, LOW);
SPI.transfer(adodeArray[(x*8) +0]);
SPI.transfer(adodeArray[(x*8) +1]);
SPI.transfer(adodeArray[(x*8) +2]);
SPI.transfer(adodeArray[(x*8) +3]);
SPI.transfer(adodeArray[(x*8) +4]);
SPI.transfer(adodeArray[(x*8) +5]);
SPI.transfer(adodeArray[(x*8) +6]);
SPI.transfer(adodeArray[(x*8) +7]);
digitalWrite (anodeLatch, HIGH);

// now turn the next cathode on
digitalWrite (cathodeLatch, LOW);
SPI.transfer(cathodeArray[x]); // shift in 0s
digitalWrite (cathodeLatch, HIGH); // rising edge updates the output register

} // end time check