Using shift register with demultiplexer to create 8x8 chart.

Hello.I’m trying to create 8x8 leds which use 8 buttons to control 8 lanes using 74HC595 and 74LS138. But as im new to arduino i can’t write code for it. I have tried writing a code but it had no effect so i didn’t share it here.

so i didn't share it

Well, I have an answer. I'm not 1000% certain that it is correct, so I didn't share it.

We have no idea what mode you used for the pins that the switches are connected to. We have no idea whether you read the state of the pins at all.

We have no idea what mode you used for the pins that the 74LS138s are connected to, or what process you use to get data out those pins.

Ok here’s the code that i wrote that bore no fruit:

int tDelay = 500;
int latchPin = A1;
int clockPin = A0;
int dataPin = 13;

int A = 12;
int B = 11;
int C = 10;
     
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int buttonPin6 = 7;
int buttonPin7 = 8;
int buttonPin8 = 9;

byte leds1 = 1;
byte leds2 = 1;
byte leds3 = 1;
byte leds4 = 1;
byte leds5 = 1;
byte leds6 = 1;
byte leds7 = 1;
byte leds8 = 1;

bool button_old1 = true;
bool button_old2 = true;
bool button_old3 = true;
bool button_old4 = true;
bool button_old5 = true;
bool button_old6 = true;
bool button_old7 = true;
bool button_old8 = true;

bool button_new1;
bool button_new2;
bool button_new3;
bool button_new4;
bool button_new5;
bool button_new6;
bool button_new7;
bool button_new8;

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds1);
   shiftOut(dataPin, clockPin, LSBFIRST, leds2);
   shiftOut(dataPin, clockPin, LSBFIRST, leds3);
   shiftOut(dataPin, clockPin, LSBFIRST, leds4);
   shiftOut(dataPin, clockPin, LSBFIRST, leds5);
   shiftOut(dataPin, clockPin, LSBFIRST, leds6);
   shiftOut(dataPin, clockPin, LSBFIRST, leds7);
   shiftOut(dataPin, clockPin, LSBFIRST, leds8);  
   digitalWrite(latchPin, HIGH);
}
void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin , OUTPUT);
  pinMode(clockPin, OUTPUT);
  
  pinMode(A , OUTPUT);
  pinMode(B , OUTPUT);
  pinMode(C , OUTPUT);
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  
  updateShiftRegister();
}

