Having trouble with led patterns

Hi guys, I've been trying to make this code work and it doesn't really answer as I wanted which is kinda disturbing
here's the code:

int led=2;
void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
pinMode(led, OUTPUT);
for (int led=2;led<=6;led=led+1)
{
  delay(1000);
  
  Serial.println(led);
  digitalWrite(led,HIGH);

  }
}

What i wanted is actually to make a "beginner" pattern with my leds and change the output every seconds
Could someone help me point out what I did do wrong?
Thanks!

What's a beginner pastern?

But with:

pinMode(led, OUTPUT);

You set pin 2 as output.

But with

for (int led=2;led<=6;led=led+1)
{
  delay(1000);
 
  Serial.println(led);
  digitalWrite(led,HIGH);

}

You drive pin 2 to 6. But 3 to are not set to output.

Thanks for the answer!
I realized that if i removed int led=2; and changed it to int led; everything works fine
Sorry for bothering haha

I really doubt that...

Well, since the outputs were being incremented thanks to the loop, it actually worked maybe you can find this messy and I apologize for that (i started learning three days ago haha)
I had a new problem since then haha
I explain myself; my project is actually to make a led board for an arcade stick that I made.
The thing I actually intended my led board to do was two simple tasks (I guess):
-To light the button when activated only
-When nothing is pressed a led pattern should appear.
For now the pattern isn't a real priority since it is working, the problem is that when i activate the pushbutton, the led gets stuck and the pattern doesn't start over again.
Could you give me a hint on that?
Thanks a lot!

boolean deux; //the boolean used to make the LEDs go forth and back
boolean pression; //Not used for now
int bouton = 13;
int led;
void setup() {
  // I set the boolean to true to get the pattern started at plug
  deux = true;
  Serial.begin(9600);
  pinMode(bouton, INPUT_PULLUP);
}

void loop() {
  int lk = digitalRead(bouton);
  Serial.println(lk);
  while ( lk == LOW)  // The idea here is that when the button (lk) is pressed, the led lights up
  {


    digitalWrite(2, HIGH);
    led == 0;
   
 if (lk == HIGH)  //The idea here is to get the LED pattern started since the button isn't pressed anymore
  {
    digitalWrite(2, LOW);
    led == 2;
    deux == true;
    break;
  }
 delay(50);
  }

  // led pattern
 if (deux) {
    for (int led = 2; led <= 6; led ++)
    {

      pinMode(led, OUTPUT);
      Serial.println(led);
      digitalWrite(led, HIGH);
      delay(50);
      digitalWrite(led, LOW);
    }
    deux = false;
    if (deux == false)
    {

      for (int led = 6; led >= 2; led  --)
      {
        pinMode(led, OUTPUT);
        Serial.println(led);
        digitalWrite(led, HIGH);
        delay(50);
        digitalWrite(led, LOW);
      }
      deux = true;
    }
  }

}

the led gets stuck

You need to read the value of the button into the variable 1k inside the while loop otherwise you will be stuck in that loop and nothing can change.

It's indeed a mess. Why do you call the variable for direction 2? UI get you might want to do it in your mother language but two? And led only weirdly defines the start of the led's you use.

What I would make of it including comments:

#include <Bounce2.h> //just makes handeling buttons dead easy

const byte ButtonPin = 13; //define the button pin
const byte LedPins[] = {2, 3, 4, 5, 6}; //define all pins for the leds
const byte NrLeds = sizeof(LedPins); //dynamically define the number of leds

const unsigned int LedInterval = 50; //really that fast? Time between leds

Bounce button; //button object from Bounce2
bool ledDirection = true; //remember the direction
byte currentLed = 255; //255 doubles as off
unsigned long ledMillis;

void setup(){
  Serial.begin(9600);
  button.attach(ButtonPin, INPUT_PULLUP);
  
  //setup all led pins
  for(byte i = 0; i < NrLeds; i++){
    pinMode(LedPins[i], OUTPUT);
  }
}

void loop(){
  //reacting on button presses
  button.update();
  if(button.fell()){
    //if off
    if(currentLed == 255){
      currentLed = 0;  //start at the first led
    }
    //if on
    else{
      //stop running
      currentLed = 255;
      
      //tun off all led's
      for(byte i = 0; i < NrLeds; i++){
        digitalWrite(LedPins[i], LOW);
      }
    }
  }
  
  //if not off and t's time
  if((currentLed != 255) && (millis() - ledMillis >= LedInterval)){
    ledMillis = millis();
    
    //turn off current led
    digitalWrite(LedPins[currentLed], LOW);
    
    //if forward
    if(ledDirection){
      //if not at the last led
      if(currentLed < NrLeds){
        //just go to next led
        currentLed++;
      }
      //if at the last led, reverse direction
      else{
        ledDirection = false;
      }
    }
    //if bacwards
    else{
      //if not at the first led
      if(currentLed > 0){
        //just go to previous led
        currentLed--;
      }
      //if at the last led, reverse direction
      else{
        ledDirection = true;
      }
    }
    
    //turn on next led
    digitalWrite(LedPins[currentLed], HIGH);
  }
}

