Shift transfer

I am trying to read in 8 inputs. Then I will display the status on a LCD. I am going to have 5 nanos hooked to each other. Each one will feed into the previous one, so that I can read in the states of all 40 inputs. The computer will read one bit at at time by pulsing a clock signal. I currently have two hooked up. The one not directly connected to the computer works properly. However the data gets shifted on the closest one to the computer. An extra zero gets added, at the end of each byte.

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif



U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);


int buf[9];
int Load = A0;
int Clock = A1;
int SER = A2;
int Q7 = A3;

void setup(void) {

  /* U8g2 Project: SSD1306 Test Board */
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(10, 0);
  digitalWrite(9, 0);   


  u8g2.begin();  

  pinMode (5, INPUT);
  pinMode (6, INPUT);
  pinMode (7, INPUT);
  pinMode (8, INPUT);
  pinMode (9, INPUT);
  pinMode (10, INPUT);
  pinMode (11, INPUT);
  pinMode (12, INPUT);

  pinMode (A0,INPUT);
  pinMode (A1,INPUT);
  pinMode (A2,INPUT);
  pinMode (A3,OUTPUT);

}


void loop(void) {
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB10_tr);
    u8g2.drawStr(0,12,"Parallel to Serial");
    u8g2.drawGlyph(0,24,-(48)*buf[0]+48);
    u8g2.drawGlyph(11,24,-(49)*buf[1]+49);
    u8g2.drawGlyph(22,24,-(50)*buf[2]+50);
    u8g2.drawGlyph(33,24,-(51)*buf[3]+51);
    u8g2.drawGlyph(44,24,-(52)*buf[4]+52);
    u8g2.drawGlyph(55,24,-(53)*buf[5]+53);
    u8g2.drawGlyph(66,24,-(54)*buf[6]+54);
    u8g2.drawGlyph(77,24,-(55)*buf[7]+55);
    u8g2.drawGlyph(88,24,-(56)*buf[8]+56);
    
    u8g2.drawGlyph(0,36,-(48)*digitalRead(5)+48);
    u8g2.drawGlyph(11,36,-(49)*digitalRead(6)+49);
    u8g2.drawGlyph(22,36,-(50)*digitalRead(7)+50);
    u8g2.drawGlyph(33,36,-(51)*digitalRead(8)+51);
    u8g2.drawGlyph(44,36,-(52)*digitalRead(9)+52);
    u8g2.drawGlyph(55,36,-(53)*digitalRead(10)+53);
    u8g2.drawGlyph(66,36,-(54)*digitalRead(11)+54);
    u8g2.drawGlyph(77,36,-(55)*digitalRead(12)+55);
    if (analogRead(SER) > 500)
          u8g2.drawGlyph(88,36,0);
          else u8g2.drawGlyph(88,36,56);
    
    u8g2.drawStr(0,63,"Out : ");
    
  } while ( u8g2.nextPage() );
  //delay(1000);
  if (analogRead(Load)<400){
      buf[0] = digitalRead(5);
      buf[1] = digitalRead(6);
      buf[2] = digitalRead(7);
      buf[3] = digitalRead(8);
      buf[4] = digitalRead(9);
      buf[5] = digitalRead(10);
      buf[6] = digitalRead(11);
      buf[7] = digitalRead(12);
      if (analogRead(SER) > 500)
          buf[8] = 1;
          else buf[8] = 0;
      do {
          delay(1);}
          while ((analogRead(Load)<400)or(analogRead(Clock)>500));
      }

        if (analogRead(Clock)>500){
            analogWrite(Q7, buf[0]*680);
            buf[0] = buf[1];
            buf[1] = buf[2];
            buf[2] = buf[3];
            buf[3] = buf[4];
            buf[4] = buf[5];
            buf[5] = buf[6];
            buf[6] = buf[7];
            buf[7] = buf[8];
            if (analogRead(SER) > 500)
                buf[8] = 1;
                else buf[8] = 0;            
            do {
              delay(1);}
              while (analogRead(Clock)>500);
          
        }
}

Why not just have 1 Nano, and 5 daisy-chained shift-in shift registers, like 74HC165?

Create a clock pulse to capture all 40 input, then shift in the 5 bytes using SPI.transfer?

digitalWrite (loadPin, LOW);
digitalWrite (loadPin, HIGH);
for (x=0; x<5; x=x+1){
dataArray[x] = SPI.transfer(0);
}

Your 5 bytes are then in dataArray[] to do whatever you are doing with it.

What are these 40 inputs? Pushbuttons? If so, one Nano can easily read them all.

Two or more Arduinos in the same circuit is almost certainly a bad idea and a mistake often made by beginners.

I am using the nano, instead of the 74165, so I can have the arduino control a small animatronic. I am making a set of moving figures for a holiday display. The LCD panel will eventually be part of the face. However I want it to be easily expanded. I will have a sensor to detect someone coming close to the figure.

Ok, well, you have told us a few more little tantalising details, but not enough to properly advise you how you should design the circuit, except to say that I am still convinced that multiple Arduino is not the best or easiest solution.

PaulRB:
I am still convinced that multiple Arduino is not the best or easiest solution.

Exactly.

OK, again the XY problem! :roll_eyes:

Stop mucking us about - "us" being those who are trying to help you here. :astonished:

Clearly you are thoroughly muddled about what you are doing. Start by laying out your requirements, and if you do want assistance, lay those requirements out here. What display, what input devices, what servos? How many to start with and how many at most? What connections to other systems?

There are readily available (I2C) modules to control multiple servos, read inputs and control displays. These are very straightforward to use and reduce wiring complexity. Using multiple microcontrollers means you consume inordinate program space and execution processing managing the communication protocols and as it appears, have difficulty even coding it.