Go Down

Topic: Shift transfer (Read 741 times) previous topic - next topic


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.

Code: [Select]

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

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

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);   


  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) {
  do {
    u8g2.drawStr(0,12,"Parallel to Serial");
    if (analogRead(SER) > 500)
          else u8g2.drawGlyph(88,36,56);
    u8g2.drawStr(0,63,"Out : ");
  } while ( u8g2.nextPage() );
  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 {
          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 {
              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?

Code: [Select]

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


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. 


Nov 09, 2018, 08:52 pm Last Edit: Nov 10, 2018, 08:23 am by PaulRB
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.


Nov 10, 2018, 04:31 am Last Edit: Nov 10, 2018, 04:32 am by Paul__B
I am still convinced that multiple Arduino is not the best or easiest solution.

OK, again the XY problem!  :smiley-roll:

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

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.

Go Up