CD4201B shiftin latency

I'm using this example to shiftin button states:

Everything works fine but I've found with projects that require multiplexing there is serious lag. Sometimes there's ways around this problem, but sometimes I just need to get those values and keep the loop running without any delay. Is there any way around this with the CD4201B and if not is there anyway around this with something else?


I think you will have to modify your code to poll the button states during non-critical times but without seeing the code it's hard to be more specific.

I think you can make the example 'shiftIn()' function run faster if you use constants for the pin numbers instead of passing function arguments. The compiler can optimize much better if it knows what the values are at compile time.

Well it’s not any specific code. But say I’m multiplexing a 7seg led clock and I need to check for inputs fairly rapidly for time changing, there’s no right time for a pause. There only thing I could think of is to have a button that tells the clock to start checking the other buttons then stop again but I sure would like to avoid that.

Later on I’ll try putting in constants and see if that helps. Thanks.

You could also use ATMEGA internal hardware based SPI to shift with instead of the software driven shiftIn() command, willl be much faster.

stoopkid: say I'm multiplexing a 7seg led clock and I need to check for inputs fairly rapidly for time changing, there's no right time for a pause.

When I wrote code to multiplex a 7-segment clock display I had to put in a 2 or 3 millisecond delay while each digit was lit or the display would be quite dim. During that delay time would be a good time to be shifting in all your button values. If about 400 refreshes per second is not a fast enough polling rate for your purposes (4-digit display refreshed 100 times per second) you may need different hardware.

Using constants seems to have sped it up a little. Crossroads, where would I find an example of that? I can't seem to get some good search keywords. This seems like it would be a more commonly encountered problem. Is there other hardware normally used for this sort of thing? Are there any alternatives to using the 4201?

Thanks guys.

Find an example of connecting the SCK line to a shift register serial clock line, the MISO to the shift register data out line, and the SS line to the parallel clock-in line to capture the data before you read it in? I don't know, did you look for SPI in the playground?

Ah, okay I see. But it seems just a bit beyond me, I'll try to find something that suits my needs and give it a try. Thanks.

Its not that difficult. Physical wiring from the arduino to shift register is similar.
Programming is similar:

You need things like
#include <SPI.h>
int SS = 10; // need for SPI

in setup:
SPI.begin(); // nothing in () because we are the master
pinMode(SS, OUTPUT); // Slave Select for SPI

in loop:
digitalWrite(SS,LOW); // take the SS pin low to select the chip - adjust this to clock data into your part
data_in = SPI.transfer(data_out_byte); // does 2 things at once using same burst of clock pulses:
// clocks out data on MOSI line to a shiftout register like HC595 if connected,
// and reads in data on MISO from a shiftin register
digitalWrite(SS,HIGH); // take the SS pin high to de-select the chip - set up the next clock-in

Go do some reading here, in case you need to slow down the clock pulse speed for your particular hardware