Help please with shift registers

I am trying to get a project done tonight.... and I think I am having some kind of brain meltdown.

I have 3 74HC595 shift registers... I need to be able to control 21 led's one at a time via the serial port....

I would like to use ascii characters (any range) to turn each led on and off.

need to have indipendent control over each Led, (e.g. 5 on, 16off then turn one on...)

need an all on, all off fucntion too (single character if possible too like led control).

I know this sounds like alot, and I am not a complete newbie.... but have been figting with this for 4hrs and its getting late. hoping for a quick answer...

thx d :~

void loop() {
    if (Serial.avaialble()) {
        char c =;
        if (c >= 'A' && c <= 'U')  // 'U' == 'A'+20
            toggleLED(c - 'A');  //  Toggle LED by number 0 through 20
       if (c == 'X')
       if (c == 'Y')

This worked great!!! Thanks for all the help…

do have one question… not sure what I am doing wrong… but would like to change the pin definitrions in this code to

D9 (SS) to ST_CP (pin 12) of all 74HC595 chips
D8 (MOSI) to DS (pin 14) of the first 74HC595 chip
D10 (SCK) to SH_CP (pin 11) of all 74HC595 chips

the code is below as from the website… and works awsome!!!

when I try and change the LATCH alone to pin 9 I get nothing so I am sure I am missing something. I also dont see any definitions for pins 13 and 11???


ps - I am using the updated example at bottom of page…

MOSI and SCK are fixed on the Uno board (if that is what you have). You can't change them, which is why they aren't in the code. They are hardware SPI pins. Rework so you can use those pins (move whatever you are using pins 11 and 13 for, to somewhere else).

Moving latch to pin 9 should be OK, but be aware that the SPI hardware needs pin 10 to be an output, otherwise it goes into slave mode. I would stick with my pins and move other stuff.