Need help creating value for shift register

Glad you got it working. Do you understand what was going wrong?

You had all the correct data written to bitsToSend using all those statements at the start of the loop. When you were calling

bitWrite(bitsToSend, whichPin, whichState);

you were writing over one of those values.

You had declared the variables whichPin and whichState, but you never initialised them (set them to a particular value). They probably automatically initialise to 0, but you cannot be sure of that.

So when you called

bitWrite(bitsToSend, whichPin, whichState);

you were overwriting the data for pin 0, as this would essentially be

bitWrite(bitsToSend, 0, 0);

therefore LED 0 will never light up as it is being set to zero every time.

Thank you Matallor, a very good explanation for which I am very grateful. It makes perfect sense when I look at the code now, but as a beginner there is so many possible sources of malfunction it is hard to spot them.

I rewired and added another 74HC595 in daisy chain to see what happened, but it was not what I had hoped. I was hoping the first on would accept the first 8 bits and then just bypass the rest, but instead it copies the results on the first to the second with a small delay.

// Declaring input pins

byte inPin0 = A0;   // pushbutton connected to analog pin A0
byte inPin1 = A1;   // pushbutton connected to analog pin A1
byte inPin2 = A2;   // pushbutton connected to analog pin A2
byte inPin3 = A3;   // pushbutton connected to analog pin A3
byte inPin4 = A4;   // pushbutton connected to analog pin A4
byte inPin5 = A5;   // pushbutton connected to analog pin A5
byte inPin6 = 5;    // pushbutton connected to digital pin 5
byte inPin7 = 6;    // pushbutton connected to digital pin 6
byte inPin8 = 7;    // pushbutton connected to digital pin 7
byte inPin9 = 8;    // pushbutton connected to digital pin 8
byte inPin10 = 9;    // pushbutton connected to digital pin 9
byte inPin11 = 10;    // pushbutton connected to digital pin 10
byte inPin12 = 11;    // pushbutton connected to digital pin 11
byte inPin13 = 12;    // pushbutton connected to digital pin 12
byte inPin14 = 13;    // pushbutton connected to digital pin 13




// Declaring output pins

int latchPin = 3;   //Pin connected to ST_CP(pin 12) of 74HC595
int clockPin = 2;   //Pin connected to SH_CP(pin 11) of 74HC595
int dataPin = 4;    //Pin connected to DS(pin 14) of 74HC595

// Declaring the bitsToSend variable which specifies the bit 
// correlating to the specific output pin on the 74HC595

byte bitsToSend;

void setup() {
  
  // Declare the chosen input pins to act as inputs
  
  pinMode(inPin0, INPUT);        // sets the analog pin A0 as input
  pinMode(inPin1, INPUT);        // sets the analog pin A1 as input
  pinMode(inPin2, INPUT);        // sets the analog pin A2 as input
  pinMode(inPin3, INPUT);        // sets the analog pin A3 as input
  pinMode(inPin4, INPUT);        // sets the analog pin A4 as input
  pinMode(inPin5, INPUT);        // sets the analog pin A5 as input
  pinMode(inPin6, INPUT);        // sets the digital pin 5 as input
  pinMode(inPin7, INPUT);        // sets the digital pin 6 as input
  pinMode(inPin8, INPUT);        // sets the digital pin 7 as input
  pinMode(inPin9, INPUT);        // sets the digital pin 8 as input
  pinMode(inPin10, INPUT);       // sets the digital pin 9 as input
  pinMode(inPin11, INPUT);       // sets the digital pin 10 as input
  pinMode(inPin12, INPUT);       // sets the digital pin 11 as input
  pinMode(inPin13, INPUT);       // sets the digital pin 12 as input
  pinMode(inPin14, INPUT);       // sets the digital pin 13 as input

  // Declare Arduino board output pins to 74HC595 input pins:
  
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  
  // Read the inputs and collects the values to be shifted out
  // Correlates the input pins to the bits to be sent

  bitWrite(bitsToSend, 0, digitalRead(inPin0));
  bitWrite(bitsToSend, 1, digitalRead(inPin1));
  bitWrite(bitsToSend, 2, digitalRead(inPin2));
  bitWrite(bitsToSend, 3, digitalRead(inPin3));
  bitWrite(bitsToSend, 4, digitalRead(inPin4));
  bitWrite(bitsToSend, 5, digitalRead(inPin5));
  bitWrite(bitsToSend, 6, digitalRead(inPin6));
  bitWrite(bitsToSend, 7, digitalRead(inPin7));
  bitWrite(bitsToSend, 8, digitalRead(inPin8));
  bitWrite(bitsToSend, 9, digitalRead(inPin9));
  bitWrite(bitsToSend, 10, digitalRead(inPin10));
  bitWrite(bitsToSend, 11, digitalRead(inPin11));
  bitWrite(bitsToSend, 12, digitalRead(inPin12));
  bitWrite(bitsToSend, 13, digitalRead(inPin13));
  bitWrite(bitsToSend, 14, digitalRead(inPin14));

  //Shifting out the collected bit data to 74HC595

  // Pull Latch Pin LOW before sending data
  digitalWrite(latchPin, LOW);

  // Shift the bits out:
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);

  // Turn on the Latch Pin so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

  delay(200);

}

