74HC165N test program

I am using the 74hc165n one register with a Uno R3 and trying to make one led blink the number of times the pressed .
(ie if button 1 pressed then blink once, if button 2 pressed then blink twice etc).
Could someone assist me with this.

/*
  74HC165 Shift Register input  5v
  This is for testing 8 buttons
*/
//------------------------------------------------------------------
//------------------------------------------------------------------
// Define Connections to 74HC165

// register PL pin 1
int load = 12;

// register CP pin 2
int clockIn = 11;

// register Q7 pin 7
int dataIn = 9;

// register CE pin 15
int clockEnablePin = 8;

// How many shift register chips are daisy-chained.
#define number_of_Registers   1

// Width of data (how many ext lines)
#define dataWidth   number_of_Registers * 8
//------------------------------------------------------------------


//------------------------------------------------------------------
//------------------------SETUP-------------------------------------
void setup()
{

  // Setup Serial Monitor
  Serial.begin(9600);

  // Setup 74HC165 connections
  pinMode(load, OUTPUT);
  pinMode(clockEnablePin, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);

  // Setup LED
  // temp remove when finished.
  // blinks when btn pressed btn1=1 blin. btn2=2 blink etc.
  pinMode(2, OUTPUT);

}
//------------------------------------------------------------------
//------------------------LOOP--------------------------------------
void loop()
{
  getData();

}
//------------------------------------------------------------------
//-----------------------165 ROUTINES-------------------------------

void getData()
{
  // Write pulse to load pin
  digitalWrite(load, LOW);
  delayMicroseconds(5);
  digitalWrite(load, HIGH);
  delayMicroseconds(5);

  // Get data from 74HC165
  digitalWrite(clockIn, HIGH);
  digitalWrite(clockEnablePin, LOW);
  byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
  digitalWrite(clockEnablePin, HIGH);

  // print2Serial(incoming);
  showLED(incoming);
}

void print2Serial(byte incoming)// show the butto state
{

  // Print to serial monitor
  Serial.print("Pin States:\r\n");

  for (int i = 0; i < dataWidth; i++)
  {
    Serial.print("  Pin-");
    Serial.print(i);
    Serial.print(": ");

    if ((incoming >> i) & 1)
    {
      Serial.print("LOW");
      Serial.print("\r\n");
    }
    else
    {
      Serial.print("HIGH");
      Serial.print("\r\n");
      
      showLED(incoming);
    }
  }
}
void showLED(byte incoming)// blink the led 
{
  for (int i = 0; i < dataWidth; i++)
  {
    if ((incoming >> i) & 1)
    {
      digitalWrite(2, HIGH); // turn the LED on
      delay(1000); // wait for a second

      digitalWrite(2, LOW); // turn the LED off
      delay(1000); // wait for a second
    }
  }
}
  //------------------------------------------------------------------
  //------------------------------------------------------------------

Looks plausible, does it not work?

What does it do or not do that it shouldn't or should?

a7

When button 0 is pressed the led should blink once. When button1 is pressed the led should blink twice and so on. That is not happening I do not know how to code for it.

What does it do?

You've said what you wanted it to do, that isn't what you've coded.

If you press two buttons, does it blink twice, three buttons three times? And so forth?

a7

OK, as I read it, shiftIn will not work directly with the 74165 shift register, as it clocks before reading a bit, where what you need is to read a bit, then clock to the next one.

You will have to be happy, then, with either a 7 bit shift register, in effect, or use the SPI pins and fix it by using the correct clock polarity. I think.

This replacement code for shiftIn

  byte incoming = 0;

  for (int i = 0; i < dataWidth; i++) {
    incoming <<= 1;
    int bit = digitalRead(dataIn);
    if (bit == HIGH) {
      incoming |= 1;
    } 
    digitalWrite(clockIn, HIGH); // Shift out the next bit
    digitalWrite(clockIn, LOW);
  }

works perfectly.

You code now blinks for each button pressed. 2 pressed, 2 blinks &c. Are you sure this isn't what the goal is?

To blink by which single button of 8 buttons is pressed

shift incoming while it is non-zero and blink each time until the variable is zero (the '1' has shifted off)

a7

Thank you I will give this a try - the purpose of the sketch is to test the wiring of the breadboard and using only one LED show which button has been pressed by blinking the led a certain number of times depending upon the button pressed.
I hope I am explaining this correctly.

Yes, I see. TBH I got a little blind counting blinks. As an exercise in programming it is fairly easy, but as a useful tool for testing anything IRL it gets old. Fast.

In my “blibk out the button number” code I had to speed up the blinks, and actually count them and print the number before I went tots insane waiting and counting… are you doing this because you have some objection to arguably better ways to check whatever you checking by simply using print statements?

a7

Just a little different way that's all. I just want to know which button was pressed.
I am open to suggestions.

Replace the showLED(incoming); with Serial.print(incoming, BIN); to see if you are getting any action from your buttons.

Do your register's input pins have pull-up or pull-down resistors? Are your buttons connected to Ground for pull-up resistors or +5V for pull-down resistors?