Can someone help me understand this code?

int clockpin = 12; //chân ST_CP của 74HC595
int latchpin = 8; //chân SH_CP của 74HC595
int datapin = 11; //Chân DS của 74HC595
byte ledstatus; //Trạng thái của LED, hay chính là byte mà ta sẽ gửi qua shiftOut

void setup()
{
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(datapin, OUTPUT);
}
int cycle = 0;
void loop()
{
ledstatus = 0;

for (int i = 0; i < 6; i++)
{
ledstatus = (ledstatus << 1) | 1;

digitalWrite(latchpin, 0);

shiftOut(datapin, clockpin, MSBFIRST, ledstatus);

digitalWrite(latchpin, 1);

delay(250);

}

for (int i = 0; i < 8; i++)
{
ledstatus <<= 1; //Đẩy toàn bộ các bit qua trái 1 bit
digitalWrite(latchpin, 0);
shiftOut(datapin, clockpin, MSBFIRST, ledstatus);
digitalWrite(latchpin, 1);
delay(250);
cycle++;
}
if (cycle >2)

digitalWrite(latchpin,HIGH);

}

I need to control 6 LEDs, and have them flow one after the other through a shift register. I dont understand this code, so if someone could break it down for me that would be great!

Yes I understand what you mean. I also struggled a lot with shift registers. Most tutorials are incomplete or just to hard for beginners. Your sketch is also using not the most efficient way of driving the shift register.

You should drive the shift register with SPI. You can google how to do that. The code below will be easier for you to understand.

#include <SPI.h>

byte shiftregister; //Store the status of the led, if it's on or off.

int latchpin = 10;

void setup() {

  SPI.begin();
}

void loop() {
  
}
 
  
  for (byte i = 0; i < 8; i++)  //8 leds >> 0-7
  
  {
  bitWrite(shiftregister, i, 1);  //Selects the shift register, selects the led, 1 = on 0 = off
  digitalWrite(latchpin,LOW); 
  SPI.transfer(shiftregister); //Transfers the data to the shift register.
  digitalWrite(latchpin,HIGH); 

  delay(500);
  }

@Lennyz1988 thanks for helping the OP out, I agree your code is easier to understand.

And your comment about efficiency of SPI versus shiftOut() is absolutely true. But completely irrelevant here. Let's get some perspective: the difference in time taken to send out one bye of data comparing SPI.transfer() with shiftOut() is a few microseconds. After that, the code performs a delay, "wasting" half a million microseconds!

If you have hundreds or thousands of bytes to send out every second, then SPI is a must. But criticizing a beginner for using it like this... well, there are many other more important things you could and should help them with (and please continue to do that).

Using shiftOut() is slightly easier for a beginner. There's no library to include, no begin() or other parameters to set beforehand, and you can use any pins you want.

Hi, Alco100,

It is absolutely normal that you can’t understand that code. It does useless things, does things incompletely, and is not commented.

Here is the same code with my comments:

//The 74HC595 is used to store 8 bits (0 or 1).
//The protocol is:
//Advise me that you want to change some bits (Open the latch)
//Send me the value of the 8 bits             (shiftOut)
//Advise me that you are done                 (Close the latch)
//
//shiftOut(dataPin, clockPin, bitOrder, value)
//This is the routine that will send the 8 bits to the 74HC595
//It needs a pin to send the 0s ans 1s, (dataPin)
//A pin to coordinate the communication (clockPin)
//A constant to specify if you want the bits sent:
//  from right to left LSBFIRST or
//  from left to right MSBFIRST
//and, finally a byte (a byte contains 8 bits) that holds the 0s and 1s

int latchpin = 8;  //The pin that starts and ends the communication
int clockpin = 12; //The pin that coordinates the communication
int datapin = 11;  //The pin on which the 0s and 1s are sent
byte ledstatus;    //The byte that holds the 0s and 1s
 
void setup() //The three pins will send information (OUTPUT)
{
  pinMode(clockpin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(datapin, OUTPUT);
}

int cycle = 0; //This is useless: you have less than 8 LEDs to control


void loop()
{
//THIS CODE IS USELESS: It sends 6 times 0s to the 74HC595 (the next 9 lines)
//------------------------------------------------------------
  ledstatus = 0;
  for (int i = 0; i < 6; i++)
  {
    ledstatus = (ledstatus << 1) | 1;
    digitalWrite(latchpin, 0);
    shiftOut(datapin, clockpin, MSBFIRST, ledstatus);
   digitalWrite(latchpin, 1);
   delay(250);
  }
//-------------------------------------------------------------


  for (int i = 0; i < 8; i++)  //This is useless
  {
    ledstatus <<= 1; // This is useless 
    digitalWrite(latchpin, 0); //Open the latch
    shiftOut(datapin, clockpin,  MSBFIRST, ledstatus); //Send the 0s and 1s
    digitalWrite(latchpin, 1); //Close the latch
    delay(250);  //This is useless
    cycle++;     //This is useless
  }
if (cycle >2)  //This is useless
  
  digitalWrite(latchpin,HIGH); //This is useless (Already done)
  
}

And here is what it should have looked like in the first place :

//The 74HC595 is used to store 8 bits (0 or 1).
//The protocol is:
//Advise me that you want to change some bits (Open the latch)
//Send me the value of the 8 bits             (shiftOut)
//Advise me that you are done                 (Close the latch)
//
//shiftOut(dataPin, clockPin, bitOrder, value)
//This is the routine that will send the 8 bits to the 74HC595
//It needs a pin to send the 0s ans 1s, (dataPin)
//A pin to coordinate the communication (clockPin)
//A constant to specify if you want the bits sent:
//  from right to left LSBFIRST or
//  from left to right MSBFIRST
//and, finally a byte (a byte contains 8 bits) that holds the 0s and 1s

int latchPin = 8;  //The pin that starts and ends the communication
int clockPin = 12; //The pin that coordinates the communication
int dataPin = 11;  //The pin on which the 0s and 1s are sent
byte ledStatus;    //The byte that holds the 0s and 1s
 
void setup() //The three pins will send information (OUTPUT)
{
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop()
{
  //Change the value of ledStatus to whatever you like

  digitalWrite(latchPin, LOW);                       //Open the latch
  shiftOut(dataPin, clockPin,  MSBFIRST, ledStatus); //Send the 0s and 1s
  digitalWrite(latchPin, HIGH);                      //Close the latch  
  delay(500); //Just to have time to see the result (not needed)
}

I hope that this will shed some light on this piece of code.

Jacques