Infuriating shift register

Hi all,

I'm just getting to grips with the shift register and am having a little trouble.
To begin with all I want to be able to do is turn on one (out of eight) LEDS.
I think (but am clearly wrong) that this code should do it:

int datapin = 2; 
int clockpin = 3;
int latchpin = 4;

byte data = 0;

void setup()
{
  // Set the three SPI pins to be outputs:

  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);  
  pinMode(latchpin, OUTPUT);
  
}

void loop()
{
  shiftWrite(0, HIGH);
  
}

void shiftWrite(int desiredPin, boolean desiredState)
{
  bitWrite(data,desiredPin,desiredState);
  shiftOut(datapin, clockpin, MSBFIRST, data);
  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);
}

But my output looks like this (see attached image) - not the one LED I was looking for!

I am pretty sure everything is connected to the correct terminal, I have disassemble and reassembled several times.

Please help!

Thanks

Llewmihs

IMG_20150307_083504.jpg

Notice in the example, the latch pin is asserted before calling shiftOut, you toggle the pin after shifting (and the opposite assertion, LOW or HIGH first??? ).

http://arduino.cc/en/Reference/ShiftOut

void loop() {
  //count up routine
  for (int j = 0; j < 256; j++) {
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, j);   
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, HIGH);
    delay(1000);
  }
}

That's great - thanks. I think I have it working now!

Llewmihs