Push Button to activate moving lights

I'm trying to use a Push button to start the code. For example when the button is pushed the code will make the LED's light up back and forth and when the button is pushed again the sequence is stopped. I've debounced the push button and add the code to do this sequence. However when i push the button only one LED turns on and it won't turn off if the button is pushed again. I was wondering if someone could give me guidance as i've been struggling for a long time.

int delayTime =40;

int currentLED = 6; //a variable to store which LED we are currently 
int currentLED1 =0;
int dir = 1;        //A variable to store the direction of travel 
int dir1 =1;

long timeChanged = 0;  //A variable to store the last time we changed something
long timeChanged1 = 0;
byte ledPin[] = {4,5,6,7,8,9,10,11,12,13}; //An array to hold the value for each LED pin 


void setup() {
for (int i=0; i<10; i++){
  pinMode(ledPin[i],OUTPUT);
}
timeChanged = millis();    //Record the tieem once the setup has completed
}

void loop() {
  if((millis()-timeChanged)>delayTime){    //Cheack whether it has been long enough 
    for (int i=5; i<10; i++) {             
      digitalWrite(ledPin[i],LOW);          // turn all the LEDs off
    }
    digitalWrite(ledPin[currentLED],HIGH);   //light the current LED

    currentLED= currentLED - dir;            //increase the direction value (up or down)
    if (currentLED ==9) {
      dir =1;
    }
    if(currentLED == 5){                      //If we are at the end of a row, change direction 
      dir =-1;
    }
    timeChanged = millis();                   //Store the current time as the time we last changed LEDS
  }
    if ((millis()-timeChanged1)>delayTime){
    for (int i=0; i<5; i++){
      digitalWrite(ledPin[i],LOW);
    }
    digitalWrite(ledPin[currentLED1],HIGH);
    currentLED1 = currentLED1 + dir1;
    if (currentLED1 == 4){
      dir1 = -1;
    }
    if(currentLED1 ==0){
      dir1 = 1;
    }
   timeChanged1=millis(); 
  }
  
}[int delayTime =40;

int currentLED = 6; //a variable to store which LED we are currently 
int currentLED1 =0;
int dir = 1;        //A variable to store the direction of travel 
int dir1 =1;

long timeChanged = 0;  //A variable to store the last time we changed something
long timeChanged1 = 0;
byte ledPin[] = {4,5,6,7,8,9,10,11,12,13}; //An array to hold the value for each LED pin 


void setup() {
for (int i=0; i<10; i++){
  pinMode(ledPin[i],OUTPUT);
}
timeChanged = millis();    //Record the tieem once the setup has completed
}

void loop() {
  if((millis()-timeChanged)>delayTime){    //Cheack whether it has been long enough 
    for (int i=5; i<10; i++) {             
      digitalWrite(ledPin[i],LOW);          // turn all the LEDs off
    }
    digitalWrite(ledPin[currentLED],HIGH);   //light the current LED

    currentLED= currentLED - dir;            //increase the direction value (up or down)
    if (currentLED ==9) {
      dir =1;
    }
    if(currentLED == 5){                      //If we are at the end of a row, change direction 
      dir =-1;
    }
    timeChanged = millis();                   //Store the current time as the time we last changed LEDS
  }
    if ((millis()-timeChanged1)>delayTime){
    for (int i=0; i<5; i++){
      digitalWrite(ledPin[i],LOW);
    }
    digitalWrite(ledPin[currentLED1],HIGH);
    currentLED1 = currentLED1 + dir1;
    if (currentLED1 == 4){
      dir1 = -1;
    }
    if(currentLED1 ==0){
      dir1 = 1;
    }
   timeChanged1=millis(); 
  }[code]

[/code]

There is a difference between detected a button being held down and detecting "button just activated". Make shure You detect the latter. Than create a logical flip-flop that changes state when "button just pressed" has occured.

You have 2 Setup() and 2 loop().That can't work.
Use autoformat in the IDE to make the code more readable.
What does the compiler say?

sorry i post the wrong code. Here is the actual code

int delayTime =40;
int PushButton =3;
int currentLED = 6; //a variable to store which LED we are currently 
int currentLED1 =0;
int dir = 1;        //A variable to store the direction of travel 
int dir1 =1;
int ledState =LOW;
int buttonState = 0;
int lastButtonState =1;
unsigned long lastDebounceTime =0;
unsigned long debounceDelay =50;
long timeChanged = 0;  //A variable to store the last time we changed something
long timeChanged1 = 0;
byte ledPin[] = {4,5,6,7,8,9,10,11,12,13}; //An array to hold the value for each LED pin 

void setup() {
  pinMode (PushButton, INPUT);
for (int i=0; i<10; i++){
  pinMode(ledPin[i],OUTPUT);
}
timeChanged = millis();    //Record the tieem once the setup has completed
}

void loop() {
 int reading = digitalRead(PushButton);
 if (reading != lastButtonState){
  lastDebounceTime = millis();
 }
if((millis()-lastDebounceTime)>debounceDelay){
  if(reading != buttonState){
    buttonState = reading;

  if (buttonState == HIGH){
    ledState =!ledState;
    }
  }
}

if((millis()-timeChanged)>delayTime){    //Cheack whether it has been long enough 
    for (int i=5; i<10; i++) {             
      digitalWrite(ledPin[i],LOW);          // turn all the LEDs off
    }
    digitalWrite(ledPin[currentLED],HIGH);   //light the current LED

    currentLED= currentLED - dir;            //increase the direction value (up or down)
    if (currentLED ==9) {
      dir =1;
    }
    if(currentLED == 5){                      //If we are at the end of a row, change direction 
      dir =-1;
    }
    timeChanged = millis();                   //Store the current time as the time we last changed LEDS
  }
    if ((millis()-timeChanged1)>delayTime){
    for (int i=0; i<5; i++){
      digitalWrite(ledPin[i],LOW);
    }
    digitalWrite(ledPin[currentLED1],HIGH);
    currentLED1 = currentLED1 + dir1;
    if (currentLED1 == 4){
      dir1 = -1;
    }
    if(currentLED1 ==0){
      dir1 = 1;
    }
   timeChanged1=millis(); 
  }

}

Duplicate threads moved to same section and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I am sorry. First time using the forum.

Why are you wanting to do this ?

Why did you select a user name like that ?

I want to do as i am making a sign. And i want to turn it off when not in use. I accidently clicked ok

Do you know about using CTRL T to format your code ?

No i'm relatively new to Arduino. I've just tried it out and i see what you mean

You need a 'flag' in there to tell the code the sequence is active or not.
I think ledState would do that, but you don't do anything with it.

For example,

if( ((millis()-timeChanged)>delayTime) && (ledState == 1)){    //Cheack whether it has been long enough

Put it in here too

    if ((millis()-timeChanged1)>delayTime){

How is your switch wired ?

CrossRoads:
You need a 'flag' in there to tell the code the sequence is active or not.
I think ledState would do that, but you don't do anything with it.

For example,

if( ((millis()-timeChanged)>delayTime) && (ledState == 1)){    //Cheack whether it has been long enough

Put it in here too

    if ((millis()-timeChanged1)>delayTime){

Thank you for your response! I've done as you've instructed and it's halfed worked. The code will start but when i push the button again to stop the code I get no response and the lights keep running.

If you just want to turn things off when the button is pushed

const unsigned long delayTime = 40;
const int PushButton = 3;
int currentLED;
int dir = 1;        //A variable to store the direction of travel
int buttonState = 0;
int lastButtonState = 1;
unsigned long debounceDelay = 50;
unsigned long timeChanged = 0;  //A variable to store the last time we changed something

const byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //An array to hold the value for each LED pin
const byte nPins = sizeof(ledPin) / sizeof(ledPin[0]);

bool isRunning = false;

void setup() {
  pinMode (PushButton, INPUT);
  for (int i = 0; i < nPins; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  timeChanged = millis();    //Record the time once the setup has completed
}

void loop() {
  int reading = digitalRead(PushButton);
  if (reading != lastButtonState) {
    if (buttonState == HIGH) {
      isRunning = !isRunning;
      if ( isRunning == false ) {
        // turn everything off
        for (int i = 0; i < nPins; i++) {
          digitalWrite(ledPin[i], LOW);
        }
      } else {
        // start lights
        timeChanged = millis();
        currentLED = 0;
      }
    }
    delay(delayTime);
  }
  lastButtonState = reading;

  if ( !isRunning ) {
    return;
  }

  //Check whether it has been long enough
  if ((millis() - timeChanged) >= delayTime) {
    digitalWrite(ledPin[currentLED], LOW); // turn off current
    currentLED += dir;
    if ( dir > 0 ) {
      // check for upper limit
      if ( currentLED >= nPins ) {
        currentLED = nPins - 1;
        dir = -1;
      }
    } else {
      // check for lower limit
      if ( currentLED < 0 ) {
        dir = 1;
        currentLED = 0;
      }
    }
  }
  digitalWrite(ledPin[currentLED], HIGH);  //light the current LED
  timeChanged = millis();                   //Store the current time as the time we last changed LEDS
}
const byte PushButton   = 3;

byte  currentLED        = 6;    //a variable to store which LED we are currently
byte  currentLED1       = 0;
byte  ledState          = LOW;
byte  buttonState       = 0;
byte  lastButtonState   = 1;
const byte ledPin[]     = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};  //An array to hold the value for each LED pin
int dir                 = 1;    //A variable to store the direction of travel
int dir1                = 1;

unsigned long delayTime        = 40;
unsigned long timeChanged      = 0;    //A variable to store the last time we changed something
unsigned long timeChanged1     = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay    = 50;
unsigned long switchMillis;

//*******************************************************************************
void setup()
{
  pinMode (PushButton, INPUT);

  for (int i = 0; i < 10; i++)
  {
    pinMode(ledPin[i], OUTPUT);
    digitalWrite(ledPin[i], LOW);
  }

  timeChanged = millis();    //Record the tieem once the setup has completed

} //END of setup()

//*******************************************************************************
void loop()
{
  //*******************************
  checkSwitches();

  //*******************************
  //Check whether it has been long enough
  if (ledState == HIGH && millis() - timeChanged > delayTime)
  {
    timeChanged = millis();                 //Store the current time as the time we last changed LEDS

    for (int i = 5; i < 10; i++)
    {
      digitalWrite(ledPin[i], LOW);         // turn all the LEDs off
    }

    digitalWrite(ledPin[currentLED], HIGH); //light the current LED

    //*******************************
    currentLED = currentLED - dir;          //increase the direction value (up or down)
    
    if (currentLED == 9)
    {
      dir = 1;
    }

    if (currentLED == 5)                    //If we are at the end of a row, change direction
    {
      dir = -1;
    }
  }

  //*******************************
  if (ledState == HIGH && millis() - timeChanged1 > delayTime)
  {
    for (int i = 0; i < 5; i++)
    {
      digitalWrite(ledPin[i], LOW);
    }

    digitalWrite(ledPin[currentLED1], HIGH);
    
    //*******************************
    currentLED1 = currentLED1 + dir1;
    
    if (currentLED1 == 4)
    {
      dir1 = -1;
    }

    if (currentLED1 == 0)
    {
      dir1 = 1;
    }

    timeChanged1 = millis();
  }

} //END of loop()

//*******************************************************************************
void checkSwitches()
{
  //*******************************
  if (millis() - switchMillis < 50)
  {
    //not time to read the switches
    return;
  }

  switchMillis = millis();

  //*******************************
  byte reading = digitalRead(PushButton);
  
  if (reading == lastButtonState)
  {
    return;
  }

  //update to the new state
  lastButtonState = reading;

  //*******************************
  if (reading == HIGH)
  {
    ledState = !ledState;
  }

} //END of  checkSwitches()

Cheers thank you for your response. I've tried your code and it stops the code but it leaves two LED's on

If you are talking about the code offered in post #14, add a for() loop to the checkSwitches() function to turn off LEDs as needed.

Thank you so much!