NeoPixel with address as a Variable

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
lumisource
 
Posts: 4
Joined: Tue May 06, 2014 12:18 pm

NeoPixel with address as a Variable

Post by lumisource »

Hi,

I was wondering if there was any way to call upon a variable such as 1 neopixel within the code.

For example I want:
strip.setPixelColor(0, 80, 0, 0); to be LED1, and
strip.setPixelColor(1, 80, 0, 0); to be LED2, so on and so forth. I want to be able to tell the Arduino Uno that I want LED1 to turn on and then turn off again (Flash). Is there a simple way in coding to do this? I thought of a couple of different ways, but just making "int LED1 = strip.setPixelColor(0, 80, 0, 0);" didn't seem to work well.

User avatar
chemdoc77
 
Posts: 148
Joined: Mon Jan 28, 2013 9:32 am

Re: NeoPixel with address as a Variable

Post by chemdoc77 »

Hi :

The sketch below will allow you to set each NeoPixel to flash to a specific color. You can change the color and the wait time for each NeoPixel. Be sure to change the number of NeoPixels in the sketch to the number of NeoPixels that you are using. This worked well on my 4 NeoPixel test box.

Enjoy.

Best Regards,
Chemdoc77

Code: Select all



// By Chemdoc77

#include <Adafruit_NeoPixel.h>
   
    #define PIN 6    // 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 w/WS2812 LEDs)
                          // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
                          // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
                          // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
    Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);
    
   

// the setup routine runs once when you press reset:
    void setup() {
    strip.begin();
    strip.show();                // initialize all pixels to 'off'    
  
}

// the loop routine runs over and over again forever;
void loop() {
  
  cd77onepixelonecolor(0, strip.Color(50, 0, 0), 100); // pixel #0 blinks red
  cd77onepixelonecolor(1, strip.Color(0, 0, 50), 100);
  cd77onepixelonecolor(2, strip.Color(50, 0, 0), 100);
  cd77onepixelonecolor(3, strip.Color(0, 0, 50), 100);
  cd77onepixelonecolor(2, strip.Color(0, 0, 50), 100);
  cd77onepixelonecolor(0, strip.Color(0, 50, 0), 100);
   
}

// Functions for loop -----------------------------------------------------------------

// blink one NeoPixel with unique color by Chemdoc77
 //loop command:  cd77onepixelonecolor(1, strip.Color(50, 0, 0), 100); // NeoPixel number 1 blinks red once.
void  cd77onepixelonecolor(uint8_t pix, uint32_t c, uint8_t wait) {
         
      strip.setPixelColor(pix, c);  // sets pixel number pix to color c
      strip.show();
      delay(wait);
      strip.setPixelColor(pix, 0); // blanks pixel
      strip.show(); 
      delay(wait);
   
} 



// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

User avatar
PilotC150
 
Posts: 132
Joined: Sun May 04, 2014 9:39 pm

Re: NeoPixel with address as a Variable

Post by PilotC150 »

It's kind of hard to tell exactly what you're trying to do. It seems like you just want a shortcut so you don't have to type "strip.setPixelColor(0, 80, 0, 0)" every time.

If it's because you don't want to have to type the "80, 0, 0" each time, then what you'll want to do is create a function. You'll do something like:

Code: Select all

void SetColor(int pixelNum)
{
   strip.setPixelColor(pixelNum, 80, 0, 0);
}
Then once that's defined, you can call it just by doing:

Code: Select all

 SetColor(1);
 SetColor(2);
You could modify the function to take another parameter that defines if you want it that particular pixel to turn on or off. But obviously you've now hardcoded the color (80, 0, 0). If you want to change the color, you need to take those as parameters, too, and now you've just recreated the setPixelColor function that already exists on the "strip" class.

If this doesn't help, could you describe a bit better what you're trying to accomplish and we can help out more.

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

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