Serial to Parallel Shifting-Out with a 74HC595 Example

I have been working with Serial to Parallel Shifting-Out with a 74HC595 Example http://arduino.cc/en/Tutorial/ShiftOut and I am trying to work with Code Sample 1.2 One by One. All I get when I open the Serial Monitor and type in an ASCII number 0-9 like I believe I am supposed to do and hit enter I can see the corresponding LED flick on and off very quickly and only the first LED stays on. I thought for each number I enter the corresponding LED stays on (4 = LED 5 on). Has anyone had experience with this example and had success? I tried the first sample, Code Sample 1.1 Hello World, and it works just fine so I believe my circuit hooked up right.

Well firstly, remove the capacitor (it’s wrong) but I can’t remember off hand exactly what that example does.

Here’s some code written by bhagman (from Rogue Robotics) which turns on/off an LED (0-15) in either red, blue or purple (both). I’m using 4 daisy chained shift registers - the first two controlling red LEDs and the second two controlling blue LEDs. The values for the previous LEDs are stored so the other LEDs remain as they were when you turn another one on/off.

void setLED(uint8_t lednum, uint8_t colour)
{
  red_leds = (red_leds & ~(1 << lednum));
  blue_leds = (blue_leds & ~(1 << lednum));

  if (colour == LED_RED || colour == LED_PURPLE)
  {
    red_leds |= (1 << lednum);
  }
  if (colour == LED_BLUE || colour == LED_PURPLE)
  {
    blue_leds |= (1 << lednum);
  }
 
  shiftOut(dataPin, clockPin, MSBFIRST, (uint8_t) (blue_leds >> 8));
  shiftOut(dataPin, clockPin, MSBFIRST, (uint8_t) blue_leds);
  shiftOut(dataPin, clockPin, MSBFIRST, (uint8_t) (red_leds >> 8));
  shiftOut(dataPin, clockPin, MSBFIRST, (uint8_t) red_leds);
  
  digitalWrite(latchPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(latchPin, LOW);
}

void clearLEDs()
{
  red_leds = 0;
  blue_leds = 0;

  for (int i = 0; i < 4; i++)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
  }

  digitalWrite(latchPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(latchPin, LOW);
}

You then need this sort of stuff at the top of your sketch:

/////////////////////////SHIFT REGISTER LED STUFF
const int latchPin = 8;//Pin connected to latch pin (ST_CP) of 74HC595
const int clockPin = 12;//Pin connected to clock pin (SH_CP) of 74HC595
const int dataPin = 11;//Pin connected to Data in (DS) of 74HC595

uint16_t red_leds;
uint16_t blue_leds;

#define LED_OFF 0
#define LED_RED 1
#define LED_BLUE 2
#define LED_PURPLE 3
/////////////////////////

Thanks for the reply and the sample code.

From what I can tell from the description and comments for the example I am trying to get to work. I am supposed to be able to type any number from 0 to 7 and the corresponding LED is supposed to light up and I suppose stay on until I choose a different number. All it does is turn on and off in a flash at very low intensity. I can barely see it happening.

I am quite new to the Arduino world. Maybe this code will help me figure out how to work with this shift register.

Well I would test it but unfortunately I don't have any Shift Registers set up atm.

How's your wiring? Have you connected all the ground pins?

Just adding my two pennies worth. I put together this example also and found the same feature as you. The LED illuminates momentarily.

I added a delay before the LatchPin is sent low again:
// turn off the output so the pins don't light up
// while you're shifting bits:
delay(1000);
digitalWrite(latchPin, LOW);

This holds the LED on for 1 second. I did this because I intend to replace the LED's with relays and wanted to test that each relay responded as expected.

You could also move the two commands to the "Turn On" section:
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
delay(1000);
digitalWrite(latchPin, LOW);

Something else I found with this example is that Q0 (PIN15 on the 74HC595) is always on and only goes off when you activate one of the other Q pins but comes back on again when those go low (turn off) again.

Hope this helps