Particle NeoPixels

From STEAMwiki
Jump to: navigation, search

First, add the Neopixel library (click Libraries on the left, search Neopixel. Add Addafruit Neopixel);

Connect the RED wire to 3v3 and the BLACK wire to GND. Connect the other wire to D2.

There are several ways to control Neopixels. They are individually addressable lights, meaning you can talk to each one on its own. Each is assigned a sequential ID number starting from the beginning of the strip. This code will set a color for each individual pixel.

You can find all kinds of neopixel code examples on the Adafruit site: https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library

Basic Code example:


#include "neopixel/neopixel.h" 

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 10
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup()
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
void loop()
{
  // Show red pixels
  for (int i=0; i<PIXEL_COUNT; i++) {
    strip.setPixelColor(i, 255, 0, 0);
  }
  strip.show();
  delay(1000);

  // Show light blue pixels
  for (int i=0; i<PIXEL_COUNT; i++) {
    strip.setPixelColor(i, 0, 128, 255);
  }
  strip.show();
  delay(1000);
}


Code to cycle through a rainbow:


#include "neopixel/neopixel.h" 

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 10
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup()
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
void loop()
{
  rainbow(20);
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}