Led will not loop

Hi, sorry to ask but I have been working on this sketch for a few days, and I have I just can’t seem to get the LED to loop I have the rest of it working. what I am trying to do is sequential turn signal using 595 shift register I want the LED to light up one at a time once the last LED is lite up they all turn off instantly and the sequence starts again. Im using a push button at the moment, my issue is I press the button and the LEDs light up in sequence and stay on . i know the problem is something I did or didn’t do, so Im hoping that fresh eye will see it
I have attached the sketch

thanks

sample2.ino (1.25 KB)

therat69:
Hi, sorry to ask but I have been working on this sketch for a few days, and I have I just can't seem to get the LED to loop I have the rest of it working. what I am trying to do is sequential turn signal using 595 shift register I want the LED to light up one at a time once the last LED is lite up they all turn off instantly and the sequence starts again. Im using a push button at the moment, my issue is I press the button and the LEDs light up in sequence and stay on . i know the problem is something I did or didn't do, so Im hoping that fresh eye will see it
I have attached the sketch

thanks

is this not the same as YOUR OTHER POST????

what does it mean to loop a LED ?

post code directly inline with code tags </> so that we can read it… Most participants won’t download a file to their computer.

Hi, this is similar, and the last one is in use at the moment

.

int latchPin = 9;
int clockPin = 10;
int dataPin = 8;
int buttonPin = 11;
int buttonValue;
int numOfRegisters = 3;
byte* registerState;




void setup() {
  //Initialize array
  registerState = new byte[numOfRegisters];
  for (size_t i = 0; i < numOfRegisters; i++) {
    registerState[i] = 0;
  }

  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(buttonPin, INPUT);
}


void loop() 
{  
  buttonValue = digitalRead(buttonPin);
  if (buttonValue == HIGH){
    for (int i = 24; i > 0; i--){
    for (int k = 0; k < i; k++){
      regWrite(k, HIGH);
      delay(100);
      regWrite(k, LOW);
    }
    regWrite(i, HIGH);
  }
}
}

void regWrite(int pin, bool state){
  //Determines register
  int reg = pin / 8;
  //Determines pin for actual register
  int actualPin = pin - (8 * reg);

  //Begin session
  digitalWrite(latchPin, LOW);
 

  for (int i = 0; i < numOfRegisters; i++){
    //Get actual states for register
    byte* states = &registerState[i];
 
    //Update state
    if (i == reg){
      bitWrite(*states, actualPin, state);
    }

    //Write
    shiftOut(dataPin, clockPin, LSBFIRST,  *states);
  }

  //End session
  digitalWrite(latchPin, HIGH);
}

LEDs don't "loop". They illuminate (or not).

    for (int i = 24; i > 0; i--){
    for (int k = 0; k < i; k++){
      regWrite(k, HIGH);
      delay(100);
      regWrite(k, LOW);
    }
    regWrite(i, HIGH);
  }

You are lighting ‘pin’ 24 which is one past your array. Your three bytes contain pins 0 through 23.

Are you still holding down the button? The code does nothing unless the button reads HIGH.

Your ‘array of pointers to bytes’ seems overly complicated. Here is a guess as to what it might look like using a simple ‘array of bytes’:

int latchPin = 9;
int clockPin = 10;
int dataPin = 8;
int buttonPin = 11;
int buttonValue;


const byte NumberOfLEDs = 24;
const byte NumberOfRegisters = 3;
byte registers[NumberOfRegisters];


void setup()
{
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}


void loop()
{
  buttonValue = digitalRead(buttonPin);
  if (buttonValue == HIGH)
  {
    for (int i = NumberOfLEDs-1; i >= 0; i--)
    {
      for (int k = 0; k < i; k++)
      {
        regWrite(k, HIGH);
        delay(100);
        regWrite(k, LOW);
      }
      regWrite(i, HIGH);
    }
  }
}


void regWrite(int pin, bool state)
{
  //Determines register
  int reg = pin / 8;
  //Determines pin for actual register
  int actualPin = pin % 8;


  // Set the bit
  bitWrite(registers[reg], actualPin, state);




  // Update the outputs
  
  //Begin session
  digitalWrite(latchPin, LOW);


  for (int i = 0; i < NumberOfRegisters; i++)
  {
    shiftOut(dataPin, clockPin, LSBFIRST,  registers[i]);
  }


  //End session
  digitalWrite(latchPin, HIGH);
}

Hi John, thanks you, the code I wrote was almost as long as war and peace the novel and you did it in just under a paragraph and that is experience thanks, I loaded the code, unfortunately it’d does exactly the same a my code , as for the button I only tap it and the led light up but after led lights they stay on but now I know it has to do with the "regWrite(I, HIGH)’ when I comment this line out the leds will flash only

   {
        regWrite(k, HIGH);
        delay(100);
        regWrite(k, LOW);
      }
      regWrite(i, HIGH);
    }
  }

how did you wire your push button ?

sherzaad:
is this not the same as YOUR OTHER POST???
https://forum.arduino.cc/index.php?topic=677650.0

therat69:
Hi, this is similar, and the last one is in use at the moment

so what are you trying to achieve now that is DIFFERENT from then?

ie what is stopping you from re-using/modifying the solution you got then?

J-M-L:
how did you wire your push button ?

A penny to a Porsche says the input pin is floating. :slight_smile:

Im sorry for wasting everyones time on my stubbiness, And I like to thank John for fine tuning the code and J-M-L for making me look at the way the push button was wired. The issue was not the code but the way I wired the push button!! I sincerely apologise for wasting everyone's time for my stubbiness!! It was easy to blame the software and not the hardware. once again THANK YOU!!

Karma point for reply # 10 :wink:

Don’t worry we all made that mistake at least once
That’s how you learn