Shifting bits through differente shift registers

Hello there. Im working on a project where im using 74hc165 shif registers to command outputs (LEDs) with the use of 74hc595 shift registers.
I managed to cascade a pair of both shift registers and they work fine, but in my application Ill have to shift some bits through shift registers.
In this case I have button 1 through 12 and I have LEDS 1 through 12.
The way im doing it right now is by sending two bytes of data, one byte for the first 8 buttons/leds and the second byte for the next 4 buttons/leds.
Since the ShiftIn and ShiftOut commands allow me to shift just one byte of data per shif register,
This is what I want to do : given an instruction (for example if this or that button is pushed) shift my 16 bits n number of positions to the right. The bit shifting part is not a problem but the fact that the last bit of the first shift register would not shift right TO THE NEXT SHIFT REGISTER. And then when i start pressing the buttons of the second shift register they would be shifted n times to the right as if they were totally indepent.
I have no idea how to fix this, Ill be working with as many as 8 shift registers and I need to be able to, when having for example this button pushed ill start lighting my leds from the 4th position and on or if i push that other button ill start commanding my ouputs from the 10th position of my leds and on.

Im communicating 2 Arduinos by radio modules, so this is the code of the transmitter.

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

// Define Connections to 74HC165

// PL pin 1
int load = 7;
// CE pin 15
int clockEnablePin = 4;
// Q7 pin 9
int dataIn = 5;
// CP pin 2
int clockIn = 6;
//buttonpin
const int buttonpin = 44;
int buttonstate=0;

//radio settings
RF24 radio(8, 9); // CE, CSN
const byte address[][6] = {"pipe1"};



void setup() {
  // Setup Serial Monitor
  Serial.begin(9600);

  // 74HC165 pins
  pinMode(load, OUTPUT);
  pinMode(clockEnablePin, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);
  pinMode(buttonpin, INPUT);
  
  // partie radio
  radio.begin();          // Initialize the nRF24L01 Radio
  radio.setChannel(108);  // Above most WiFi frequencies
  radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
  radio.setPALevel(RF24_PA_LOW);//  radio.setPALevel(RF24_PA_MAX);
  radio.openWritingPipe(address); 
  radio.stopListening();


}

void loop() {
  // Read Switches

  // 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);
  byte incoming2= shiftIn(dataIn, clockIn, LSBFIRST);
  digitalWrite(clockEnablePin, HIGH);

  // Print to serial monitor
  Serial.print("Pin States(1-8):\r\n");
  Serial.println(incoming, BIN);
  Serial.print("Pin States(9-12):\r\n");
  Serial.println(incoming2, BIN);


 //bit shift
  int test= int (incoming);
  test= test/2;
  incoming=byte (test);

  int test2= int (incoming2);
  test2= test2/2;
  incoming2=byte (test2);
  
  
  
  //send via radio
  radio.write(&incoming, sizeof(incoming));
  radio.write(&incoming2, sizeof(incoming2));
  delay(150);
  
}

And this is the code of the receiving Arduino.

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

// Define Connections to 74HC595
// ST_CP pin 12
const int latchPin = 10;
// SH_CP pin 11
const int clockPin = 11;
// DS pin 14
const int dataPin = 12;

// radio settings
RF24 radio(7, 8); // CE, CSN
const byte address[][6] = {"pipe1"};

void setup() {

  Serial.begin(9600);
  
  // 74HC595 pins
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

    // partie radio
  radio.begin();          // Initialize the nRF24L01 Radio
  radio.setChannel(108);  // Above most WiFi frequencies
  radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
  radio.setPALevel(RF24_PA_LOW);//  radio.setPALevel(RF24_PA_MAX);
  radio.openReadingPipe(1,address); 
  radio.startListening();


}

void loop() {
   //receive byte
  delay(5);
   if ( radio.available()) {
    byte incoming=0;
    byte incoming2=0;
    while (radio.available()) {    
      radio.read(&incoming, sizeof(incoming));
      radio.read(&incoming2, sizeof(incoming2));
      Serial.print("Pin States(1-8):\r\n");
      Serial.println(incoming, BIN);
      Serial.print("Pin States(9-12):\r\n");
      Serial.println(incoming2, BIN);
    }
   
   // Write to LEDs
  // ST_CP LOW to keep LEDs from changing while reading serial data
  digitalWrite(latchPin, LOW);

  // Shift out the bits
  shiftOut(dataPin, clockPin, LSBFIRST, incoming2);
  shiftOut(dataPin, clockPin, LSBFIRST, incoming);
  

  // ST_CP HIGH change LEDs
  digitalWrite(latchPin, HIGH);

  delay(150);


   }
   }

I would really appreciate if you have any tips, ideas or information regarding all this.

I would really appreciate if you read the forum rules about posting code as well as other things here How to use this forum

I managed to cascade a pair of both shift registers and they work fine,

How, can you post a schematic of this.

Note that the 74HC165 is a parallel in serial out shift register and the 74hc595 is a serial in parallel out shift register. It is very very unusual to use these two together.

Thank you for the Forum Instructions.
Here is how I have both 74hc595 and 74hc165 cascaded.

Thanks, I can see now you are talking about two of each type. I still can't check if you have done this correctly because all you have is a physical layout of the chips and very few of the pins are labeled.

Also hosting pictures on an external site is not the preferred method of displaying an image, we like them attached.

Anyway what now exactly is your question?
You say:-

And then when i start pressing the buttons of the second shift register they would be shifted n times to the right as if they were totally indepent.

Where are these buttons on you circuit?
Where is the Arduino on your circuit?

Thank you for the Forum Instructions.

Now you have read them you should go back and modify your first post, like it said. Underneath your post will be a downward pointing arrow, move your mouse to it and select modify. Then select the code and click on the </> icon in the top left corner and then click save.

Note when posting code we need to see it all, not just the bit you think is relevant because often there are mistakes in the bits we can't see.

Grumpy_Mike:
Also hosting pictures on an external site is not the preferred method of displaying an image, we like them attached.

Actually it is a preferred method of displaying an image, but only if the site does not obfuscate the image (such as Google Drive) and is durable, that is, has a life expectancy of at least ten years or so. :grinning:

If you attach it to a posting here - and do not then play around by removing the attachment or uploading another version - which then enables it to be linked to view inline, we reasonably expect that it will be retained as long as the forum is maintained.

Here, I hope its more clear, if you still have doubts let me know.
Already updated my first post too.

Basically my problem is that when bit shifting the last bits of my first 74hc595 shift register they are erased out of existence.
What I hope to get with this is that the last bit of the first shift register becomes the first bit of the next shift register while bit shifting.

I made a mistake in the schema, pin 10 of the first 74hc165 register goes to the pin 9 of the second one.

Basically my problem is that when bit shifting the last bits of my first 74hc595 shift register they are erased out of existence.

You have a complex setup so first it is advisable to make sure each of the shift registers on its own works before joining them together.
From what I could see the 74hc595 looks like it should work and bits from one will be transferred to the other. So forget the radio and write a sketch that tests just that.

Same with the 74HC165. You don't show the wiring of the switches here but I am assuming they are wired between input and ground with an external pull up resistor.

However on the 74HC165 you seem to have the serial outputs ( pin 9 ) to both chips wired together which is bad and might break one chip or other. The serial output of one chip must be connected to the serial input of the other.
So just tie Q7 (pin 9) of the first chip to the serial data input (pin 10) of the next chip.

[

Expand<](Shifting bits through differente shift registers - LEDs and Multiplexing - Arduino Forum)