Shift Register with 40 buttons!

I started an ambitious project but I am very new to coding :stuck_out_tongue:

In the attachment is the project model that I have soldered and hooked it up to the board.
I have 40 momentary buttons with 8 different LEDs (plus piezo buzzer).

I have a problem connecting the shift register with the buttons:

  • 8 LEDs all light up at the beginning
  • pressing a "wrong" button will turn off one LED
  • pressing a "right" button will turn on the one LED

I managed to get some help to write some button codes.
I used the Arduino example coding for shift register (I tried to use as a reference but it ended up copy n paste).
However, I am having hard time linking the shift register codes with the button codes to work together, not separately.

p.s

The codes are not complete, especially with the shift register.

const int row[] = {
  2,3,4,5,6};

const int col[] = {
  7,8,9,10,11,12,A0,A1};

int datapin=A4;
int clockpin=A3;
int latchpin=A2;

byte data=0;

int state;
int buttonNumber;
int rightNumber=(2,3,5,7,11,13,17,19,23,29,31,37);

void setup() {
  
  pinMode(datapin,OUTPUT);
  pinMode(clockpin,OUTPUT);
  pinMode(latchpin,OUTPUT);
  
  Serial.begin(9600);
  pinMode (7,INPUT_PULLUP);
  pinMode (8,INPUT_PULLUP);
  pinMode (9,INPUT_PULLUP);
  pinMode (10,INPUT_PULLUP);
  pinMode (11,INPUT_PULLUP);
  pinMode (12,INPUT_PULLUP);
  pinMode (A0,INPUT_PULLUP);
  pinMode (A1,INPUT_PULLUP);
  for(int i=2;i<7;i++){
    pinMode(i, OUTPUT);
  }
}


void loop() {
  
  
  oneAfterAnother();
  
  
  for(int i=0; i<5; i++){
    digitalWrite(row[i], HIGH);
    for(int j=0; j<8; j++){
      state=digitalRead( col[j]);
      if(state == 0){
        
        buttonNumber=((i*8)+j);    //this should give you a number for the button pushed
                                  //a button number should make it easier to compare the button pushed to a list of "right" buttons
                                  //each row contains 8, so row * 8 works to get a value for the current column (column zero is the first one)
                                  //then j would be the current button in the column                                                    
        Serial.println(buttonNumber);
        
        Serial.print ("row ");

        Serial.print (i);
        Serial.print ("column ");

        Serial.print (j);

        Serial.print (" ");

        Serial.println (state);
        delay(1000);
        
      
      }

    }
    digitalWrite(row[i], LOW);
  }
}

void shiftWrite(int desiredPin, boolean desiredState){
  bitWrite(data,desiredPin,desiredState);
  shiftOut(datapin,clockpin,MSBFIRST,data);
  digitalWrite(latchpin,HIGH);
  digitalWrite(latchpin,LOW);
}

void oneAfterAnother(){
  int index;
  if (state==HIGH){
    digitalWrite(desiredPin,HIGH);
  }
  else (state==LOW){
    digitalWrite(desiredPin,LOW);
  }

Thank You!

_40buttons_shiftertest.ino (1.98 KB)

You can not connect a button and an LED to the same type of shift register.

Post a schematic of what you have if you want more help.

Grumpy_Mike:
You can not connect a button and an LED to the same type of shift register.

Oh, I wouldn't bet on that!

You can use the shift register outputs to poll the buttons. You would need diodes on the buttons. And of course, a counter is a useful alternative to poll buttons.