Optimizing the arduino led code

Hi I'm working to optimize my code and I wonder is there anyway I can use the condition in my code. I've been looking online about this but all I've seen so far is I have to add a input to my device to be able to use if, for and while condition.
This is my code

//declare pin
 int LED1=13;
 int LED2=12;
 int LED3=11;
 int LED4=10;
 int LED5=9;
 int LED6=8;
 int LED7=7;
 int LED8=6;
 int sensor=A0;
void setup()
{
  Serial.begin(9600);
  //initialize the output
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LED4,OUTPUT);
  pinMode(LED5,OUTPUT);
  pinMode(LED6,OUTPUT);
  pinMode(LED7,OUTPUT);
  pinMode(LED8,OUTPUT);
  //initialize the output
}
void loop()
{
  //turn on LED lights from left to right
  
  digitalWrite(LED1, HIGH);    // turn on LED1 
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, HIGH);    // turn on LED2
  delay(100);                  // wait for 100ms       
  digitalWrite(LED3, HIGH);    // turn on LED3 
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, HIGH);    // turn on LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, HIGH);    // turn on LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, HIGH);    // turn on LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, HIGH);    // turn on LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, HIGH);    // turn on LED8
  delay(100);                  // wait for 100ms
  digitalWrite(LED1, LOW);     // turn off LED lights
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);     // turn off LED2
  delay(100);                  // wait for 100ms
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);     // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);     // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);     // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);     // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);     // turn off LED8
  delay(500);                  // wait for 500ms
  //turning on for only odd number LED lights
  digitalWrite(LED1, HIGH);    // turn on LED1 
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);    // turn off LED2
  delay(100);                  // wait for 100ms       
  digitalWrite(LED3, HIGH);    // turn on LED3 
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);    // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, HIGH);    // turn on LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);    // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, HIGH);    // turn on LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);    // turn off LED8
  delay(100);				  //wait for 100ms
  digitalWrite(LED1, LOW);     // turn off LED lights
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);     // turn off LED2
  delay(100);                  // wait for 100ms
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);     // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);     // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);     // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);     // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);     // turn off LED8
  delay(500);                  // wait for 500ms
  //turn on for only even number of LED lights
  digitalWrite(LED1, LOW);    // turn off LED1 
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, HIGH);    // turn on LED2
  delay(100);                  // wait for 100ms       
  digitalWrite(LED3, LOW);    // turn off LED3 
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, HIGH);    // turn on LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);    // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, HIGH);    // turn on LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);    // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, HIGH);    // turn on LED8
  delay(100);                  // wait for 100ms
  digitalWrite(LED1, LOW);     // turn off LED lights
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);     // turn off LED2
  delay(100);                  // wait for 100ms
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);     // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);     // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);     // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);     // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);     // turn off LED8
  delay(500);                  // wait for 500ms
  //turn on 4 LED lights only starting from the left
  digitalWrite(LED1, HIGH);    // turn on LED1 
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, HIGH);    // turn on LED2
  delay(100);                  // wait for 100ms       
  digitalWrite(LED3, HIGH);    // turn on LED3 
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, HIGH);    // turn on LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);     // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);     // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);     // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);     // turn off LED8
  delay(100);                  // wait for 100ms
  digitalWrite(LED1, LOW);     // turn off LED lights
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);     // turn off LED2
  delay(100);                  // wait for 100ms
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);     // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);     // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);     // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);     // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);     // turn off LED8
  delay(500);                  // wait for 500ms
  //turn on 4 LED lights only starting from the right
  digitalWrite(LED1, LOW);     // turn off LED1
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);     // turn off LED2
  delay(100);                  // wait for 100ms
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);     // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, HIGH);    // turn on LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, HIGH);    // turn on LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, HIGH);    // turn on LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, HIGH);    // turn on LED8
  delay(100);
  digitalWrite(LED1, LOW);     // turn off LED lights
  delay(100);                  // wait for 100ms
  digitalWrite(LED2, LOW);     // turn off LED2
  delay(100);                  // wait for 100ms
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 100ms
  digitalWrite(LED4, LOW);     // turn off LED4
  delay(100);                  // wait for 100ms
  digitalWrite(LED5, LOW);     // turn off LED5
  delay(100);                  // wait for 100ms
  digitalWrite(LED6, LOW);     // turn off LED6
  delay(100);                  // wait for 100ms
  digitalWrite(LED7, LOW);     // turn off LED7
  delay(100);                  // wait for 100ms
  digitalWrite(LED8, LOW);     // turn off LED8
  delay(500);                  // wait for 500ms
}

