Problem with ShiftOut while also using ShiftIn

Hey, I am fairly new at programming and electronics although I have had some courses on C and electrical circuits from my degree (mechanical engineer), but its still difficult for me to troubleshoot my stuff alone. Long story short I am having some trouble with expanding the number of ins and outs on my arduino.

So to the problem.

I wanted to take the data that I am getting from my ShiftIn and send it to an array of LEDs using ShiftOut. Both the shiftin and shiftout seem to work independently. Working off of the examples here on the arduino website I can get my current set up for my shiftout to count up to 256 on the LEDS and I can also display the proper button presses using the serial window. The problem is when I try to combine these actions I can only get my LEDs to respond to the press of the "1" digit of the shiftin. When I do this, all 8 LEDs light up.

Most of my current code is taken straight from the examples of shiftout and shiftin on the site here.

Here is my code for Setup and Loop, the ShiftIn() is taken directly from the example code:

void setup() {
  //start serial
  Serial.begin(9600);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
  
  pinMode(latchPinOut, OUTPUT);
  pinMode(clockPinOut, OUTPUT);
  pinMode(dataPinOut, OUTPUT);
}

void loop() {

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

  //while the shift register is in serial mode
  //collect each shift register into a byte
  //the register attached to the chip comes in first 
  switchVar1 = shiftIn(dataPin, clockPin);
  
//This is the data going out
  digitalWrite(latchPinOut, LOW);
  
  shiftOut(dataPinOut, clockPinOut, MSBFIRST, switchVar1);
  
  digitalWrite(latchPinOut, HIGH);

  //Print out the results.
  //leading 0's at the top of the byte 
  //(7, 6, 5, etc) will be dropped before 
  //the first pin that has a high input
  //reading  
  Serial.println(switchVar1,BIN);
 

//white space
Serial.println("-------------------");
//delay so all these print satements can keep up. 
delay(500);
  
}
  digitalWrite(latchPinOut, LOW);
  
  shiftOut(dataPinOut, clockPinOut, MSBFIRST, switchVar1);
  
  digitalWrite(latchPinOut, HIGH);

That's not strictly how best to use a shift register for output. All the time the latch is HIGH the serial input is passed through to the parallel output. It is better by far to use:

  shiftOut(dataPinOut, clockPinOut, MSBFIRST, switchVar1);  
  digitalWrite(latchPinOut, HIGH);
  digitalWrite(latchPinOut, LOW);

so that the serial data gets passed through to the parallel output only the once. If you happen to be sharing clock pins (which I can't tell from your code as it's missing some vital bits at the top) then you will be shifting who knows what through the parallel output during all the rest of your program.

what are you trying to shift in and out, to ?

I am doing a similar project to you . I have two 4021's as inputs and 2 595's as outputs.
I seem to remember I had similar problems until I added the lines

   delayMicroseconds  (20);
   digitalWrite       (clockPinin,HIGH);/*puts the clockpin high so the first
                                bit is read before the register is clocked on*/

as the first two lines in the void loop(). I think it has something to do with getting the clock pulse set before you start. try it.
My shift registers work now.
I also do the same as majenko with a

delay (20);

between the digitalWrite lines

What are you using for your input shift register? I'm going to assume 74HC165 since it's pretty basic.

  //Pulse the latch pin:
  //set it to 1 to collect parallel data
  digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);

You might want to try swapping your HIGH and LOW here. Parallel Load mode is LOW, Serial Shift is HIGH. With the latch pin in Parallel Load mode while your are transmitting, all your data will be determined by the first bit.

Thank you all for your feedback!

Unfortunately my problem was not related to anything I suggested or fixed by what you guys responded with. I kept tinkering with the program and found that if I commented out the serial lines I was able to get things to act differently... Turns out... those Tx Rx lines on the arduino are sorta needed for the serial functions. I moved my shifting out up a few pins and now everything works! I was unaware that this could happen, lesson learned.

Thanks again for the replys!

Turns out... those Tx Rx lines on the arduino are sorta needed for the serial functions.

What a surprise.

... if you'd shown your ENTIRE code like you're supposed to we'd have immediately spotted that and corrected your mistake in moments.

That just goes to show the importance of posting ALL your code, not just the bits YOU think are causing the problem!

Consider that a lesson for the future.