Lighting Up One LED at a Time

Hello,

I have a series of 7- 595 shift registers hooked up to my Arduino Uno board. I have set up these registers exactly the way they are set up in the shift out tutorial and I am able to turn whatever lights on I wish and have turned on all the lights at once so I know that the hardware is set up correctly. I just need some code that will turn on then off each light in sequence so I can make sure the lights are in the proper order.

I am sure that this is something easy to do, but my searches of the forum have not turned anything up and I am a programming n00b. I would assume that the answer would have something to do with the bitShift command, but I can't wrap my brain around getting this to transfer between shift registers.

I have made so much progress with my project and have learned a lot about Arduino programming, but this particular problem has brought my project to a standstill for a couple months. Thank you in advance for any help you can provide.

Heath

I have examples of sending various patterns to 595s here:

Thanks for the link Nick.

I did not get a chance to try it out tonight, but definitely will be trying the code out tomorrow night and will let you know how it works.

Heath

Hey Nick,

Had a little time to look at the code. I guess I am confused. In my other codes I have specified a latch pin a clock pin and a data pin. I don't see where that is in your code, is that because you are SPI? I guess I am confused.

Thank you again for your help.

Heath

  digitalWrite (LATCH, LOW);
  SPI.transfer (c);
  digitalWrite (LATCH, HIGH);

Latch is there. SPI does clock and data. Simple.

If you daisy-chain them, just keep clocking:

  digitalWrite (LATCH, LOW);
  SPI.transfer (0xAB);
  SPI.transfer (0xCD);
  SPI.transfer (0xEF);
  SPI.transfer (0x42);
  digitalWrite (LATCH, HIGH);

If you want to use shiftout you need to send the appropriate byte value that corresponds to the 8 pins, 1 is on 0 is off.

ie if you want a LED chaser, write the latch low, and send the register

1 which is "00000001" then write the latch high, delay, latch low, then send
2 which is "00000010" then write the latch high, delay, latch low, then send
4 which is "00000100" ....
8 ...
16 ....
32
64
128 which is "10000000" ....

then shift those values in reverse to make the LED go the other way.

This is a nifty binary to dec converter
http://acc6.its.brooklyn.cuny.edu/~gurwitz/core5/nav2tool.html

EDIT: Sorry I reread your post, I see what you're asking now. Take a looks at this function from my LED chaser that uses 3 959s.
The first parameter is which pin (in my case since I have 3 969s its between 0-23, in your case you have 7 so it'll be between 0-55) and the second parameter is which state 0 off 1 on. The bitWrite function is where the magic happens.

void registerWrite(int whichPin, boolean whichState) {
  digitalWrite(latchPin, LOW);

  bitWrite(bitsToSend, whichPin, whichState);
  bitShift = bitsToSend;

  byte registerThree = lowByte(bitShift);
  bitShift = bitShift >> 8;
  byte registerTwo = lowByte(bitShift);
  bitShift = bitShift >> 8;
  byte registerOne = lowByte(bitShift);
  
  shiftOut(dataPin, clockPin, MSBFIRST, registerThree);
  shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
  shiftOut(dataPin, clockPin, MSBFIRST, registerOne);

  digitalWrite(latchPin, HIGH);
}

Nick - I dug a little deeper into SPI and it looks like there are standard pins to connect the registers to on the Arduino for SPI to control things properly? Right now my latch pin is on pin 8, my data pin is on pin 11, and my clock pin is on pin 12. Is there a way to reset the defaults in SPI to match this configuration? I don't really want to change the way I have it wired at this point as it would take a long time to rewire it. Plus, I have all the LED's and switches working so I don't really want to mess with it. Any ideas?

Racer X89 - I'm slowly but surely wrapping my head around your code. Sorry I am slow on this. Where is WhichPin defined?

Thanks so much to both of you for your help.

Heath

whichPin is defined at the at the start of the function

void registerWrite(int whichPin, boolean whichState) {

bitsToSend is a 24 bit long which corresponds to the 24 pins off the 3 registers I'm using. For example if bitsToSend has the value of 8421504, thats
100000001000000010000000 in binary. So it'll basically light up the first pin on each register.

So lets say you want to turn the fourth pin on (out of 24)
you want this one to be a 1 --- 100000001000000010000000

So you call this function. bitWrite(bitsToSend, whichPin, whichState);
bitsToSend is the variable
whichPin (in this case you'll send a 3 (since we start a 0)) is the pin that you want to change.
whichState is what you want to change it to (0 or 1).

So now bitsToSend has the value of 9470080 which is 100100001000000010000000 in binary.

Now since I'm using 3 shift registers chained together I have split the 24 bit number into 3 8 bit numbers and call shiftout() 3 times.

This part of the code does just that.

bitShift = bitsToSend; --- copy it to another variable so we don't destroy the original

byte registerThree = lowByte(bitShift); --- takes the lowest 8 bits and stores them to registerThree as a byte
bitShift = bitShift >> 8; --- moves the whole thing down 8 bits
byte registerTwo = lowByte(bitShift); --- take the next 8 lowest bits and stores them to registerTwo
bitShift = bitShift >> 8; --- shift again
byte registerOne = lowByte(bitShift); ---store the last 8 bits

Then its all shifted out and the three registers together have the value bitsToSend represents.

Heathnorton:
Is there a way to reset the defaults in SPI to match this configuration?

No, it's a hardware thing. If it is all pre-wired you may as well use shiftOut.