void loop() 
{ 
   button_new1 = digitalRead(buttonPin1);
   button_new2 = digitalRead(buttonPin2);
   button_new3 = digitalRead(buttonPin3);
   button_new4 = digitalRead(buttonPin4);
   button_new5 = digitalRead(buttonPin5);
   button_new6 = digitalRead(buttonPin6);
   button_new7 = digitalRead(buttonPin7);
   button_new8 = digitalRead(buttonPin8);
//button 1//
    if(button_new1 != button_old1)
    {       
      digitalWrite(A , LOW);
      digitalWrite(B , LOW);
      digitalWrite(C , LOW);
      if(button_new1 == false)
      {

        leds1 = leds1 << 1;
        if(leds1 == 0b00000000)
        {
          leds1 = 0b00000001;
        }
        updateShiftRegister();
     }
     button_old1 = button_new1;
    }
    delay(tDelay);
// button 2 //
    if(button_new2 != button_old2)
    {       
      digitalWrite(A , HIGH);
      digitalWrite(B , LOW);
      digitalWrite(C , LOW);
      if(button_new2 == false)
      {

        leds2 = leds2 << 1;
        if(leds2 == 0b00000000)
        {
          leds2 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old2 = button_new2;
    }
    delay(tDelay);

// button 4//
    if(button_new4 != button_old4)
    {       
      digitalWrite(A , HIGH);
      digitalWrite(B , HIGH);
      digitalWrite(C , LOW);
      if(button_new4 == false)
      {

        leds4 = leds4 << 1;
        if(leds4 == 0b00000000)
        {
          leds4 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old4 = button_new4;
    }
    delay(tDelay);
 // button 5//
    if(button_new5 != button_old5)
    {       
      digitalWrite(A , LOW);
      digitalWrite(B , LOW);
      digitalWrite(C , HIGH);
      if(button_new5 == false)
      {

        leds5 = leds5 << 1;
        if(leds5 == 0b00000000)
        {
          leds5 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old5 = button_new5;
    }
    delay(tDelay);   
 // button 6//
    if(button_new6 != button_old6)
    {       
      digitalWrite(A , HIGH);
      digitalWrite(B , LOW);
      digitalWrite(C , HIGH);
      if(button_new6 == false)
      {

        leds6 = leds6 << 1;
        if(leds6 == 0b00000000)
        {
          leds6 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old6 = button_new6;
    }
    delay(tDelay);  
 // button 7//
    if(button_new7 != button_old7)
    {       

      if(button_new7 == false)
      {
      digitalWrite(A , LOW);
      digitalWrite(B , HIGH);
      digitalWrite(C , HIGH);
        leds7 = leds7 << 1;
        if(leds7 == 0b00000000)
        {
          leds7 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old7 = button_new7;
    }
    delay(tDelay);    
 // button 8//
    if(button_new8 != button_old8)
    {         
      if(button_new8 == false)
      {
      digitalWrite(A , HIGH);
      digitalWrite(B , HIGH);
      digitalWrite(C , HIGH);

        leds8 = leds8 << 1;
        if(leds8 == 0b00000000)
        {
          leds8 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old8 = button_new8;
    }
    delay(tDelay);            
}

PikaOrgil:
[...] that bore no fruit:

Well, you have to be a bit more specific.

This too. As you can see i can’t really set the select inputs properly and shorten my code using a single line that can detect the button pressed. The result is that no leds are lighting up. Or should i say there’s no change in the whole circuit when i test it on proteus 8.7.

int tDelay = 500;
int latchPin = A1;
int clockPin = A0;
int dataPin = 13;

int A = 12;
int B = 11;
int C = 10;
     
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int buttonPin6 = 7;
int buttonPin7 = 8;
int buttonPin8 = 9;

byte leds1 = 1;
byte leds2 = 1;
byte leds3 = 1;
byte leds4 = 1;
byte leds5 = 1;
byte leds6 = 1;
byte leds7 = 1;
byte leds8 = 1;

bool button_old1 = true;
bool button_old2 = true;
bool button_old3 = true;
bool button_old4 = true;
bool button_old5 = true;
bool button_old6 = true;
bool button_old7 = true;
bool button_old8 = true;

bool button_new1;
bool button_new2;
bool button_new3;
bool button_new4;
bool button_new5;
bool button_new6;
bool button_new7;
bool button_new8;

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds1);
   shiftOut(dataPin, clockPin, LSBFIRST, leds2);
   shiftOut(dataPin, clockPin, LSBFIRST, leds3);
   shiftOut(dataPin, clockPin, LSBFIRST, leds4);
   shiftOut(dataPin, clockPin, LSBFIRST, leds5);
   shiftOut(dataPin, clockPin, LSBFIRST, leds6);
   shiftOut(dataPin, clockPin, LSBFIRST, leds7);
   shiftOut(dataPin, clockPin, LSBFIRST, leds8);  
   digitalWrite(latchPin, HIGH);
}
void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin , OUTPUT);
  pinMode(clockPin, OUTPUT);
  
  pinMode(A , OUTPUT);
  pinMode(B , OUTPUT);
  pinMode(C , OUTPUT);
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  
  updateShiftRegister();
}

void loop() 
{ 
   button_new1 = digitalRead(buttonPin1);
   button_new2 = digitalRead(buttonPin2);
   button_new3 = digitalRead(buttonPin3);
   button_new4 = digitalRead(buttonPin4);
   button_new5 = digitalRead(buttonPin5);
   button_new6 = digitalRead(buttonPin6);
   button_new7 = digitalRead(buttonPin7);
   button_new8 = digitalRead(buttonPin8);
//button 1//
    if(button_new1 != button_old1)
    {       
      digitalWrite(A , LOW);
      digitalWrite(B , LOW);
      digitalWrite(C , LOW);
      if(button_new1 == false)
      {

        leds1 = leds1 << 1;
        if(leds1 == 0b00000000)
        {
          leds1 = 0b00000001;
        }
        updateShiftRegister();
     }
     button_old1 = button_new1;
    }
    delay(tDelay);
// button 2 //
    if(button_new2 != button_old2)
    {       
      digitalWrite(A , HIGH);
      digitalWrite(B , LOW);
      digitalWrite(C , LOW);
      if(button_new2 == false)
      {

        leds2 = leds2 << 1;
        if(leds2 == 0b00000000)
        {
          leds2 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old2 = button_new2;
    }
    delay(tDelay);

// button 4//
    if(button_new4 != button_old4)
    {       
      digitalWrite(A , HIGH);
      digitalWrite(B , HIGH);
      digitalWrite(C , LOW);
      if(button_new4 == false)
      {

        leds4 = leds4 << 1;
        if(leds4 == 0b00000000)
        {
          leds4 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old4 = button_new4;
    }
    delay(tDelay);
 // button 5//
    if(button_new5 != button_old5)
    {       
      digitalWrite(A , LOW);
      digitalWrite(B , LOW);
      digitalWrite(C , HIGH);
      if(button_new5 == false)
      {

        leds5 = leds5 << 1;
        if(leds5 == 0b00000000)
        {
          leds5 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old5 = button_new5;
    }
    delay(tDelay);   
 // button 6//
    if(button_new6 != button_old6)
    {       
      digitalWrite(A , HIGH);
      digitalWrite(B , LOW);
      digitalWrite(C , HIGH);
      if(button_new6 == false)
      {

        leds6 = leds6 << 1;
        if(leds6 == 0b00000000)
        {
          leds6 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old6 = button_new6;
    }
    delay(tDelay);  
 // button 7//
    if(button_new7 != button_old7)
    {       

      if(button_new7 == false)
      {
      digitalWrite(A , LOW);
      digitalWrite(B , HIGH);
      digitalWrite(C , HIGH);
        leds7 = leds7 << 1;
        if(leds7 == 0b00000000)
        {
          leds7 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old7 = button_new7;
    }
    delay(tDelay);    
 // button 8//
    if(button_new8 != button_old8)
    {         
      if(button_new8 == false)
      {
      digitalWrite(A , HIGH);
      digitalWrite(B , HIGH);
      digitalWrite(C , HIGH);

        leds8 = leds8 << 1;
        if(leds8 == 0b00000000)
        {
          leds8 = 0b00000001;
        }
        updateShiftRegister();
      }
      button_old8 = button_new8;
    }
    delay(tDelay);            
}

Without worrying about switches, can you output anything to the demultiplexer(s)?

Have you ever heard of arrays? Your code is 8 times as long as it needs to be.

Or should i say there's no change in the whole circuit when i test it on proteus 8.7.

Screw proteus. What happens when you run the code with the real hardware?

Do you know how the 74LS138 works? You've tied E3 to GND, so whatever you apply to E1, E2, A0-3, the ic will always output high on all output pins.

Oh my apologies i connected E3 to ground rather than E1. ill try fixing it right away.

The correct way would be E2 to GND, E3 to VCC. Then use E1 as your data input for the line selected by A0-2.

Now i can see that the buttons are functioning fine thank you. But the leds are still not turning on so i’ll try changing my codes.

How do you know that the buttons are working, if the leds are not turning on?

Since im trying the circuit on proteus at the moment i can see the indicator that shows if there's voltage coming or not.

What happens after that? Do you see the signals being applied to the demux and shift registers? Or nothing at all?

The Select inputs are not working. I think the loop is stuck on the first button.The arduino isn’t sending any signals out.

Here’s the updated circuit.

Currently i'm trying to control a single phase shifter through demultiplexer.I'll update when i get the code done.

So i tried this and the results are : the select pin turns High,LOW,LOW from LOW,LOW,LOW but if press 1 again the enable doesn’t go back to LOW,LOW,LOW

int tDelay = 500;
int latchPin = A0;
int clockPin = A1;
int dataPin = 13;
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int buttonPin6 = 7;
int buttonPin7 = 8;
int buttonPin8 = 9;
int A = 12;
int B = 11;
int C = 10;
byte leds = 0;
bool button_old1 = true;
bool button_old2 = true;
bool button_new1;
bool button_new2;

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  updateShiftRegister();
}

void loop()
{
  button_new1 = digitalRead(buttonPin1);
  button_new2 = digitalRead(buttonPin2);
  if(button_new1 != button_old1) 
  {
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    if(button_new1 == false)
    {
      leds = leds << 1;
      if(leds == 0b00000000)
      {
        leds = 0b00000001;
      }
      updateShiftRegister();
    }
    button_old1 = button_new1;
 
  delay(tDelay);
  }
 
  if(button_new2 != button_old2)
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    if(button_new2 == false)
    {
      leds = leds << 1;
      if(leds == 0b00000000)
      {
        leds = 0b00000001;
      }
      updateShiftRegister();
    }
    button_old2 = button_new2;
 
  delay(tDelay);
  }
}

Ok so this code does control the select pins but the data is not being transmitted through. What should i do about that?

int tDelay = 500;
int latchPin = A0;
int clockPin = A1;
int dataPin = 13;
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int buttonPin6 = 7;
int buttonPin7 = 8;
int buttonPin8 = 9;
int A = 12;
int B = 11;
int C = 10;
byte leds1 = 0;
byte leds2 = 0;
byte leds3 = 0;
bool button_old1 = true;
bool button_old2 = true;
bool button_old3 = true;
bool button_new1;
bool button_new2;
bool button_new3;

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds1);
  shiftOut(dataPin, clockPin, LSBFIRST, leds2);
  shiftOut(dataPin, clockPin, LSBFIRST, leds3);
  digitalWrite(latchPin, HIGH);
}

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  updateShiftRegister();
}

void loop()
{
  button_new1 = digitalRead(buttonPin1);
  button_new2 = digitalRead(buttonPin2);
  button_new3 = digitalRead(buttonPin3);
  if(button_new1 != button_old1) 
  {
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    if(button_new1 == false)
    {
      leds1 = leds1 << 1;
      if(leds1 == 0b00000000)
      {
        leds1 = 0b00000001;
      }
      updateShiftRegister();
    }
    button_old1 = button_new1;
 
  delay(tDelay);
  }
 
  if(button_new2 != button_old2)
  {
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    if(button_new2 == false)
    {
      leds1 = leds1 << 1;
      if(leds1 == 0b00000000)
      {
        leds1 = 0b00000001;
      }
      updateShiftRegister();
    }
    button_old2 = button_new2;
 
  delay(tDelay);
  }
  
  if(button_new3 != button_old3)
  {
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    if(button_new3 == false)
    {
      leds3 = leds3 << 1;
      if(leds3 == 0b00000000)
      {
        leds3 = 0b00000001;
      }
      updateShiftRegister();
    }
    button_old3 = button_new3;
 
  delay(tDelay);
  }
}

PikaOrgil:
Currently i'm trying to control a single phase shifter through demultiplexer.

You are changing the phase of what? I thought you connected shift registers after the demux? A phase shifter is something different.

PikaOrgil:
Ok so this code does control the select pins but the data is not being transmitted through. What should i do about that?

Okay, you really need to be more specific. What do you mean by select pins? The address pins for the demux, A,B and C in your code? The data is not transmitted through what? Does your arduino output the signal? Does your demux receive the signal but doesn't forward it? Is your shift register receiving the signal but not forwarding it?

I'd suggest you do one simulation with the arduino, one button and one demux. Put a led to whatever demux output and try getting that to work. Or do it on a breadboard with the real components, as I don't know how reliable the simulator is.