Outputting bits on shift registers and reading bits on another

Hi,

The code below outputs 62 bits one by one using a shift register (74HC595) fine. I.e
...00000001 then...00000010 then ...00000100 etc. I've attached part of the schematic, it basically has 8 serial to parallel shift registers on the left and 8 parallel to serial shift registers on the right. The 5V LED's are just for indication.

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-32 one by one then stops at number 32.
#include <MultiShiftRegister.h>

int latchPin = 10;
int clockPin = 11;
int dataPin = 12;

MultiShiftRegister msr (8, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:
  pinMode (latchPin, OUTPUT);
  pinMode (clockPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
  msr.shift();

  for (int pin = 0; pin <= 63; pin += 1) //(pin +=1) = (pin = pin+1)
  {
    //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
    msr.set(pin); //Select LED to turn on
    msr.shift();  //Shift out on register
    delay(250);
    msr.clear_shift(pin); //Turn the selected LED off
    Serial.print (pin);
    delay(250);
  }


}
void loop() {


}

The following code reads the bits back using shift register (74HC165) and displays them fine in serial monitor. I'm just using a push button to simulate a logic '1' and it shows it in the serial monitor fine, in any bit position.

#include <ShiftIn.h>

// Init ShiftIn instance with one chip.
// The number in brackets defines the number of daisy-chained 74HC165 chips
// So if you are using two chips, you would write: ShiftIn<2> shift;
ShiftIn<8>shift;

void setup() {
  Serial.begin(9600);
  // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
  shift.begin(7, 4, 5, 6);
}

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++) {
    Serial.print( shift.state(i) ); // get state of button i
    Serial.print(','); // comma
  }
  Serial.println();
}

void loop() {
  if (shift.update()) // read in all values. returns true if any button has changed
    displayValues();
  delay(1000);
}

I now want to combine the two pieces of code so I can output 1 bit at a time and read back to the serial monitor one bit at a time. I've tried to combine it below but instead of reading the 62 bits as I would expect:
...00000001 then ...00000010 then ...00000100 etc, I get all zero's every time. I.e
...00000000
...00000000
...(62 times)

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-32 one by one then stops at number 32.
#include <MultiShiftRegister.h>
#include <ShiftIn.h>

// Init ShiftIn instance with one chip.
// The number in brackets defines the number of daisy-chained 74HC165 chips
// So if you are using two chips, you would write: ShiftIn<2> shift;
ShiftIn<8>shift;

int latchPin = 10;
int clockPin = 11;
int dataPin = 12;

MultiShiftRegister msr (8, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:

  // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
  shift.begin(7, 4, 5, 6);//setup hc165 pins
  
  pinMode (latchPin, OUTPUT);//setup hc595 pins
  pinMode (clockPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
  msr.shift();

  for (int pin = 0; pin <= 64; pin += 1) //(pin +=1) = (pin = pin+1)
  {
    //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
    msr.set(pin); //Select LED to turn on
    msr.shift();  //Shift out on register

    displayValues();
    delay(250);
    msr.clear_shift(pin); //Turn the selected LED off
 //   Serial.print (pin);
    delay(250);
  }


}

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++) {
    Serial.print( shift.state(i) ); // get state of button i
    Serial.print(','); // comma
  }
  Serial.println();
}
void loop() {
 if (shift.update()) // read in all values. returns true if any button has changed
    displayValues();
  delay(1000);

}

Any tips would be appreciated! Thanks

What are the LEDs you are using? There are no '5V LEDs'. LEDs need series resistors.

You can't combine the code as it stands because of the delays. You need to learn to use millis for timing and learn how to do more than one thing at a time.

Study these:
Using millis for timing
Demonstration for several things at the same time
Then think about how they apply to what you are doing.

The 5V LED's have integral resistors, no need for external current limiting.

Both the pieces of code work independently which is why I can't understand why it's not working when it's combined. The hardware is also unchanged. The delays are not causing any issues, they are just so I can see the LED's changing.

The delays are not causing any issues, they are just so I can see the LED's changing.

My advice is my advice, you are not compelled to take it.

Someone else will be along with different advice...

balgill021:
The delays are not causing any issues, they are just so I can see the LED's changing.

Have you tried it without the LEDs? Maybe pull them so it's just circuit to circuit and observe the input data in the serial monitor. Stick a meter on the 165 inputs just to make sure you're getting valid logic levels.

YMMV

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.