Multiple Pixel Definition

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
donbeu
 
Posts: 10
Joined: Thu Dec 19, 2013 11:55 am

Multiple Pixel Definition

Post by donbeu »

Hello,
I have the LED Pixels, and I need to know how to call several of the Pixels at once.
I am using the function Color, which sets the pixel as a 24 bit color value, found in the strandtest code in the tutorial, but I stripped it down.

The code
strip.setPixelColor( x, Color(255, 0, 0));
Sets the pixel # x with the color value, in this case red. However I want to call more then one pixel with that same color, such as pixels 2, 26, 37, ect.
How do I refer to several pixels at once for this?
Do I define them as a vector, or what? Thanks for the help!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Multiple Pixel Definition

Post by adafruit_support_mike »

Our library only sets the color of a single pixel at a time, but you can iterate over a list of pixels and set them all to the same color that way:

Code: Select all

void setMultiplePixels (uint16_t count, uint16_t *list, uint32_t color) {
	for (int i=0 ; i < count ; i++) {
		strip.setPixelColor( list[ i ], color );
	}
}

setMultiplePxiels( 4, [3, 5, 7, 11], Color(255, 0, 0) );

User avatar
ventolin
 
Posts: 11
Joined: Sat Aug 23, 2014 7:43 pm

Re: Multiple Pixel Definition

Post by ventolin »

I also want to blink several pixels at once and have the code below:

Code: Select all

#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(11, PIN, NEO_GRB + NEO_KHZ800);

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

void loop() {
  setMultiplePixels (4, [3, 5, 7, 10], color(255, 0, 0));
}
void setMultiplePixels (uint16_t count, uint16_t * list, uint32_t color) {
   for (int i=0 ; i < count ; i++) {
      strip.setPixelColor( list[ i ], color );
      strip.show();
   }
}
I get an "expected primary-expression before "[" token" error.
¿What's wrong with my code?

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: Multiple Pixel Definition

Post by pburgess »

C++ doesn't allow declaring lists as parameters like that; the list needs to be explicitly declared with an identifier:

Code: Select all

uint16_t list[] = {3, 5, 7, 10};
void loop() {
  setMultiplePixels (4, list, strip.Color(255, 0, 0));
}
Edit: This syntax does appear to work, give it a shot:

Code: Select all

  setMultiplePixels (4, (uint16_t[]){3, 5, 7, 10}, strip.Color(255, 0, 0));
More edit: also, you can strip.show() outside the 'i' loop.

User avatar
ventolin
 
Posts: 11
Joined: Sat Aug 23, 2014 7:43 pm

Re: Multiple Pixel Definition

Post by ventolin »

Yes pburgess. That's what i was looking for, a list o pixels to blink at the same time.

Heres the code:

Code: Select all

#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800);

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

void loop() {
    setMultiplePixels (10, (uint16_t[]){1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, strip.Color(255, 0, 0));
    setMultiplePixels (10, (uint16_t[]){1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, strip.Color(0, 0, 0));
}

void setMultiplePixels (uint16_t count, uint16_t * list, uint32_t color) {
  for (int i=0 ; i < count ; i++) {
    strip.setPixelColor( list[ i ], color );
    delay(9);
   }
  strip.show();
}
Thank you :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”