2x Shift Register: Turn ON/OFF each LED individually

Hy guys! :) I have a question: How can i use two Shift Registers (74HC595) to switch On or OFF each LED individually?

I have installed two Shift Registers (74HC595) on my breadboard and 16 LEDs. The connections are working perfectly, but i can't find a code to do what i need.

I tried the following codes: 1). http://bildr.org/2011/02/74HC595/ Problem: I can toggle each LED individually, but the first one is not working. 2). http://danceswithferrets.org/geekblog/?p=69 Problem: I can't figure out how to use the code to toggle each LED. :( 3). http://www.multiwingspan.co.uk/arduino.php?page=led6 Problem: I don't understand the coding. :D

So, can you point me in the right direction? I need some examples, or tutorials to figure out how to do what i need. :D Thanks!

With shift registers you cannot "turn LEDs on and off individually". You have to send an entire new "pattern" to the shift register, which has only one bit different to before.

You need to have a local variable that contains your "pattern", modify that, then send the entire set to the shift registers again.

majenko: With shift registers you cannot "turn LEDs on and off individually". You have to send an entire new "pattern" to the shift register, which has only one bit different to before.

I know that.

majenko: You need to have a local variable that contains your "pattern", modify that, then send the entire set to the shift registers again.

I already have that. I used in my code booleans for each LED. But i need an idea for a function to toggle them. :D

Keep two byte variables that have an image of the value you want on the SRs. Change the bits that need changing and shift the two bytes out.

byte sr1 = 0;
byte sr2 = 0;


sr1 |= 4;      // set bit 3 in SR2
sr2 = 0xFF;  // set all bits in SR2
sr2 &= ~(1 << 5);   // clear bit 5 in SR3 

shiftOut (...,sr1);  // shift them out, add parms for pins
shiftOut (...,sr2);

Study up on bit manipulation.


Rob

3AgL3_DeeJay:
How can i use two Shift Registers (74HC595) to switch On or OFF each LED individually?

What are you trying to accomplish?
Honestly.
What’s the situation?
What’s the objective?

3AgL3_DeeJay: I have installed two Shift Registers (74HC595) on my breadboard and 16 LEDs. The connections are working perfectly, but i can't find a code to do what i need.

So you have decoupling for both the shift registers? Failure to decouple them could lead to various hard to fathom bugs. 0.1uF ceramic from Vcc to ground right next the the chips.

MarkT:

3AgL3_DeeJay: I have installed two Shift Registers (74HC595) on my breadboard and 16 LEDs. The connections are working perfectly, but i can't find a code to do what i need.

So you have decoupling for both the shift registers? Failure to decouple them could lead to various hard to fathom bugs. 0.1uF ceramic from Vcc to ground right next the the chips.

Also, are they in a single 16-bit chain, or two separate 8-bit shift registers?

I have a sketch here that does that:

http://www.gammon.com.au/forum/?id=11518

However basically it does what the other posters said, you keep a copy of the bit patterns that you want on/off and send that when required.

Good tutorial here as well:

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

Graynomad: Study up on bit manipulation.

I'm trying. :D

MarkT: So you have decoupling for both the shift registers? Failure to decouple them could lead to various hard to fathom bugs. 0.1uF ceramic from Vcc to ground right next the the chips.

As in this image: http://www.gammon.com.au/images/74HC595_schematic.png Why use that? :D I don't know what are you saying by "decouple", my english is not so good. :(