Like this I managed not to mirror it but only the second register lights up, and it lights from the first 8 inputs.

// Declaring input pins

byte inPin0 = A0;   // pushbutton connected to analog pin A0
byte inPin1 = A1;   // pushbutton connected to analog pin A1
byte inPin2 = A2;   // pushbutton connected to analog pin A2
byte inPin3 = A3;   // pushbutton connected to analog pin A3
byte inPin4 = A4;   // pushbutton connected to analog pin A4
byte inPin5 = A5;   // pushbutton connected to analog pin A5
byte inPin6 = 5;    // pushbutton connected to digital pin 5
byte inPin7 = 6;    // pushbutton connected to digital pin 6
byte inPin8 = 7;    // pushbutton connected to digital pin 7
byte inPin9 = 8;    // pushbutton connected to digital pin 8
byte inPin10 = 9;    // pushbutton connected to digital pin 9
byte inPin11 = 10;    // pushbutton connected to digital pin 10
byte inPin12 = 11;    // pushbutton connected to digital pin 11
byte inPin13 = 12;    // pushbutton connected to digital pin 12
byte inPin14 = 13;    // pushbutton connected to digital pin 13




// Declaring output pins

int latchPin = 3;   //Pin connected to ST_CP(pin 12) of 74HC595
int clockPin = 2;   //Pin connected to SH_CP(pin 11) of 74HC595
int dataPin = 4;    //Pin connected to DS(pin 14) of 74HC595

// Declaring the bitsToSend variable which specifies the bit 
// correlating to the specific output pin on the 74HC595

byte bitsToSend;
byte bitsToSend2;

void setup() {
  
  // Declare the chosen input pins to act as inputs
  
  pinMode(inPin0, INPUT);        // sets the analog pin A0 as input
  pinMode(inPin1, INPUT);        // sets the analog pin A1 as input
  pinMode(inPin2, INPUT);        // sets the analog pin A2 as input
  pinMode(inPin3, INPUT);        // sets the analog pin A3 as input
  pinMode(inPin4, INPUT);        // sets the analog pin A4 as input
  pinMode(inPin5, INPUT);        // sets the analog pin A5 as input
  pinMode(inPin6, INPUT);        // sets the digital pin 5 as input
  pinMode(inPin7, INPUT);        // sets the digital pin 6 as input
  pinMode(inPin8, INPUT);        // sets the digital pin 7 as input
  pinMode(inPin9, INPUT);        // sets the digital pin 8 as input
  pinMode(inPin10, INPUT);       // sets the digital pin 9 as input
  pinMode(inPin11, INPUT);       // sets the digital pin 10 as input
  pinMode(inPin12, INPUT);       // sets the digital pin 11 as input
  pinMode(inPin13, INPUT);       // sets the digital pin 12 as input
  pinMode(inPin14, INPUT);       // sets the digital pin 13 as input

  // Declare Arduino board output pins to 74HC595 input pins:
  
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  
  // Read the inputs and collects the values to be shifted out
  // Correlates the input pins to the bits to be sent

  bitWrite(bitsToSend, 0, digitalRead(inPin0));
  bitWrite(bitsToSend, 1, digitalRead(inPin1));
  bitWrite(bitsToSend, 2, digitalRead(inPin2));
  bitWrite(bitsToSend, 3, digitalRead(inPin3));
  bitWrite(bitsToSend, 4, digitalRead(inPin4));
  bitWrite(bitsToSend, 5, digitalRead(inPin5));
  bitWrite(bitsToSend, 6, digitalRead(inPin6));
  bitWrite(bitsToSend, 7, digitalRead(inPin7));
  bitWrite(bitsToSend2, 8, digitalRead(inPin8));
  bitWrite(bitsToSend2, 9, digitalRead(inPin9));
  bitWrite(bitsToSend2, 10, digitalRead(inPin10));
  bitWrite(bitsToSend2, 11, digitalRead(inPin11));
  bitWrite(bitsToSend2, 12, digitalRead(inPin12));
  bitWrite(bitsToSend2, 13, digitalRead(inPin13));
  bitWrite(bitsToSend2, 14, digitalRead(inPin14));

  //Shifting out the collected bit data to 74HC595

  // Pull Latch Pin LOW before sending data
  digitalWrite(latchPin, LOW);

  // Shift the bits out:
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend2);

  // Turn on the Latch Pin so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

  delay(200);

}

How many bits are there in your bitsToSend variable ?

UKHeliBob:
How many bits are there in your bitsToSend variable ?

Sorry I think we posted simuntaniously. Did you mean in my first example?

This solved it. Now let me ponder why :slight_smile:

// Declaring input pins

byte inPin0 = A0;   // pushbutton connected to analog pin A0
byte inPin1 = A1;   // pushbutton connected to analog pin A1
byte inPin2 = A2;   // pushbutton connected to analog pin A2
byte inPin3 = A3;   // pushbutton connected to analog pin A3
byte inPin4 = A4;   // pushbutton connected to analog pin A4
byte inPin5 = A5;   // pushbutton connected to analog pin A5
byte inPin6 = 5;    // pushbutton connected to digital pin 5
byte inPin7 = 6;    // pushbutton connected to digital pin 6
byte inPin8 = 7;    // pushbutton connected to digital pin 7
byte inPin9 = 8;    // pushbutton connected to digital pin 8
byte inPin10 = 9;    // pushbutton connected to digital pin 9
byte inPin11 = 10;    // pushbutton connected to digital pin 10
byte inPin12 = 11;    // pushbutton connected to digital pin 11
byte inPin13 = 12;    // pushbutton connected to digital pin 12
byte inPin14 = 13;    // pushbutton connected to digital pin 13




// Declaring output pins

int latchPin = 3;   //Pin connected to ST_CP(pin 12) of 74HC595
int clockPin = 2;   //Pin connected to SH_CP(pin 11) of 74HC595
int dataPin = 4;    //Pin connected to DS(pin 14) of 74HC595

// Declaring the bitsToSend variable which specifies the bit 
// correlating to the specific output pin on the 74HC595

byte bitsToSend;
byte bitsToSend2;

