Shift Register Compare variables upon State change

Alright guys Im using 74hc165 parallel in serial out register. Hooking 6 buttons up to it and capturing serial into a variable.
Now Im having a hard time figuring out which way to go to tell when i have a change in one of the buttons.
I have read over and over Arduino reference and anything Google pops up but just cant figure out most efficient way to check for a button state change and act on that.

here is the code i have and im not very far into it.

int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
int buttonstate = 0;

void setup()
{
  pinMode(dataPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600); 
}

void loop()
  {
  btnstate = shiftIn(dataPin, clockPin, MSBFIRST);
  }

The 'copy for forum' feature in the IDE is a total waste of space and converts your code to garbage. Just copy the code and paste it in between a pair of code tags.

What is the Serial.read for?

to be honest now that i look at it i dont think i need it. I think i started out with that and just didnt remove.

How are your buttons wired to the 165? What state are the buttons in when they are open?

Corrected OP. Using 595n for input shift register.
Using a pull up resistor button setup and buttons will read high when pushed.

74HC595 is serial in parallel out. Not suited for button inputs. The 74165 is actually better. When using pullup resistor the switch should be wired to digital input and ground. The input will be LOW when the button pushed and high while open. What do the buttons do? To see if the buttons have changed save the old state before reading and compare after reading.

Thank you for the clarification. I will try and check serial for what I'm reading over it by end of day, and wire in 165.
I believe it is supposed to be a byte of 0 and 1s depending on if buttons are high or low. So I'm assuming that the way I wrote the program that btstate variable will be something like 00101001.

Right. If you wire the switches like I described in the earlier post a closed switch (pushed) will be a 0 in bstate. To use the 165 you would insert a statement before the shiftIn() function to pulse the shift/load input of the 165 LOW (digitalWrite( latchPin,LOW); ) to latch the switch states and then set shift/load HIGH (digitalWrite(latchPin, HIGH):wink: and call then shiftIn().

So will I only see a 0 or a series of 1 and 0's in my variable vtstate.
Also with my shiftin line will that not set the variable?

Since the 165 inputs with open (unpushed) buttons are pulled high by the pullup resistors those will be HIGH. Any input with a pushed (closed) button will be LOW. So if the first and third buttons are pushed the bstate variable will be 11111010. The bits G and H should be tied HIGH as they are unused (don't leave them floating).

Awesome thanks for info on tying the unused pins high.
Now what is the best way to sense a change in button states and act on it? Especially since I have to latch pin low and get readings ever cycle.

If you start out with all buttons open the reading would be 11111111. So declare a variable, say oldState, and, in setup() give it a value of B11111111 or 0xff or 255 (they all mean the same thing to the Arduino). Then when you read the 165 compare oldState to bState to see which buttons changed. Then save bState to oldState before looping back to reading the 165.

Alright new code, did not do much but want to make sure headed in right direction. Ive got to add more code later to talk to another arduino over SPI and control another Shift register serial in parallel out. I basically was having writers block and if this works(no arduino setup with me) i cant believe that i could not sort this out for myself. Appreciate all your help and hope for further help possibly later on.

int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
int btnstate = 0;
int oldstate = 0;
void setup()
{
  pinMode(dataPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  oldstate = B11111111;
  Serial.begin(9600); 
}

void loop()
  {
  btnstate = shiftIn(dataPin, clockPin, MSBFIRST);
  if (btnstate != oldstate)
    {
      btnstate = oldstate;
    }
  
  
}

btnstate = oldstate;

I think that this is backwards.
like this?

void loop()
{
  btnstate = shiftIn(dataPin, clockPin, MSBFIRST);
  if (btnstate != oldstate)
  {
    // buttons have changed do some stuff
    oldState = btnState; \\record present buttons so I can check for change next time they are read in loop()
    }
    // buttons haven't changed,  Get another reading
  }

I made a schematic of how I would connect the switches. I include the 0.1uf cap across each switch for hardware debouncing.

digital switch.jpg