I have a problem with the shift register 74HC595.
The sketch I use is able to enable a specific led (8 are connected).
When I enable led 0 to 7 it behaves like normally (they switch on when I enter the numbers).
However, for the led connected to pin 15 (other side of the ic, see layout) it behaves differently. When I switch this led on, ALL leds go off. When I retry, nothing happens. When I enable another led, after a short time (but can be seconds) the led for pin 15 is emitting light together with all others which where already enabled.
This seems like a malfunction in the IC (I checked already another led, another cable, another resistor). The only think I haven't checked is moving the IC to another position of the breadboard (that would take a lot of time since I have to rewrite everything). Is it possible I missed something or is the shift register broken?
The code to switch on a led:
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
where leds are the indices of the leds (0..7).
1 Answer 1
You cannot directly give an index (0..7) to a shift register if you want the index to represent the LED number to be switched on. You have to build a table similar to the following:
byte led[8] = {
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000
};
and later . . .
shiftOut(dataPin, clockPin, LSBFIRST, led[leds]);
where leds is a number 0..7
-
3Or more cheaply
shiftOut(dataPin, clockPin, LSBFIRST, bit(leds));
Edgar Bonet– Edgar Bonet2017年02月25日 09:34:12 +00:00Commented Feb 25, 2017 at 9:34 -
1Nice. I knew there had to be an even quicker way for such a simple mapping.6v6gt– 6v6gt2017年02月25日 11:24:51 +00:00Commented Feb 25, 2017 at 11:24
-
Thanks ... my example uses the shiftOut, but I will find another example and check if it also gives problems and than post the layout/full code if needed.Michel Keijzers– Michel Keijzers2017年02月25日 12:12:29 +00:00Commented Feb 25, 2017 at 12:12
-
Somehow it seems to work, after using another scheme (although it looked exactly equal). So I'm really not sure why the problem existed; now it seems to work ... wondering if I should remove the question since there might be useful information for others (regarding the table and shiftOut function.Michel Keijzers– Michel Keijzers2017年02月25日 19:01:35 +00:00Commented Feb 25, 2017 at 19:01
leds
value and howleds
variable was declared (unsigned
Vs.signed
may have surprising effect on your code).