Running LEDs with 3 sequences controlled with push button

Hi everyone!

Im currently working about our activity on running
7 leds controlled with a push button. There are 3 sequencies, first sequence is that the leds are off, then if the
button is pressed, second sequence of running leds starts, then third is that another running sequence of leds, then if the button is pressed again, it will go back to the first sequence where the leds are off, and so on and so forth.
The code i made does not function properly. Please make some corrections.

int ctr=0;
const int buttonPin = 2;  

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 5;    // the debounce time; increase if the output flickers

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() 
{
  
  pinMode(buttonPin, INPUT);
  
  for(int led=6; led<14; led++){
    pinMode(led,OUTPUT);
  }
}


void loop() 
{

  
  
  int buttonState = digitalRead(buttonPin);
  
    if (buttonState != lastButtonState) 
    {
      // if the state has changed, increment the counter
      if (buttonState == HIGH && lastButtonState == LOW) 
      {
        
      if(ctr==0)
      {
       ctr++;
      }
      else if(ctr>2)
      {
        ctr=0;
      }
      
      
     
      } 
    
    }
  
lastButtonState = buttonState;
   
  
  switch(ctr)
  {
  
  case 1:
  {
        for(int led=13; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        }
    
        for(int led=13; led>5; led--)
        {
        digitalWrite(led,LOW);
        }
        delay(5);
    
        int led=13;
        digitalWrite(led,HIGH);
        delay(50);
  
        for(int led=12; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        } 
        for(int led=13; led>11; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);
  
        for(int led=11; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        }
        for(int led=13; led>10; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);

        for(int led=10; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        }
        for(int led=13; led>9; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);

        for(int led=9; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        }
        for(int led=13; led>8; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);

        for(int led=8; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        }
  
        for(int led=13; led>7; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);

  
        for(int led=7; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        }
        for(int led=13; led>6; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);
  
        for(int led=6; led>5; led--)
        {
        digitalWrite(led,HIGH);
        delay(5);
        digitalWrite(led,LOW);
        delay(5);
        }
        {
        for(int led=13; led>5; led--)
        {
        digitalWrite(led,HIGH);
        }
        delay(5);
        }
        for(int led=13; led>5; led--)
        {
        digitalWrite(led,LOW);
        delay(5);
        }
    }break;

//--------------------------
  case 2:
{
        for(int led=13; led>5; led--)
        {
        digitalWrite(led,HIGH); 
        delay(5);
        digitalWrite(led,LOW); 
        delay(5);
        }
        delay(5);

        
        {
        for(int led=6; led<14; led++)
        {
        digitalWrite(led,HIGH);    
        }
        delay(10);
  
        for(int led=6; led<14; led++)
        {
        digitalWrite(led,LOW);    
        }
        delay(10);
        }
        {
        for(int led=6; led<14; led++)
        {
        digitalWrite(led,HIGH);    
        }
        delay(10);
  
        for(int led=6; led<14; led++)
        {
        digitalWrite(led,LOW);    
        }
        delay(10);
        }
        {
        for(int led=6; led<14; led++)
        {
        digitalWrite(led,HIGH);    
        }
        delay(10);
  
        for(int led=6; led<14; led++)
        {
        digitalWrite(led,LOW);    
        }
        delay(10);
        }
  }break;
  
  case 0:
    {
      for(int led=6; led<14; led++)
      {
        digitalWrite(led,LOW);  
      }
      
    }break;
  }

}

For it to work, drop all the delay's and se the Blink without delay example. Now you're just blocking the Arduino in doing anything else.

Also, there is no debouncing for the switch. Make it easy for yourself, use the Bounce2 library ;)

Also, "does not function properly" is like the worst possible explanation of what happens...

Thank you for the comment, but I dont know how to use the BlinkWithOutDelay in this set-up. Could you show me how it is done? Thanks :-)

Also, "does not function properly" is like the worst possible explanation of what happens

So what happens that you wish would not happen. A 5mS delay would seem to small to be useful here.

jeevr:
7 leds controlled with a push button.

Please make some corrections.

OK. Let’s start with the easy things: Number of LEDs.

How many LEDs do you actually use?
Connected to which pins?

And how many pins do you set to OUTPUT with this code:

  for(int led=6; led<14; led++){
    pinMode(led,OUTPUT);
  }

jeevr:
The code i made does not function properly.

Next thing would be: Describe how the LEDs would switch on and off, if it “does function properly”!

I’m very bad in guessing how to make something “function properly” while only code is posted that “does not function properly”.

There seems to be a common newbee problem. pinMode(xx, INPUT ); instead of pinMode(xx, INPUT_PULLUP);

I'm assuming the switch is to ground and there is no pullup resistor on the switch. If the switch is to VCC, then you need an external pulldown resistor. In any case, if you use: pinMode(xx, INPUT ); and a single through switch, you must use an external pullup or pulldown resistor. Dwight