I did make the assumption you want to stop the running when you press the button again and turn off all leds. And do you really want to loop that fast!?

Hi, sorry didn't have time to answer until now.
So i have found out thanks to this comment how to fix my previous problem

You need to read the value of the button into the variable 1k inside the while loop otherwise you will be stuck in that loop and nothing can change

.
So I fixed but then i saw the code septillion sent wich didn't work because

initializer fails to determine size of 'NrLeds'

and I couldn't fix it since I did not understand where the problem was.
So I continued with the code you guys were helping me with and it seems 3 is the magic number hahaha
Let me explain, everything works fine but there is a last annoyance here, I cant get several buttons pushed at the same time and get the same amount of leds to light up (annoying haha).
Could any of you give me a hint on how I should do to fix this?

boolean patternforth; //the boolean used to make the LEDs go forth and back
boolean pression; //When a button is pressed
int lp = 2; 
int mp = 3;
int hp = 4;
int led;


void setup() {
  // I set the boolean to true to get the pattern started at plug
  patternforth = true;
  pinMode(9, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600);
  pinMode(lp, INPUT_PULLUP);
  pinMode(mp, INPUT_PULLUP);
  pinMode(hp, INPUT_PULLUP);
}

void loop() {


  while ( digitalRead(lp) == LOW)  // The idea here is that when the button (lk) is pressed, the led lights up
  { int lpon = digitalRead(lp);


    digitalWrite(12, HIGH);
    led == 0;
    pression == true;
    if (lp == HIGH)  //The idea here is to get the LED pattern started since the button isn't pressed anymore
    {
      digitalWrite(12, LOW);
      pression == false;
      patternforth == true;
      delay (500);      // I'm tryng to make the pattern after 500ms after the button is pressed but it doesn't seem to work for now

    }

  }




  while ( digitalRead(mp) == LOW)  // The idea here is that when the button (mp) is pressed, the led lights up
  { int mpon = digitalRead(mp);


    digitalWrite(11, HIGH);
    led == 0;
    pression == true;
    if (mp == HIGH)  //The idea here is to get the LED pattern started since the button isn't pressed anymore
    {
      digitalWrite(11, LOW);
      pression == false;
      patternforth == true;
      delay (500);  // I'm tryng to make the pattern after 500ms after the button is pressed but it doesn't seem to work for now
    }

  }


  while ( digitalRead(hp) == LOW)  // The idea here is that when the button (hp) is pressed, the led lights up
  { int hpon = digitalRead(hp);


    digitalWrite(10, HIGH);
    led == 0;
    pression == true;
    if (hp == HIGH)  //The idea here is to get the LED pattern started since the button isn't pressed anymore
    {
      digitalWrite(10, LOW);
      pression == false;
      patternforth == true;
      delay (500);  // I'm tryng to make the pattern after 500ms after the button is pressed but it doesn't seem to work for now

    }

  }
  
  // led pattern
  if (pression == false) {
    for (int led = 8; led <= 12; led ++)
    {

      Serial.println(led);
      digitalWrite(led, HIGH);
      delay(50);
      digitalWrite(led, LOW);
      

    }
    patternforth = false;
    if (patternforth == false)
    {

      for (int led = 12; led >= 8; led  --)
      {

        Serial.println(led);
        digitalWrite(led, HIGH);
        delay(50);
        digitalWrite(led, LOW);
       
        
      }
      patternforth = true;
    }

  }

}

Thanks a lot for your help guys by the way I really appreciate that you took time for this

As soon as one button is pressed it gets locked into the while loop and it will not come out of the while loop until that button is released. So yes only one LED will light up at a time.

You need to structure your code better, so that each time through the loop you read all the push buttons, then use if statements to determine if you want to turn that light on or off, for all three buttons.

I indeed wrote it freehand aka did not compile/test it. But my errors where minor and with all the comments not that hard to find.

I did edit my post to include the small fixes. It compiles now. I don't say it has no error but it at least gives you a direction of thinking about the problem :slight_smile: