user49729: I am working on a project where a rotary encoder is generating 0 - 359° output and I am feeding that to a 60 Neopixel ring but I need a way to simplify or come up with a better way of a bunch of if-then-else statements.  Here is some sample code.
// NeoPixel Display   
  if ((counter >=3) && (counter <=9))
(
  strip.setPixelColor(0, 255, 0, 0); //set pixel number 1 and set color to red
  strip.show(); //send color to pixel 1 on NeoPixel Ring  
)
else if strip.setPixelColor(0, 0, 0, 0);
        strip.show();

  if ((counter >=10) && (counter <=15))
(
  strip.setPixelColor(1, 255, 0, 0); //set pixel number 2 and set color to red
  strip.show(); //send color to pixel 2 on NeoPixel Ring  
)
else if strip.setPixelColor(1, 0, 0, 0);
        strip.show();

  if ((counter >=16) && (counter <=21))
(
  strip.setPixelColor(2, 255, 0, 0); //set pixel number 3 and set color to red
  strip.show(); //send color to pixel 3 on NeoPixel Ring  
)
else if strip.setPixelColor(2, 0, 0, 0);
        strip.show();


user31105: You don't use the correct syntax. I assume you mean:
// NeoPixel Display   
if ((counter >=3) && (counter <=9))
{
  strip.setPixelColor(0, 255, 0, 0); //set pixel number 1 and set color to red
  strip.show(); //send color to pixel 1 on NeoPixel Ring  
}
else 
{
  strip.setPixelColor(0, 0, 0, 0)
  strip.show();
}

And similar for the other colors.
First you see there are a lot of occurences of calls to setPixelColor and show. We make a function of this, where the color and the red value is parameterized:
void setPixel(int pixelNumber, int red)
{
  strip.setPixelColor(pixelNumber, red, 0, 0);
}

Now we can write your code as
// NeoPixel Display   
if ((counter >=3) && (counter <=9))
{
  setPixel(0, 255);
}
else
{
  setPixel(0, 0);
}

void setPixel(int pixelNumber, int red)
{
  strip.setPixelColor(pixelNumber, red, 0, 0);
  strip.show();
}

And similar for the other colors.
Now we get rid of the if statements. For the first color this will become:
// NeoPixel Display   
setPixel(0, ((counter >= 3) && (counter <= 9)) ? 255 : 0);

Combined with the other colors we get
// NeoPixel Display   
setPixel(0, ((counter >=  3) && (counter <=  9)) ? 255 : 0);
setPixel(1, ((counter >= 10) && (counter <= 15)) ? 255 : 0);
setPixel(2, ((counter >= 16) && (counter <= 21)) ? 255 : 0);

void setPixel(int pixelNumber, int red)
{
  strip.setPixelColor(pixelNumber, red, 0, 0);
  strip.show();
}

However, the conditions can be simplified by adding them into the function. So we get
// NeoPixel Display   
setPixel(0, counter,  3,  9);
setPixel(1, counter, 10, 15);
setPixel(2, counter, 16, 21);

void setPixel(int pixelNumber, int counter, int lowValue, int highValue)
{
  strip.setPixelColor(pixelNumber, 
     ((counter >= lowValue) && (counter <= highValue)) ? 255 : 0, 
     0, 0);
  strip.show();
}

If you need to have more checks if a value is in range you can make it a specific function. This will increase the code, but makes it easier to reuse code. So you will get:
// NeoPixel Display   
setPixel(0, counter,  3,  9);
setPixel(1, counter, 10, 15);
setPixel(2, counter, 16, 21);

void setPixel(int pixelNumber, int counter, int lowValue, int highValue)
{
  strip.setPixelColor(pixelNumber, 
    inRange(counter, lowValue, highValue) ? 255 : 0,
     0, 0);
  strip.show();
}

int inRange(int counter, int lowValue, int highValue)
{
   return ((counter >= lowValue) && (counter <= highValue));
}


49729: <p>I am working on a project where a rotary encoder is generating 0 - 359° output and I am feeding that to a 60 Neopixel ring but I need a way to simplify or come up with a better way of a bunch of <em>if-then-else</em> statements.  Here is some sample code.</p>

<pre><code>// NeoPixel Display   
  if ((counter &gt;=3) &amp;&amp; (counter &lt;=9))
