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));
}