Neopixels

Neopixel is Adafruit’s name for a type of RGB-leds that can communicate with each other, making it fairly easy to set up a bunch and control them without needing as many cables as you would with conventional LEDs.

In their section for tutorials, Adafruit has a comprehensive tutorial on Neopixels in their Überguide, including how to add more power if you end up with more lights than the Arduino can feed.

Here is a bare minimum example of blinking a Neopixel:

#include <Adafruit_NeoPixel.h>
 
int pixelPin = 3;
int amountNeopixels = 1;
int interval = 500;
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products)
//   NEO_KHZ400  400 KHz (classic 'v1' FLORA pixels)
//   NEO_GRB Pixels wired as GRB bitstream (most NeoPixels)
//   NEO_RGB Pixels wired for RGB bitstream (v1 FLORA)


Adafruit_NeoPixel strip = Adafruit_NeoPixel(amountNeopixels, pixelPin, NEO_GRB + NEO_KHZ800);
 
void setup()
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop()
{
  //setPixelColor(index, R, G, B); //0-255
  strip.setPixelColor(0, 255, 255, 255);
  strip.show();

  delay(interval);
 
  strip.setPixelColor(0, 0, 255, 0);
  strip.show();
 
  delay(interval);
}

First and foremost you need to download Adafruits Neopixel-library. Menu choice “sketch”→ include library → manage libraries. Search and install Adafruit Neopixel.

Early in the code above you create a representative object called “strip” which we use to control our length of Neopixels. In this example it is only one, but we could control a lot more. In the IxL windows there is a stretch of 300 Neopixels. Using that many, though, means that they also need external powersources.

Once we have our “strip” we can start defining how we want the Neopixel to light up next. The command strip.setPixelColor() needs four parameters. Pixel-index, and levels of red, green, and blue. Pixel-index is just which RGB-led in order we are configuring. Remember that a computer starts counting on 0, so the first one is 0, the second is 1 and so on. The levels of color is a number ranging from 0 to 255. In the example we are only setting up one Neopixel, but we can follow that up with any number of configurations. Each one with a new call of strip.setPixelColor(). If you want some randomness to your color, try the command random() as your color level.

Once we’ve configured the lights we use strip.show() to apply the colors and this is when they actually light up. This combination of setPixelColor() and show() makes it possible for us to define a complex color scheme before showing it.

The example uses these commands to set up the Neopixel to first show white, using delay() it pauses so we can see it, then setting the Neopixel to show green, and then another pause.

Progression

But what if you want to fade up? Or down?

void loop()
{
  //setPixelColor(index, R, G, B); //0-255
 
  for(int i=0; i<255; i++)
  {
    strip.setPixelColor(0, i, i, i);
    strip.show();
    delay(10);
  }
 
  delay(500);
 
  for(int i=255; i>0; i--)
  {
    strip.setPixelColor(0, i, i, i);
    strip.show();
    delay(10);
  }
 
  delay(500);
}

For this, we can use the for-loop. A function we’ve touched on in P5 for when we want to do something many times. A for-loop has three defining parts. for(A;B;C)

* Part A is defining a counter variable and setting a start value, in our example it is an integer called i and we set it to have starting value 0.

* Part B is the circumstance that limits the for-loop. In this case, we run while variable i is less than 255.

* Part C is what we do with i each time around. In this example it’s adding one to whatever value i currently have. i++

For the second for-loop we are counting down from 255 to 0 instead. The curly brackets following for-loop are as usual there to show what piece of code is repeated.

As we loop through this 255 times we then use the value our variable i has each round to set the color in strip.setPixelColor. Looping from 0 to 255 and then from 255 to 0.