# Light only half of the LEDs

How can I only let half of the LEDs light up, for example, LED 1 on, LED 2 off, LED 3 on...and so on.

I did it this way (this example fades also all LEDs on and off)

``````   int x = 1;
for (int i = 0; i > -1; i = i + x){
leds = CRGB(i,0,0);
leds = CRGB(0,i,0);
leds = CRGB(i,0,0);
leds = CRGB(0,i,0);
Serial.println(i);
FastLED.show();
if (i >= 55) x = -1;             // switch direction at peak
delay(10);
}
``````

but that is not so nice if you have 100 LEDs or so.

• Change `int x = 1;` to `int x = 2;`. Dec 17 '15 at 19:31

I don't know much about neopixel but as a general coding practice if you want to do something for every other object, you use the modulus operator like so

``````if(i % 2 == 0)
{
//i is even
//off
}
else
{
//i is odd
//on
}
``````

So just wrap your current for loop in another one and to the above with the iterating variable.

``````int x = 1;
for (int i = 0; i > -1; i = i + x){
// using modulo
if(i % 2 == 0){
leds[i] = CRGB(i,0,0)}else{
leds[i] = CRGB(0,i,0);}

Serial.println(i);
FastLED.show();
if (i >= 55) x = -1;             // switch direction at peak
delay(10);
}
``````

or

``````int x = 2;
for (int i = 0; i > -1; i = i + x){
leds[i] = CRGB(i,0,0);

Serial.println(i);
FastLED.show();
if (i >= 55) x = -2;             // switch direction at peak
delay(10);
}
``````

This is, for the most part, exactly what others have said. However, nobody has managed to actually use a `for` loop properly; they've carried forward your inefficiencies for some unknown reason.

The whole point of a `for` loop is to execute a block of code a predetermined number of times. In your code, you create a loop that loops for a ridiculous length (65,536 times) but each time checks how many loops there have been.

Once the `for` loop has been constructed properly there are two approaches:

``````int NUMBER_LEDS = 100;      // you only ever have to change this
// if the number of LEDs change - and it's really
// easy to find if you put it at the start of

for (int x = 0; x < NUMBER_LEDS; x++)
{
if (x % 2 == 0)
leds[x] = CRGB(x,0,0);
else
leds[x] = CRGB(0,x,0);
Serial.println(x);
FastLED.show();
delay(10);
}
``````

The above code is fine if you just want two LED subsets. If you want more than two, you can use a `switch/case` statement:

``````int NUMBER_LEDS = 100;      // you only ever have to change this
// if the number of LEDs change - and it's really
// easy to find if you put it at the start of
int NUMBER_SETS = 4;        // this will give four groups.

for (int x = 0; x < NUMBER_LEDS; x++)
{
switch (x % NUMBER_SETS)
{
case (0):
{
leds[x] = CRGB(0,0,0);
break;
}
case (1):
{
leds[x] = CRGB(x,0,0);
break;
}
case (2):
{
leds[x] = CRGB(0,x,0);
break;
}
case (3):
{
leds[x] = CRGB(0,0,x);
break;
}
}
Serial.println(x);
FastLED.show();
delay(10);
}
``````

You don't have to write a `case` block for every outcome of `x%2`; you might want a certain group of LEDs to stay on for two iterations, for example. More information on `switch/case` can be found here, towards the bottom of the page.

I recommend you read this article on Operators in C++, starting from "Compound Assignement". It helps make code more readable and can considerably decrease the amount of typing you have to do - `i = i + 1` neatly becomes `i++`.