problem with LED and array

Hi, I’m noob at arduino

recently I studied about array in arduino

and I can make code about led turns 1,2,3 or 3,2,1

but I can’t make code about 1,2,3,3,2,1 only use one for sentence

Not just copy 3,2,1 code below 1,2,3 code how can I make code?

int button1 = 2;
int led[] = {8,9,10};

void setup(){
for(int i =0; i<3; i++){
  pinMode(led[i], OUTPUT);
}
pinMode(button1, INPUT);
}

void loop(){
int Push1 = digitalRead(button1);

if(Push1 ==1 ) {
for(int i=0; i<3; i++){
  digitalWrite(led[i], HIGH);
  delay(1000);
  digitalWrite(led[i], LOW);
  delay(1000);
}
}

This video is about code what I want to make

have a good day

Please use code tags when you post code or it will not make sense. This is the </> in the edit controls.

To reverse the leds, you write a for loop that goes backwards

for (int i=3; i>=0; --i)
etc

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Sorry, this is my first time to posting code.
Thanks for letting me know

What I want to know is not just reverse. But thank you for replying marco.

Hi,
Try this edit of your code;
As you only have 3 LEDs 1,2,3,2 is all you need to see 1,2,3,2,1,2,3,2,1,2,3 etc as long as the button is pressed.

int button1 = 2;
int led[] = {8, 9, 10};

void setup() {
  for (int i = 0; i < 3; i++) {
    pinMode(led[i], OUTPUT);
  }
  pinMode(button1, INPUT);
}

void loop() {
  int Push1 = digitalRead(button1);

  if (Push1 == 1 ) {
    for (int i = 0; i < 3; i++) {
      digitalWrite(led[i], HIGH);
      delay(1000);
      digitalWrite(led[i], LOW);
      delay(1000);
    }
    digitalWrite(led[1], HIGH);
    delay(1000);
    digitalWrite(led[1], LOW);
    delay(1000);

  }
}

It compiles UNO, IDE 1.8.0 Win7.
But I have not tried it in practice.

How have you got your button wired?
Do you have a 10K pull down resistor on the input pin 2?

Tom… :slight_smile:

This should work but is untested:

int button1 = 2;
int led[] = {8,9,10,10,9,8};        //  This change is very important.

void setup(){
  for(int i =0; i<3; i++){
    pinMode(led[i], OUTPUT);
  }
  pinMode(button1, INPUT);
}

void loop(){
  int Push1 = digitalRead(button1);

  if(Push1 == HIGH) {
    for(int i=0; i<6; i++){        //  This change goes along with the other change.
      digitalWrite(led[i], HIGH);
      delay(1000);
      digitalWrite(led[i], LOW);
      delay(1000);
    }
  }
}

But what is the point? This sounds like an XY problem, or school homework, or a school project. I hope that I did not just hand someone the answer to a homework question.

Notice that digitalRead is documented to return HIGH or LOW. It currently happens to return 1 or 0 but this is not guaranteed. I made a simple change to the code to conform.

The delay function is handy in toy programs and in some test cases, but generally I believe that it should be avoided. I did not avoid it in this case because I was trying to limit the scope of my changes.

It would be nice if kokocom would answer the questions that have been posed.