Create a shift register using Arduino

Hello everyone. I'm working on a small circuit using numeric keypad, register and display. I'm a bad writer so pardon me if my writing is messy :smiley: . First, I'll explain my circuit so I attached a picture of the schematic I made in Multisim and also the file just in case.

When i press A numeric key, it will shows on the first display. When i press another key, previous number will displace to the second display while new number will show on first display. It will repeat the same process whenever a key is pressed.

I'm using the shift register 74194 to do the displacement but I want to remove it and use Arduino instead. I started to learn about Arduino recently so my knowledge is kinda shallow. I have a general idea of how to write the code but I don't have a concrete idea of how to do it (I hope I explained myself well).

From I have deduced is that basically what the code will do is read 4 inputs from the Decimal to BCD (74147) and write 8 outputs on the BCD to 7 Segment. So for the inputs I think I'll need to create an array and for the outputs I'll need to make an iteration for each display.

I want to know if I'm on the right track or if there is a more optimal way to do it.

I also don't know how to read the input only when a key is pressed. I have been looking for information on Google but I can't seem to find it or maybe I'm not searching correctly. I know how to read an input from a directly connected switch but I don't know how when it's from the Decimal to BCD. I was thinking about using the idea of a "Clock" like in my schematic. I connect 4 cables from the output of the Decimal to BCD to an AND gate for one input on Arduino. But I'm not sure if this will work unless I try it first.

Thanks in advance.

Register.zip (579 KB)

So for the inputs I think I'll need to create an array and for the outputs I'll need to make an iteration for each display.

I want to know if I'm on the right track or if there is a more optimal way to do it.

Basically yes, see http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

I also don't know how to read the input only when a key is pressed.

You can't. What you do is to record each input in a variable and compare it to the last time you read it. If they are different then an input has become high or become low. The last thing you do in the loop is make previous input values the values you just read. This is known as a state change and there is an example in the IDE of how to do this.

Your idea of the key recognition won't work. If a switch bounces, the same key would be recognized multiple times. So you'll have to debounce the input anyhow.

For the shift register, you can use the shift operators << and >>, and some bit masking, to emulate the 74194 with 8 output pins.

I already wrote a working code. I'll post it here in case someone will need to do something similar and want it as reference in the future:

 int Encoder[4]={14, 15, 16, 17};
 int Decoder[8]={6, 7, 8, 9, 2, 3, 4, 5};

 int Num1[4]={0};  
 int Num2[4]={0};

 int buttonState=0;
 int lastButtonState=0;

void setup()
{

 for(int i=0; i<4; i++) pinMode(Encoder[i], INPUT);

 for(int i=0; i<8; i++) pinMode(Decoder[i], OUTPUT);
 
}

void loop()
{


 for(int i=0; i<4; i++) bitWrite(buttonState, i, digitalRead(Encoder[i]));

 if (buttonState != lastButtonState)
 {
   
   if (buttonState > 0)
   {

     for(int i=0; i<4; i++) Num2[i] = Num1[i];
     for(int i=0; i<4; i++) Num1[i] = digitalRead(Encoder[i]);  
     delay(50);
   
   }
   
 }
 
 lastButtonState = buttonState;
   
 for(int i=0; i<4; i++) digitalWrite(Decoder[i], Num1[i]);
 for(int i=4; i<8; i++) digitalWrite(Decoder[i], Num2[i-4]);
 delay(50);

}

Thanks guys.

Did you made your code italic? Don't think so, please read How to use the forum and go back and add code-tags please :slight_smile:

How about some comments in the code?

Very nice code, except for the many empty lines and missing comments :slight_smile:

Ah, the display should be updated only after button state changed.
And what are the delays good for?

Do you consider to optimize it, for using less loops and arrays? Or for using ports, instead of single pins?