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!
Multiple Pixel Definition
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Multiple Pixel Definition
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) );
- ventolin
- Posts: 11
- Joined: Sat Aug 23, 2014 7:43 pm
Re: Multiple Pixel Definition
I also want to blink several pixels at once and have the code below:
I get an "expected primary-expression before "[" token" error.
¿What's wrong with my code?
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();
}
}
¿What's wrong with my code?
- pburgess
- Posts: 4161
- Joined: Sun Oct 26, 2008 2:29 am
Re: Multiple Pixel Definition
C++ doesn't allow declaring lists as parameters like that; the list needs to be explicitly declared with an identifier:
Edit: This syntax does appear to work, give it a shot:
More edit: also, you can strip.show() outside the 'i' loop.
Code: Select all
uint16_t list[] = {3, 5, 7, 10};
void loop() {
setMultiplePixels (4, list, strip.Color(255, 0, 0));
}
Code: Select all
setMultiplePixels (4, (uint16_t[]){3, 5, 7, 10}, strip.Color(255, 0, 0));
- ventolin
- Posts: 11
- Joined: Sat Aug 23, 2014 7:43 pm
Re: Multiple Pixel Definition
Yes pburgess. That's what i was looking for, a list o pixels to blink at the same time.
Heres the code:
Thank you :)
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();
}
Please be positive and constructive with your questions and comments.