[quote author=Nick Gammon link=topic=184338.msg1365306#msg1365306 date=1377388179] I have a sketch here that does that: http://www.gammon.com.au/forum/?id=11518 However basically it does what the other posters said, you keep a copy of the bit patterns that you want on/off and send that when required. [/quote] I will try this now.

lemming: Good tutorial here as well: http://arduino.cc/en/Tutorial/ShiftOut

I tried this first, but i didn't make the code to work.

[quote author=Runaway Pancake link=topic=184338.msg1365278#msg1365278 date=1377385098] What are you trying to accomplish? Honestly. What's the situation? What's the objective? [/quote]

I will explain my project: I receive comands (one char at the time) through the serial port. Then, the arduino lights up the LED according to the command that it received. I send commands like "a", "g", "d" and so on. I have 18 LED, so i use the first 18 letters of the alphabet. For example, the letter "a" (since is the first letter of the alphabet) will toggle the LED 1. The letter "d" will toggle LED 4. Easy to to that. But i can't use all my pins to do that. So that's why i want to use the 74HC595. In my code, i used some booleans to keep track of my LEDs. So when i receive a command (for example "a") i check to see if the boolean for that LED is HIGH or LOW, then i toggle the LED according to its boolean.

So i think that i need to use a function like this: 1. In "void setup" i set all my booleans to LOW, then i send all "0" to the 74HC595. 2. After i receive a command, i modify only the boolean for the LED according to that command. 3. Then need a function to convert all the booleans to 1 and 0 4. Then i send the data to 74HC595

Example: Lets say that my booleans are like this: HIGH, LOW, LOW, LOW, HIGH, HIGH, LOW, HIGH, (and 8 more) So i have to convert that in: 1 0 0 0 1 1 0 1

PS: Yes, i have two 74HC595 wired up just like this: http://www.gammon.com.au/images/74HC595_schematic.png

3AgL3_DeeJay:

MarkT: So you have decoupling for both the shift registers? Failure to decouple them could lead to various hard to fathom bugs. 0.1uF ceramic from Vcc to ground right next the the chips.

As in this image: http://www.gammon.com.au/images/74HC595_schematic.png Why use that? :D I don't know what are you saying by "decouple", my english is not so good. :(

Those two capacitors at the top of the diagram - they are required and they need to be right next to the chips to be effective.

MarkT: Those two capacitors at the top of the diagram - they are required and they need to be right next to the chips to be effective.

Ok. I will add those 0.1uF capacitors. :) Thanks!

3AgL3_DeeJay:
Lets say that my booleans are like this:
HIGH, LOW, LOW, LOW, HIGH, HIGH, LOW, HIGH, (and 8 more)
So i have to convert that in: 1 0 0 0 1 1 0 1

Not sure to understand, HIGH = 1 and LOW = 0 already. Or do you mean you want to join the 8 bools into a single byte?

There are more than one way to do it, one of them is:

bool
  b1 = true,
  b2 = false,
  b3 = true,
  b4 = true,
  b5 = true,
  b6 = false,
  b7 = false,
  b8 = false;
        
uint8_t result = (b1<<7) | (b2<<6) | (b3<<5) | (b4<<4) | (b5<<3) | (b6<<2) | (b7<<1) | b8;

result will be 0b10111000

But I suggest you just use a single byte instead of 8 booleans (which takes already a byte of memory each), and instead of assigning true or false to one of the boolean, simply change the corresponding bit of the byte…

Also, there is a cool little library on this page: http://bildr.org/2011/02/74hc595 http://bildr.org/2011/08/74hc595-breakout-arduino

I have modified it to add some useful functions and SPI support, just tell me if you want it.

guix: Or do you mean you want to join the 8 bools into a single byte?

Exactly! Only thing is that there will be 16 booleans. :D

Then if i send the byte resulted from your code, it will do what i need? :)

3AgL3_DeeJay: Then if i send the byte resulted from your code, it will do what i need? :)

Yes

Sorry here is the correct link to the library: http://bildr.org/2011/08/74hc595-breakout-arduino/

guix: Sorry here is the correct link to the library: http://bildr.org/2011/08/74hc595-breakout-arduino/

I found that a few days ago. And i'm still searching for a store in my country to buy that part. ;))

You don’t need to buy this breakout board, the code will work with your “naked” shift registers as well :slight_smile:

guix: You don't need to buy this breakout board, the code will work with your "naked" shift registers as well :)

I know that. But it will look better in my final project than with my own soldered board :))

I did it! :slight_smile: Wooo! I’m so happy! :stuck_out_tongue_closed_eyes:
I used the “Shifter.h” library from this article: bildr Arduino: Individually Control Shift Register Pins - bildr
Thanks guix! :slight_smile:

Here is a demo:

Thanks all! :slight_smile: