improvement on my code

4 shift registers,can any1 help me to improve on my code as i want to do more patterns and understand it.Thanks…

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


int first = 0;
int second = 0;
int Third = 0;
int fourth = 0;
int waitfor = 100;


void setup() {
 pinMode (dataPin, OUTPUT);
 pinMode (clockPin, OUTPUT);
 pinMode (latchPin, OUTPUT);
}

void loop() {
 lightLeds (0b00000000, 0b00000000, 0b00000000, 0b00000000);
 delay(100);
 lightLeds (0b11111111, 0b11111111, 0b11111111, 0b11111111);
 
}

void lightLeds (int first, int second, int third, int fourth) {
 digitalWrite (latchPin, LOW);
 shiftOut (dataPin, clockPin, LSBFIRST, first);
 shiftOut (dataPin, clockPin, LSBFIRST, second);
 shiftOut (dataPin, clockPin, LSBFIRST, third);
 shiftOut (dataPin, clockPin, LSBFIRST, fourth);
 
 
 digitalWrite (latchPin, HIGH);
 delay (waitfor);
}
int first = 0;
int second = 0;
int Third = 0;
int fourth = 0;

These aren't used at all.

The variables of the same name in lightLeds() are separate to these (belong to the function).

Use a timeout to run code, rather than a delay. As in use the millis() function to determine when to run your code next; effectively leaving loop to 'loop' constantly until there is something to do.

Look at the example in the the IDE "blink without delay" for hints.

And lastly, what sort of patterns do you want. Maybe look into the left & right shift operators.

im trying to make different patterns like a knight rider rain alternate blink and so on...im sorry but i did not understand what you meant by making changes to the code.im new so please be kind.Thanks..

int dispVal;

void loop()
{
 lightLeds (dispval, ~dispVal, dispVal-42, dispVal>>4);
 dispVal++;
 delay(100);
}

Just playing around.

For Halloween, I added 20 LEDs to the bottom of my daughter’s dress, controlling (no shift register) using bitMasks and some cute functions. Maybe you can find some ideas in this:

#define MULTI_FUNCTION

const int ledPin[20] = {1,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A4,A5,A6,A7};

const unsigned long smashMask[9] = {0b100000000000000001, 0b010000000000000010, 0b001000000000000100, 0b000100000000001000, 0b000010000000010000, 0b000001000000100000, 0b000000100001000000, 0b000000010010000000, 0b000000001100000000};
const unsigned long carouselMask[8] = {0b110000000011000000, 0b011000000001100000, 0b001100000000110000, 0b000110000000011000, 0b000011000000001100, 0b000001100000000110, 0b000000110000000011, 0b100000011000000001};
const unsigned long minaret[5] = {0b11000110001100011000, 0b01100011000110001100, 0b00110001100011000110, 0b00011000110001100011, 0b10001100011000110001};
const unsigned long marquee[4] = {0b11001100110011001100, 0b01100110011001100110, 0b00110011001100110011, 0b10011001100110011001};

void (*ledDisplay[10])(void);

byte state;
byte lastState;
boolean printed = false;
unsigned long loopTime;
//
void setup()
{
  randomSeed(analogRead(A0));
  ledDisplay[0] = ledDisplay0;
  ledDisplay[1] = ledDisplay1;
  ledDisplay[2] = ledDisplay2;
  ledDisplay[3] = ledDisplay3;
  ledDisplay[4] = ledDisplay4;
  ledDisplay[5] = ledDisplay5;
  ledDisplay[6] = ledDisplay6;
  ledDisplay[7] = ledDisplay7;
  ledDisplay[8] = ledDisplay8;
  ledDisplay[9] = ledDisplay9;
  for (byte i = 0; i < 20; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
  state = random(9);
}
//
void loop() 
{
  #ifdef MULTI_FUNCTION
  if (millis() - loopTime >= 10000UL)
  {
    lastState = state;
    while (state == lastState)
    {
      state = random(10);
    }
    clearLeds();
    loopTime = millis();
  }
  ledDisplay[state]();
  #else
  ledDisplay[8]();//<<<<<<<<<<<<<<<<<<<<<< sample any single function here
  #endif
}
//
void clearLeds(void)
{
  for (byte i = 0; i < 18; i++)
  {
    digitalWrite(ledPin[i], LOW);
  }
}
//
void ledDisplay0(void)
{
  static unsigned long lastBlinkTime;
  static byte spinIndex = 0;
  static  byte increment = 1;
  if (millis() - lastBlinkTime >= 80UL)
  {
    for (byte i = 0; i < 20; i++)
    {
      digitalWrite(ledPin[i], bitRead(1UL << spinIndex, i));
    }
    spinIndex++;
    if (spinIndex >= 20) spinIndex = 0;
    lastBlinkTime =millis();
  }
}
//
void ledDisplay1(void)  //sparkle effect
{
  int myLed = random(20);
  digitalWrite(ledPin[myLed], HIGH);
  delay(15);
  digitalWrite(ledPin[myLed], LOW);
  delay(random(50,100));
}
//
void ledDisplay2(void) //collision effect
{
  static unsigned long smashStart;
  static byte smashIndex = 0;
  static byte smashIncrement = 1;
  if (millis() - smashStart >= 100UL)
  {
    for (byte i = 0; i < 18; i++)
    {
      digitalWrite(ledPin[i], bitRead(smashMask[smashIndex], i));
    }
    smashIndex += smashIncrement;
    if (smashIndex == 0 || smashIndex == 8)
    {
      smashIncrement = -smashIncrement;
    }
    smashStart = millis();
  }
}
//
void ledDisplay3(void) //spinning carousel effect
{
  static unsigned long carouselStart;
  static byte carouselIndex = 0;
  static byte carouselIncrement = 1;
  static boolean clockWise;
  if (millis() - carouselStart >= 200UL)
  {
    for (byte i = 0; i < 18; i++)
    {
      digitalWrite(ledPin[i], bitRead(carouselMask[carouselIndex], i));
    }
    carouselIndex += carouselIncrement;
    if (carouselIndex == 0 || carouselIndex == 7)
    {
      carouselIncrement = -carouselIncrement;
    }
    carouselStart = millis();
  }
}
//
void ledDisplay4(void)  //simple spinning
{
  static unsigned long lastBlinkTime1;
  static byte spinIndex1 = 0;
  if (millis() - lastBlinkTime1 >= 100UL)
  {
    for (byte i = 0; i < 18; i++)
    {
      digitalWrite(ledPin[i], bitRead(1UL << spinIndex1, i));
    }
    spinIndex1++;
    if (spinIndex1 >= 18) spinIndex1 = 0;
    lastBlinkTime1 = millis();
  }
}
//
void ledDisplay5(void) //
{
  static unsigned long lastBlinkTime;
  static byte spinIndex = 0;
  static  byte increment = 1;
  if (millis() - lastBlinkTime >= 1)
  {
    for (byte i = 0; i < 18; i++)
    {
      digitalWrite(ledPin[i], bitRead(1UL << spinIndex, i));
    }
    spinIndex++;
    if (spinIndex >= 18) spinIndex = 0;
    lastBlinkTime =millis();
  }
}
//
void ledDisplay6(void) //accelerating spin effect
{
  int spiralTime = 75UL;
  for (int k = 0; k < spiralTime; k += 5)
  {
    for (int j = 0; j < 18; j ++)
    {
      for (int i = 0; i < 18; i++)
      {
        digitalWrite(ledPin[i], bitRead(1UL << j, i));
      }
      delay (spiralTime - k);
    } 
  }
  for (int k = 0; k < spiralTime; k += 5)
  {
    for (int j = 0; j < 18; j ++)
    {
      for (int i = 0; i < 18; i++)
      {
        digitalWrite(ledPin[i], bitRead(1UL << j, i));
      }
      delay (k);
    } 
  }
}
//
void ledDisplay7(void)
{
  static unsigned long minaretStart;
  static byte minaretIndex = 0;
  static byte minaretIncrement = 1;
  if (millis() - minaretStart >= 200UL)
  {
    for (byte i = 0; i < 18; i++)
    {
      digitalWrite(ledPin[i], bitRead(minaret[minaretIndex], i));
    }
    minaretIndex += minaretIncrement;
    if (minaretIndex > 4) minaretIndex = 0;
    minaretStart = millis();
  }
}
//
void ledDisplay8(void)
{
  static unsigned long marqueeStart;
  static byte marqueeIndex = 0;
  static byte marqueeIncrement = 1;
  if (millis() - marqueeStart >= 150UL)
  {
    for (byte i = 0; i < 18; i++)
    {
      digitalWrite(ledPin[i], bitRead(marquee[marqueeIndex], i));
    }
    marqueeIndex += marqueeIncrement;
    if (marqueeIndex > 3) marqueeIndex = 0;
    marqueeStart = millis();
  }
}
void ledDisplay9(void)
{
  for (int i = 0; i < 20; i++)
  {
    digitalWrite(ledPin[i], LOW);
  }
}