1

I am as new as new can be to the Arduino world and have been playing around with them. I have a few "Arduino Uno" boards that I purchased and have successfully loaded sketches to them. However I am having problems figuring out how to write code in a simple manner.

I am using a LED strip of WS2812 LEDs (similar to this). Here is what I would like to achieve:

I would like to be able to light "lets say" LEDs 1, 3, 5, 7. Then light LEDs 2, 4, 6, 8 or any other combination of "multiple LEDs", by that I mean maybe LEDs 0, 14, 15, 36, 37, 52, 54. I am sure you get the idea.

Can I write a single line code to light these LEDs or do I have to write out each line.

Sorry if this is a simplistic question, but I have searched and have not seen anything that may accomplish this, or I just over looked it.

Thanks in advance for your help

5
  • Oh I guess I should say they are strip led(s) ws2812
    – Weeman
    Nov 25 '18 at 3:54
  • 3
    please post code
    – guyd
    Nov 25 '18 at 4:10
  • Please edit your question to add the important information that you're using WS2812. It's better to have all the info in the question rather than expecting that people will read the comments.
    – per1234
    Nov 25 '18 at 4:22
  • Most times you would set the states of the LEDs in a for loop, while checking the rule with which they should light up. Please state clearly the rule behind this
    – chrisl
    Nov 25 '18 at 9:43
  • if you want to use a single command to light multiple LEDs, then you will have to write a function that accepts multiple parameters ..... each of the parameters would be the LED number to light
    – jsotola
    Nov 25 '18 at 17:27
2

I have to say that I do not have any experience with the ws2812 led strip but basically it is used with the Adafruit_Neopixel library where you have the function pixel_obj.setPixelColor(pixel_number,pixel_obj.Color(R,G,B) and the function pixel_obj.show(). Now you basically want to make the code in void loop() clean and there is a wide choice of possibilities and techniques to use. The simplest is not to do it, but if you want you can write a library to do it or write a function that takes the number of the pins and light 'em up (all with the same color), like this
void light_pixel(int pixels[],int Red,int Green,int Blue){ for(int i=0;i<NUMPIXELS;++i){ pixel_obj.setPixelColor(pixels[i],pixel_obj.Color(Red,Green,Blue)); } pixel_obj.show(); }
This works assuming you have declared pixel_obj in the outermost scope, and you will call the function like this:

#include <Adafruit_NeoPixel.h>
#define PIN //Whatever you pin is
#define NUMPIXELS //Thenumber of the pixels in your strip
Adafruit_NeoPixel pixel_obj = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// You may have to modify the third parameter Rationale: You didn't 
// specified anything about your led strip
void setup(){
pixel_obj.begin();
pinMode(PIN, OUTPUT);
}
void light_pixel(int pixels[],int Red, int Green, int Blue){
for(int i=0;i<NUMPIXELS && pixels[I]!=-1 ;++i){
    pixel_obj.setPixelColor(pixels[i],pixel_obj.Color(Red,Green,Blue));
}
pixel_obj.show();
}
void loop(){
int my_pixels[NUMPIXELS]={1,3,5,7,-1}; // Using -1 as terminator
// Initialize the array . Disclaimer: this is not optimised and you might use
// new opearator or better pass the reference of a vector to the function so
// that you can modify the range of the vector and have a more concise function
light_pixel(my_pixels,255,255,255);
delay(1000);
light_pixel(my_pixels,0,0,0);
}

Then you can change the number of my_pixels with a smile array assignment This function is just a sample to get the job done but I'd recommend you to study further the C/C++ programming language in general in order to write better code
Good Luck!

2
  • 3
    sizeof(pixels)/sizeof(*pixels) won't work here, as at this point pixels is a pointer, not an array. Nov 25 '18 at 13:31
  • @EdgarBonet This should work, I just set a fixed value for both the array sizes Nov 26 '18 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.