(
  strip.setPixelColor(0, 255, 0, 0); //set pixel number 1 and set color to red
  strip.show(); //send color to pixel 1 on NeoPixel Ring  
)
else if strip.setPixelColor(0, 0, 0, 0);
        strip.show();

  if ((counter &gt;=10) &amp;&amp; (counter &lt;=15))
(
  strip.setPixelColor(1, 255, 0, 0); //set pixel number 2 and set color to red
  strip.show(); //send color to pixel 2 on NeoPixel Ring  
)
else if strip.setPixelColor(1, 0, 0, 0);
        strip.show();

  if ((counter &gt;=16) &amp;&amp; (counter &lt;=21))
(
  strip.setPixelColor(2, 255, 0, 0); //set pixel number 3 and set color to red
  strip.show(); //send color to pixel 3 on NeoPixel Ring  
)
else if strip.setPixelColor(2, 0, 0, 0);
        strip.show();
</code></pre>
 31105: <p>You don't use the correct syntax. I assume you mean:</p>

<pre><code>// NeoPixel Display   
if ((counter &gt;=3) &amp;&amp; (counter &lt;=9))
{
  strip.setPixelColor(0, 255, 0, 0); //set pixel number 1 and set color to red
  strip.show(); //send color to pixel 1 on NeoPixel Ring  
}
else 
{
  strip.setPixelColor(0, 0, 0, 0)
  strip.show();
}
</code></pre>

<p>And similar for the other colors.</p>

<p>First you see there are a lot of occurences of calls to setPixelColor and show. We make a function of this, where the color and the red value is parameterized:</p>

<pre><code>void setPixel(int pixelNumber, int red)
{
  strip.setPixelColor(pixelNumber, red, 0, 0);
}
</code></pre>

<p>Now we can write your code as</p>

<pre><code>// NeoPixel Display   
if ((counter &gt;=3) &amp;&amp; (counter &lt;=9))
{
  setPixel(0, 255);
}
else
{
  setPixel(0, 0);
}

void setPixel(int pixelNumber, int red)
{
  strip.setPixelColor(pixelNumber, red, 0, 0);
  strip.show();
}
</code></pre>

<p>And similar for the other colors.</p>

<p>Now we get rid of the if statements. For the first color this will become:</p>

<pre><code>// NeoPixel Display   
setPixel(0, ((counter &gt;= 3) &amp;&amp; (counter &lt;= 9)) ? 255 : 0);
</code></pre>

<p>Combined with the other colors we get</p>

<pre><code>// NeoPixel Display   
setPixel(0, ((counter &gt;=  3) &amp;&amp; (counter &lt;=  9)) ? 255 : 0);
setPixel(1, ((counter &gt;= 10) &amp;&amp; (counter &lt;= 15)) ? 255 : 0);
setPixel(2, ((counter &gt;= 16) &amp;&amp; (counter &lt;= 21)) ? 255 : 0);

void setPixel(int pixelNumber, int red)
{
  strip.setPixelColor(pixelNumber, red, 0, 0);
  strip.show();
}
</code></pre>

<p>However, the conditions can be simplified by adding them into the function. So we get</p>

<pre><code>// NeoPixel Display   
setPixel(0, counter,  3,  9);
setPixel(1, counter, 10, 15);
setPixel(2, counter, 16, 21);

void setPixel(int pixelNumber, int counter, int lowValue, int highValue)
{
  strip.setPixelColor(pixelNumber, 
     ((counter &gt;= lowValue) &amp;&amp; (counter &lt;= highValue)) ? 255 : 0, 
     0, 0);
  strip.show();
}
</code></pre>

<p>If you need to have more checks if a value is in range you can make it a specific function. This will increase the code, but makes it easier to reuse code. So you will get:</p>

<pre><code>// NeoPixel Display   
setPixel(0, counter,  3,  9);
setPixel(1, counter, 10, 15);
setPixel(2, counter, 16, 21);

void setPixel(int pixelNumber, int counter, int lowValue, int highValue)
{
  strip.setPixelColor(pixelNumber, 
    inRange(counter, lowValue, highValue) ? 255 : 0,
     0, 0);
  strip.show();
}

int inRange(int counter, int lowValue, int highValue)
{
   return ((counter &gt;= lowValue) &amp;&amp; (counter &lt;= highValue));
}
</code></pre>
