595 & 4021 multiplexing

Hello all,

I know you're busy so I'll be brief, ish. I have an array of 16 solid state arrays which I want to drive with two 595 shift registers. I also have over 18 buttons, rotary encoders and switches which I'd like to use as inputs for them through three or more 4021 shift registers.

I have worked out how to read the pins of a single 4021 and I'm confident I can use the existing examples to work out how to read the pins of two or three more, so I don't think that's a problem at the moment. The part I'm struggling with is the code to set the outputs to the 595's if I need to have multiple pins high simultaneously as the corresponding buttons are pressed in a completely random pattern.

As far as I currently know, I can't shift out one byte for one pin then another byte for another pin without overwriting the byte currently stored in the register, so I'd have to code every possible combination of input presses and therefore every possible combination of pins high and low. Of course, this is ludicrous when you have potentially over 18 inputs and 16 outputs, and there must be a way to do this, but I can't find it on the internet or in my brain.

I'm guessing there's some kind of mathematical calculation for this or an automatically generating array trick (I did read about a method of generating an array then reading from it in Processing, actually, but I suspect doing that would fill the memory of the Arduino pretty quickly if I have all those combinations to do), but to be honest I've fallen behind in learning Arduino coding and have been concentrating more on learning the actual electronics. Plus I was in the bottom class for maths at school so I have no idea what I'm doing there, either.

Thanks for taking the time to read this and thank you in advance for any replies you might send my way.

Cheers,
Neema

Just keep byte variables for each shift register, modify the bits in the variables then shift all of them out.

byte x = 0;
byte y = 0;

shiftOut (y...);
shiftOut (x...);

x |= B00010000;
y |= B00010001;

shiftOut (y...); // turn on bits 0 and 4 in one SR
shiftOut (x...); // turn on bit 4 in the other SR

y &= B1111110;

shiftOut (y...); // turn off bit 0 in the other SR, bit 4 stays on
shiftOut (x...); // no change here


Rob

Graynomad:
Just keep byte variables for each shift register, modify the bits in the variables then shift all of them out.

Wow, thanks, I think that kind of went over my head, though... Actually, I've just looked up the Bitwise Compound Or and And operators (I've never seen |= or &= before); I take it they're what I want to use if I want to be able to 'stack' two bytes together? Like, if I wanted 00000001 and 00000010 together I'd use one of those operators (|=) to get 00000011, or something along those lines?

Thanks again!

If you are not conformable with bit wise operations try the bitSet() and bitClear() functions they might be easer to get your head round.

Grumpy_Mike:
If you are not conformable with bit wise operations try the bitSet() and bitClear() functions they might be easer to get your head round.

Thank you, I'll look into that, though I found this Arduino Playground - BitMath and it's explaining it quite well. I'll have a play around with some code when I can and see what I get.

Cheers guys!

Hello, nemma_t.

I am new here on this forum, singed in this week, and from Slovenia, so if I write anything incorrectly, don`t be mad;)

I am working on the same project as you are/had, I need to do this in one month, for my presentation in school, so I could finish school subject. Hope you understand, What I mean.

So, here It goes.
I bought shift in register HCF4014BE, and wired it exactly as the circuit on Arduino page shows. All though I did not use the 10k resistors, I use 8,2k, but I think that is not the point. Then I copied and pasted the program called "What is pressed", also from arduino page. When I run the program, I dont get any understanding code on Serial monitor. I thought that, when I would push the, let us say third button, that is 3 in DEC, I could get 00000100 in BIN and nothing else. Now, when I upload the code on my arduino uno board, I dont even have to push any button, I always get some incorrect BIN code.

This is my first step I am working on now in my project, and it is all going wrong. Later I meant to add a second shift in register, so I could have sixteen buttons, and two shift out registers, so I could lit up that LED diode, which could represent the button I would push. I know you know, what I mean;)

I dont have much knowledge, we studied Arduino language in school, but on much easier, basic examples. I surfed all over the internet to find any example based on shiftIn/shiftOut, I read all about shifting on Arduino page many times, but I didnt get far. Now I am turning up to you guys, help me.

I would be really grateful I you, or anyone, who read this post, could help me a bit.

Frenk

I can't find that "What is pressed" code, can you post it here.

How are you sending the data to the serial monitor?

If you are doing Serial.print (x) and x = B00000100 then you won't see anything because B00000100 == 4 and that's not a printable character.

Try Serial.print (x,HEX) or Serial.print (x,BIN)


Rob

Thanks for replying, here is the link to the code I am using.

Sorry, I miscalculated the logic number, it is 4. My brains don`t work fine today.
I use Serial.println (X, BIN);

11100
e
f
g

111000
f
g
a

1110000
g
a
b

11100001
C
a
b
c

11000011
C
d
b
c

10000111
C
d
e
c

1110
d
e
f

11100
e
f
g

111000
f
g
a

1110000
g
a
b

11100001
C
a
b
c

Hey, this is what I get on serial monitor, when I upload the program. Like I said, I don`t have to push any button, and I get this.

Maybe I forget to say, that now I have only one shift in register and eight buttons.

Thanks for your time

That code prints the raw value then several chars from an array.

So what numbers/characters are you seeing?

Are you sure the SR is connected correctly?

OK, crossed posts. I'll have a think.


Rob

I don`t have to push any button, and I get this.

It looks like the data is essentially random, you should get all 1s or 0s. Are you sure the SR is connected correctly?


Rob

Have you got pull up resistors on the push button inputs to the shift register?
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

It looks like the data is essentially random, you should get all 1s or 0s. Are you sure the SR is connected correctly?

Hello Rob.
I am sure, that the register is connected correctly. I checked a few times. Maybe my protoboard is a problem. But I checked the connections with multimeter and all seems fine.

Have you got pull up resistors on the push button inputs to the shift register?
Inputs

Hello Mike.
Great page! I read all about inputs, I have a pull down connection, now I think that I am having problems with my wires connected from buttons to register? Maybe there are too long and there are acting as an antenna?

Thanks for reply

I forgot to say something. I am using 8,2k resistors in my connection. Do I have to replace them with smaller value resistors?

thanks

Pull downs aren't great most professionals would always use pull ups when given the choice. Any interference picked up by long wires is then automatically grounded instead of having to work its way through the supply to ground. Also you don't need as low a value for the resistors. A value of 8K2 might be alright but it depends on exactly what chip you have for example for a 74LS595 it is a bit high.

Isnt 74LS595 shift out chip? I tried a different protoboard, but I get the same results. I tried to use pull ups, but I think I cant write the program to work correctly. Also I didnt find time to work on my project, hope I will made it till than. I dont know why the interference would be a problem with a wires only two or three centimeters long(from buttons to chip) and bit more from chip to arduino board? Maybe the material the wire is made is guilty?
I also tried to connect only one button on chip, and when I push it, I get all eight logic "1" on serial monitor.

Isn`t 74LS595 shift out chip?

Yes it is, are you using it as an input? It is the 4021 that is the input chip. Have you got a link to how you have wired it up.
That length of wire is fine.

I also tried to connect only one button on chip, and when I push it, I get all eight logic "1" on serial monitor.

That sounds to me like you haven't wired things up right.