There are a few things to mention, but the first thing you should read up on is for() loops.

I think you would benefit from googling some projects similar to the kind you are looking to accomplish.

Begin learning to program by reading code, line by line, and trying to understand what is being expressed.

There are also basic examples right in the IDE. They may be both baffling and seemingly trivial at once, but it is a certainty that real programs are composed of small steps, any one of which can be thoroughly understood.

Larger programs need to be structured, reading programs can give you an idea of how that goes.

Your code above represents knowledge of the alphabet, maybe even words. Now on to sentences, paragrpaphs, chapters and soon you can write your own. Novel.

a7

1 Like

You have written a program that works! That is a good start. Sometimes, it can be actually sufficient to write out all the steps individually, as you did. Problem solved. Task implemented.

Now, it sounds like you want to write code that says the same thing, but uses fewer lines.

Flow control is a coding concept that you can use for this purpose. You mention "for", which is one kind of flow control. A "for loop" seems like a good fit for what your code does.

Begin first by looking at an example of a "for loop" provided by one of the good online schools, such as W3Schools.

I will modify the example to match what your setup() function does. It is interesting that you count down from pin 13 rather than up from pin 6. That is OK. Code can handle it! The comments in the code below will show how.

int pinNumber; // you need only one integer variable

void setup() {

  // start Serial for printing "debug" messages
  Serial.begin(9600);
  // some boards need the following magic line
  // before they are ready to print stuff
  while (!Serial) delay(10);
  
  // use a for loop to set the pin mode to output
  // for pins from 13 down to 6 in decreasing order

  // The loop control statements would normally be written
  // on one line, with some math shortcuts, like this:
  // for (pinNumber = 13; pinNumber > 5; pinNumber--)

  // I am going to spread out the control statements
  // below, so you can see in detail what is going on.
  // Do please study the example at W3Schools also,
  // until you can see how they are similar,
  // and how they are different.
  // By the way, "cout" in the W3Schools example does the same
  // thing that Serial.print() does here, more or less.

  for (
    // start with pin number 13
    pinNumber = 13; 

    // stop after pin number 6    
    pinNumber > 5;

    // subtract 1 from pin number each time
    pinNumber = pinNumber - 1) 

  {
    // set the pin mode
    pinMode( pinNumber, OUTPUT);

    // send a "debug" message to Serial
    // reporting what just happened
    Serial.print("Pin number ");
    Serial.print(pinNumber);
    Serial.println(" was set to OUTPUT mode.");

    // ... and that's it!
  } // end for loop. It will repeat for the pins 12 down to 6
} // end setup

If you will open the Serial Monitor in the Arduino IDE you can view the output of the setup() function as it runs on an Arduino connected to your computer.
When all goes well, the output will look like this:

Pin number 13 was set to OUTPUT mode.
Pin number 12 was set to OUTPUT mode.
Pin number 11 was set to OUTPUT mode.
Pin number 10 was set to OUTPUT mode.
Pin number 9 was set to OUTPUT mode.
Pin number 8 was set to OUTPUT mode.
Pin number 7 was set to OUTPUT mode.
Pin number 6 was set to OUTPUT mode.

Study the loop to see how it was able to repeat the same instructions 8 times, while changing the pin number each time.

Try to apply this flow control technique to your main() loop. You might struggle at first, but if you persist, I predict you will succeed!

1 Like

@IowaDave Thank you so much, I'm starting to get how the condition loop works now. I'm a left hander I usually start with number close to my left finger while typing XD

@alto777 Ok I will thank you so much your info

But before you use a "for" loop, you need to set up an array.

Yeah, I was going there, but the OP was looking like he had a way to go…

Toes first, followed by head.