void setup() {
  
  // Declare the chosen input pins to act as inputs
  
  pinMode(inPin0, INPUT);        // sets the analog pin A0 as input
  pinMode(inPin1, INPUT);        // sets the analog pin A1 as input
  pinMode(inPin2, INPUT);        // sets the analog pin A2 as input
  pinMode(inPin3, INPUT);        // sets the analog pin A3 as input
  pinMode(inPin4, INPUT);        // sets the analog pin A4 as input
  pinMode(inPin5, INPUT);        // sets the analog pin A5 as input
  pinMode(inPin6, INPUT);        // sets the digital pin 5 as input
  pinMode(inPin7, INPUT);        // sets the digital pin 6 as input
  pinMode(inPin8, INPUT);        // sets the digital pin 7 as input
  pinMode(inPin9, INPUT);        // sets the digital pin 8 as input
  pinMode(inPin10, INPUT);       // sets the digital pin 9 as input
  pinMode(inPin11, INPUT);       // sets the digital pin 10 as input
  pinMode(inPin12, INPUT);       // sets the digital pin 11 as input
  pinMode(inPin13, INPUT);       // sets the digital pin 12 as input
  pinMode(inPin14, INPUT);       // sets the digital pin 13 as input

  // Declare Arduino board output pins to 74HC595 input pins:
  
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  
  // Read the inputs and collects the values to be shifted out
  // Correlates the input pins to the bits to be sent

  bitWrite(bitsToSend, 0, digitalRead(inPin0));
  bitWrite(bitsToSend, 1, digitalRead(inPin1));
  bitWrite(bitsToSend, 2, digitalRead(inPin2));
  bitWrite(bitsToSend, 3, digitalRead(inPin3));
  bitWrite(bitsToSend, 4, digitalRead(inPin4));
  bitWrite(bitsToSend, 5, digitalRead(inPin5));
  bitWrite(bitsToSend, 6, digitalRead(inPin6));
  bitWrite(bitsToSend, 7, digitalRead(inPin7));
  bitWrite(bitsToSend2, 0, digitalRead(inPin8));
  bitWrite(bitsToSend2, 1, digitalRead(inPin9));
  bitWrite(bitsToSend2, 2, digitalRead(inPin10));
  bitWrite(bitsToSend2, 3, digitalRead(inPin11));
  bitWrite(bitsToSend2, 4, digitalRead(inPin12));
  bitWrite(bitsToSend2, 5, digitalRead(inPin13));
  bitWrite(bitsToSend2, 6, digitalRead(inPin14));

  //Shifting out the collected bit data to 74HC595

  // Pull Latch Pin LOW before sending data
  digitalWrite(latchPin, LOW);

  // Shift the bits out:
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend2);

  // Turn on the Latch Pin so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

  delay(200);

}

Perhaps RT_M

shiftOut()

Especially the note section that says:

"shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255"

adwsystems:
Perhaps RT_M

shiftOut()

Especially the note section that says:

"shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255"

Thank you adwsystems, i have now studied that very carefully and it makes sence about the bits. I had to trial and error a couple of times with what affected what regarding the input numbers and where and how I should split, but after a few tests it worked. I had used up all pins so I only had 8+7, which actually is kind of helpful when finding that out.

Is my code very ugly btw?

You could condense your code quite a bit.

