Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

XRAD'S Beginner neopixel code helper, One neopixel at a time
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

XRAD'S Beginner neopixel code helper, One neopixel at a time

by XRAD on Tue Aug 18, 2020 9:16 pm

So i thought that it would be helpful to describe an easy way to light up ONE or MANY neopixels on a strip. It is not intuitive for beginners to isolate just ONE or every other neopixel or groups of pixels and have them fade or do something cool. I happen to be working on a display that needs exactly this. So here is the code complied from Adafruit tutorials and the Net and minimally modified for ease of use.

Using arrays in the code, and a 30 segment neopixel strip, and a Trinket Feather MO Express, you can see that it is easy to do some cool functions. I just cleared the display and added pauses so that you can follow the functions. You can add any number of switch case or button or motion or whatever sensors. You could get more complex using millis as timers and non blocking functions.

Hope this is useful!

Code: Select all | TOGGLE FULL SIZE
/*
   Do you want to light up just one or several specific neopixels on a strip.
   Use arrays. Each Array can hold 1 or many neopixels. From Adafruit teaching
   examples and the arduino forums. Modified for ease of use by XRAD.
*/

#include <Adafruit_NeoPixel.h> // include the Neopixel Library in this Sketch

#define LED_PIN 9 // Data out to strip

#define LED_COUNT 30 // how many pixels in entire strip


Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN);//num pixels , pin


// define the following neopixel arrays:
int PXL1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // array controlling the first 10 neopixel elements. First in array is '0'
int PXL2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int PXL2B[] = {15};//just one neopixel, middle of the strip in this case
int PXL3[] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
int PXL4[] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39};//> 30 array elements not used
int PXL5[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49};
int PXL6[] = {50, 51, 52, 53, 54, 55, 56, 57, 58, 59};

int delayval = 100; // delaytime if needed

uint32_t magenta = strip.Color(255, 0, 255);// to show how to substitute colors



//bunch of cut n pasted n slightly baked neopixel functions.....

void rainbow0(int wait) {//fade ALL elements in array at same time
  // Hue of first pixel runs 5 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < 10; i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / PXL1[i]); //ARRAY PXL1(i)
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(PXL1[i], strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(wait);  // Pause for a moment
  }
}

void rainbow1(int wait) {//fade cycle through array elements
  // Hue of first pixel runs 5 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < 10; i++) { // For each pixel in ARRAY (10)...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / 10);// For each pixel in ARRAY (10)...
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(PXL3[i], strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(wait);  // Pause for a moment
  }
}

void rainbow2(int wait) {//fade cycle through ONE element in array
  // Hue of first pixel runs 5 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < 1; i++) { // For each pixel in array (1)...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / 1); // For each pixel in array (1)...
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(PXL2B[i], strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show();
    delay(wait);
  }
}


// 0 to 255, I like blue
void fadeIn() {//all pixels in strip
  uint16_t i, j;//num pixels, Blue brightness
  for (j = 0; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, j);
    }
    strip.show();
    delay(20);
  }
}

// 255 to 0
void fadeOut() {//all pixels in strip
  uint16_t i, j;
  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, j);
    }
    strip.show();
    delay(20);
  }
}


void Larson() {//classic Larson scanner!

  int pos = 0, dir = 1; // Position, direction of "eye"

  for (int i = 0; i < 300; i++) {
    int j;
    // Draw 5 pixels centered on pos.  setPixelColor() will clip any
    // pixels off the ends of the strip, we don't need to watch for that.
    // you could modify to have all pixels display, and the 'eye' can run
    // through the lit pixels
    strip.setPixelColor(pos - 2, 0x100000); // Dark red
    strip.setPixelColor(pos - 1, 0x800000); // Medium red
    strip.setPixelColor(pos    , 0xFF3000); // Center pixel is brightest
    strip.setPixelColor(pos + 1, 0x800000); // Medium red
    strip.setPixelColor(pos + 2, 0x100000); // Dark red

    strip.show();
    delay(30);

    // Rather than being sneaky and erasing just the tail pixel,
    // it's easier to erase it all and draw a new one next time.
    for (j = -2; j <= 2; j++) strip.setPixelColor(pos + j, 0);

    // Bounce off ends of strip
    pos += dir;
    if (pos < 0) {
      pos = 1;
      dir = -dir;
    } else if (pos >= strip.numPixels()) {
      pos = strip.numPixels() - 2;
      dir = -dir;
    }
  }
}



void setup() {
  strip.begin();
  strip.clear();
  strip.show();
}

void loop() {

  strip.fill(magenta, 6, 15);//one way to add a color
  //strip.fill(strip.Color(100,100,100),15, LED_COUNT);//another way to add color
  strip.show();
  delay(1000);

  strip.clear();
  strip.show();
  delay(1000);

  Larson();
  strip.clear();
  strip.show();
  delay(1000);

  fadeIn();
  fadeOut();
  strip.clear();
  strip.show();
  delay(1000);

  rainbow0(10);
  strip.clear();
  strip.show();
  delay(2000);

  rainbow1(10);
  strip.clear();
  strip.show();
  delay(2000);

  rainbow2(10);
  strip.clear();
  strip.show();
  delay(2000);


//just turning on selected array in sequence with neopixel colors
  for (int i = 0; i < 10; i++) { //each selected array has 10 neopixel elements
    strip.setPixelColor(PXL1[i], strip.Color(139, 0, 139)); // array number 1 is magenta
    strip.setPixelColor(PXL2[i], strip.Color(255, 255, 0)); // array number 2 is yellow
    strip.setPixelColor(PXL3[i], strip.Color(255, 255, 255)); // array number 3 is white
    // strip.setPixelColor(PXL4[i], strip.Color(0, 255, 0)); // array number 4 is green
    // strip.setPixelColor(PXL5[i], strip.Color(0, 0, 255)); // array number 5 is blue
    // strip.setPixelColor(PXL6[i], strip.Color(255, 0, 0)); // array number 6 is red

    strip.show();
    delay(delayval);
  }

  for (int i = 10; i > -1; i--) { // Off
    strip.setPixelColor(PXL1[i], strip.Color(0, 0, 0));
    strip.setPixelColor(PXL2[i], strip.Color(0, 0, 0));
    strip.setPixelColor(PXL3[i], strip.Color(0, 0, 0));
    // strip.setPixelColor(PXL4[i], strip.Color(0, 0, 0));
    // strip.setPixelColor(PXL5[i], strip.Color(0, 0, 0));
    // strip.setPixelColor(PXL6[i], strip.Color(0, 0, 0));

    strip.show();
    delay(delayval);
  }

  strip.clear();
  strip.show();
  delay(2000);
}

XRAD
 
Posts: 591
Joined: Sat Nov 19, 2016 3:28 pm

Please be positive and constructive with your questions and comments.