16 bit shift register with LED Help

Hi, Basically. I’m trying to do this on 16 leds using 2 shift registers.

The pattern is as follows: From all led turned off, light up one by one (lsb to msb) until every led is lit and then from the last lit led, going backwards (msb to lsb), turning everything off one by one as well.

This is what I have, it does lit up the leds one by one and then turn them off one by one but I want them turned off like I described above. The code below turns them off from lsb to msb.

int latchPin = 15;
int clockPin = 14;
int dataPin = 16;

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

void loop() {
  for(int i=0; i < 16; i++) {
    digitalWrite(dataPin, LOW);
    digitalWrite(clockPin, LOW);
    digitalWrite(latchPin, LOW);
    
    digitalWrite(dataPin, HIGH);
    digitalWrite(clockPin, HIGH);
    digitalWrite(latchPin, HIGH);
    delay(100);
  }

    for(int i=0; i < 16; i++) {
    digitalWrite(dataPin, LOW);
    digitalWrite(clockPin, LOW);
    digitalWrite(latchPin, LOW);
      
      digitalWrite(dataPin, LOW);
      digitalWrite(clockPin, HIGH);
      digitalWrite(latchPin, HIGH);
      delay(100);
  }
}

Thanks for any info.

Hello,

You must update all outputs in the chain, from the first to the last. That is, if you want to turn off the last LED (15), you have to first set the LEDs 0-14 to HIGH, then 15 to LOW. To turn off the next LED (14), you have to set LEDs 0-13 to HIGH, then 14-15 to LOW.. etc

There are small and useful libraries to control shift registers. This one for example (scroll down for library and example).

And just so you know, you can use SPI to send the bytes. I suggest you read this :slight_smile:

Is this really that complicated? Again newbie here.

What I have is basically what I want, I just want the 'direction' of the led being turned off (second loop) to start from the opposite side.

I've been looking at shiftOut now you're giving shifter. I did try out shiftOut but for some reason the 1st and second shift register just mimics each other, they don't seem to act independently.

I'm sure I'll get it eventually but if I can find an easy to follow example, especially for what I want to do in the OP.

No, you won’t get it that way. Because in the first instance, you are depending on the innate direction of shifting in the actual shift register. You are effectively asking that it be reversed. You can’t do that in hardware. That is what reply #1 is trying to tell you.

The solution is to write out all the bits every time you change anything.

aarg:
The solution is to write out all the bits every time you change anything.

Yup. Just write a function that takes a 16-byte int as a parameter and blatts it out to the shift register. Then use that.