const byte inPins[] = { A0, A1, A2, A3, A4, A5, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

const byte latchPin = 3;   //Pin connected to ST_CP(pin 12) of 74HC595
const byte clockPin = 2;   //Pin connected to SH_CP(pin 11) of 74HC595
const byte dataPin = 4;    //Pin connected to DS(pin 14) of 74HC595

// Declaring the bitsToSend variable which specifies the bit
// correlating to the specific output pin on the 74HC595

byte bitsToSend;
byte bitsToSend2;

void setup() {
  for (auto pin : inPins) {
    pinMode(pin, INPUT);
  }

  // Declare Arduino board output pins to 74HC595 input pins:

  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {

  // Read the inputs and collects the values to be shifted out
  // Correlates the input pins to the bits to be sent

  for (byte i = 0; i < sizeof(inPins); i++) {
    if (i < 8) {
      bitWrite(bitsToSend, i, digitalRead(inPins[i]));
    } else {
      bitWrite(bitsToSend2, i - 8, digitalRead(inPins[i]));
    }
  }

  //Shifting out the collected bit data to 74HC595

  // Pull Latch Pin LOW before sending data
  digitalWrite(latchPin, LOW);

  // Shift the bits out:
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
  shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend2);

  // Turn on the Latch Pin so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

  delay(200);
}

Whandall:
You could condense your code quite a bit.

Wow! Very good learning, thank you!

I tested to change LSB to MSB to see how that affected it. It changed the order on each register. But why is the first buttons affecting the last register? I havent figured that out yet.

stajo:
But why is the first buttons affecting the last register? I havent figured that out yet.

Because it (the byte containing it) gets shifted out first, so it lands last.

Whandall:
Because it (the byte containing it) gets shifted out first, so it lands last.

So they´re made to function like that, if more then one byte they ship the first onwards, or do I get you wrong?

Well I just reorged the shiftouts and then it works like I had in mind.

A shift register is supposed to shift its content out to the next register,
that's what makes it a shift register

You could put any bit to any position just by placing it in the right spot in the inPins array.
But thats just a different method to achieve the same mapping.

Actually both examples and both bitsToSend/bitsToSend2 variables

Hi folks, happy new year and all that.

After a full day of code debugging, googling and head scratching I just found a faulty ground wire after some measuring on the jucky breadboard. So the code I made here, for 8 to 8 shift in/shift out with 74HC165 and 74HC595 it just worked after finding the broken wire.

I cleaned it up and commented it a bit. I just want some opinion if its overly complicated, non logic or something. All feedback welcome!

// Give each respective Input Pin a named variable
// Each pin is to connect to the serial inputs of
// the PISO Shift Register

const byte inLatchPin = 5;
const byte inClockPin = 6;
const byte inDataPin = 7;
const byte inEnablePin = 8;

// Give each respective Output Pin a named variable
// Each pin is to connect to the serial inputs of
// the SIPO Shift Register

const byte outLatchPin = 2;
const byte outClockPin = 3;
const byte outDataPin = 4;

// Initializes a variable for storing the byte that
// is going to be sent to the Shift Register

byte shiftByte;

void setup() {
  
  // Initializes the Input Pins to work in Output  
  // Mode to the PISO Shift Register Latch Pin
  // and Clock Pin, and in Input Mode to the
  // inDataPin
  
  pinMode(inLatchPin, OUTPUT);
  pinMode(inClockPin, OUTPUT);
  pinMode(inDataPin, INPUT);
  pinMode(inEnablePin, OUTPUT);

  // Initializes the Output Pins to work in Output
  // Mode to the SIPO Shift Register input pins
  
  pinMode(outLatchPin, OUTPUT);
  pinMode(outClockPin, OUTPUT);
  pinMode(outDataPin, OUTPUT);

  // Required initial states of these two pins
  // according to the datasheet timing diagram

  digitalWrite(inLatchPin,HIGH);
  digitalWrite(inEnablePin,HIGH);
}

void loop() {

  // ********INPUT PART********

  // Reading the PISO Shift Register
  
  // Pulse the Input Latch Pin to load the parallell
  // data in the PISO Shift Register to the serial outputs

  digitalWrite(inLatchPin, LOW);
  delayMicroseconds(5);
  digitalWrite(inLatchPin, HIGH);
  delayMicroseconds(5);  

  // Shift the bits out from the PISO Shift Register

  digitalWrite(inClockPin, HIGH);
  digitalWrite(inEnablePin, LOW);

  shiftByte = shiftIn(inDataPin, inClockPin, MSBFIRST);

  digitalWrite(inEnablePin, HIGH);

  // ********OUTPUT PART******** 

  // Pull Output Latch Pin LOW before sending data
  
  digitalWrite(outLatchPin, LOW);

  // Shift the bits out to the SIPO Shift Register
  
  shiftOut(outDataPin, outClockPin, MSBFIRST, shiftByte);

  // Turn on the Output Latch Pin to move the values to 
  // the output stage of the Shift Register
  
  digitalWrite(outLatchPin, HIGH);

  // Give the process a 0,2 sec break

  delay(200);

}