Daisy Chaining Shift Registers Code Question

Hi, I am trying to set up two 74HC595 shift registers daisy chained to one another in order to output 16 bits of information at once through only a few pins on the Arduino. I found this lovely tutorial on how to control one shift register, but I am unable to figure out what I should modify to allow myself to output 16 bits of information instead of just 8 bits. This is the code they used in the example. I have lots of programming experience in Java but I'm newer to C, so I'm just caught on what I should change. Thanks in advance, I will also link the tutorial I followed to get the first one working.

int latchPin = 5;  // Latch pin of 74HC595 is connected to Digital pin 5
int clockPin = 6; // Clock pin of 74HC595 is connected to Digital pin 6
int dataPin = 4;  // Data pin of 74HC595 is connected to Digital pin 4

byte leds = 0;    // Variable to hold the pattern of which LEDs are currently turned on or off

/*
 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
 */
void setup() 
{
  // Set all the pins of 74HC595 as OUTPUT
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

/*
 * loop() - this function runs over and over again
 */
void loop() 
{
  leds = 0; // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
  updateShiftRegister();
  delay(500);
  for (int i = 0; i < 8; i++) // Turn all the LEDs ON one by one.
  {
    bitSet(leds, 9);    // Set the bit that controls that LED in the variable 'leds'
    updateShiftRegister();
    delay(500);
  }
}

/*
 * updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function 'shiftOut' to shift out contents of variable 'leds' in the shift register before putting the 'latchPin' high again.
 */
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}


https://lastminuteengineers.com/74hc595-shift-register-arduino-tutorial/#:~:text=A%20shift%20register%20allows%20you,using%20only%20three%20input%20pins.

Welcome,

Look here :slight_smile: Controlling 16 LED's with two 74HC595 Shift register

Hint: "byte leds = 0; // Variable to hold the pattern of which LEDs are currently turned on or off" A byte is 8 bits, an int is 16 bits.

uint16_t leds = 0;
...
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, lowByte(leds));
   shiftOut(dataPin, clockPin, LSBFIRST, highByte(leds));
   digitalWrite(latchPin, HIGH);

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