Adding to existing led control code

hi guys,

i have a batch of code found online, that the coder posted for others to use, its using adafruits libraries and im using it to control a string of 28 ws2812b led strip.

however, while i have a very basic understanding of the code, and it is VERY basic, i'd like to add a section where the middle 2 leds (14,15) turn on at the beginning of the code and stay on.

the existing code is essentially an led chaser effect running from either end of the stip to the center,

but those 2 center leds need to stay on at all times without turning off as a result of the existing code.

heres the original code:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            2

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      28


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 175; // delay for half a second

void setup() {
  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

while(1){
  for(int i = NUMPIXELS - 1; i >= (NUMPIXELS/2); i--) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,0,255)); // Blue Bright
    pixels.setPixelColor(i+2, pixels.Color(0,0,40)); // Blue Not So Bright
    pixels.setPixelColor((NUMPIXELS-1)-i, pixels.Color(0,0,255)); // Blue Bright
    pixels.setPixelColor((NUMPIXELS-3)-i, pixels.Color(0,0,40)); // Blue Not So Bright
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
    pixels.setPixelColor(i, pixels.Color(0,0,0));
    pixels.setPixelColor((NUMPIXELS-1)-i,pixels.Color(0,0,0)); // Setting the pixel to no output
    pixels.setPixelColor((NUMPIXELS-3)-i,pixels.Color(0,0,0)); // Setting the pixel to no output
    pixels.setPixelColor(i+2,pixels.Color(0,0,0));
    pixels.show();
  }
}
}

now am i right in thinking i can reduce the "numpixels" from 28 to 26 and add a bit into the "voidsetup" to write to leds 14 and 15 to turn on blue? or would that cause a conflict in the signal being sent down the signal line of the led strip?

also because of the code chasing from either end, would the code still operate from the 1st and 28th led or would it simply start from leds 1 and 26?

if you put the setPixelColor for 14 and 15 just before each pixels.show() you would overwrite whatever the the rest of the code has done to that two pixels.

P.S. the while(1) doesn't make sense so much. The rest of the code is already in the loop() and will repeat over and over again.

A quick untested hack. Hopefully my comments explain what is going on

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 2

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 28

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 175;  // delay for half a second

void setup()
{
    pixels.begin();                                     // This initializes the NeoPixel library.
    pixels.setPixelColor(13, pixels.Color(0, 0, 255));  //LED 13 Blue Bright
    pixels.setPixelColor(14, pixels.Color(0, 0, 255));  //LED 14 Blue Bright
}

void loop()
{
    // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

    for (int i = NUMPIXELS - 1; i >= (NUMPIXELS / 2); i--)
    {
        if (i != 13 && i != 14) //ignore LEDs 13 and 14
        {
            // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
            pixels.setPixelColor(i, pixels.Color(0, 0, 255));                    // Blue Bright
            pixels.setPixelColor(i + 2, pixels.Color(0, 0, 40));                 // Blue Not So Bright
            pixels.setPixelColor((NUMPIXELS - 1) - i, pixels.Color(0, 0, 255));  // Blue Bright
            pixels.setPixelColor((NUMPIXELS - 3) - i, pixels.Color(0, 0, 40));   // Blue Not So Bright
            pixels.show();                                                       // This sends the updated pixel color to the hardware.
            delay(delayval);                                                     // Delay for a period of time (in milliseconds).
            pixels.setPixelColor(i, pixels.Color(0, 0, 0));
            pixels.setPixelColor((NUMPIXELS - 1) - i, pixels.Color(0, 0, 0));  // Setting the pixel to no output
            pixels.setPixelColor((NUMPIXELS - 3) - i, pixels.Color(0, 0, 0));  // Setting the pixel to no output
            pixels.setPixelColor(i + 2, pixels.Color(0, 0, 0));
            pixels.show();
        }
    }
}

helibob, thats an interesting way to do it, seems alot simpler (when you know how to do things like that) to add an "ignore" code" to the existing code batch. i'll give it a go and see how it works.

noiasca, im not entirely sure on the while statement but it works so i guess its doing something right aha.

very simple

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            2

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      28


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 175; // delay for half a second

void setup() {
  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  for (int i = NUMPIXELS - 1; i >= (NUMPIXELS / 2); i--) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0, 0, 255)); // Blue Bright
    pixels.setPixelColor(i + 2, pixels.Color(0, 0, 40)); // Blue Not So Bright
    pixels.setPixelColor((NUMPIXELS - 1) - i, pixels.Color(0, 0, 255)); // Blue Bright
    pixels.setPixelColor((NUMPIXELS - 3) - i, pixels.Color(0, 0, 40)); // Blue Not So Bright
    middle();
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    pixels.setPixelColor((NUMPIXELS - 1) - i, pixels.Color(0, 0, 0)); // Setting the pixel to no output
    pixels.setPixelColor((NUMPIXELS - 3) - i, pixels.Color(0, 0, 0)); // Setting the pixel to no output
    pixels.setPixelColor(i + 2, pixels.Color(0, 0, 0));
    middle();
    pixels.show();
  }

}
void middle() {
  pixels.setPixelColor(13, pixels.Color(0, 0, 255));  //LED 13 Blue Bright
  pixels.setPixelColor(14, pixels.Color(0, 0, 255));  //LED 14 Blue Bright
}

using .fill() could omit the function

pixels.fill(0x0000FF, 13, 2); // switch to blue starting with pixel 13, in total 2 pixels

see:

#include <Adafruit_NeoPixel.h>
#define PIN            2
#define NUMPIXELS      28
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 175; 

void setup() {
  pixels.begin(); 
}

void loop() {
  for (int i = 0; i < (NUMPIXELS / 2-1); i++) {
    pixels.clear();
    pixels.setPixelColor(i+ 2, pixels.Color(0, 0, 255)); // Blue Bright
    pixels.setPixelColor(i , pixels.Color(0, 0, 40)); // Blue Not So Bright
    pixels.setPixelColor((NUMPIXELS - 3) - i, pixels.Color(0, 0, 255)); // Blue Bright
    pixels.setPixelColor((NUMPIXELS - 1) - i, pixels.Color(0, 0, 40)); // Blue Not So Bright
    pixels.fill(0x0000FF, 13, 2); // switch to blue starting with pixel 13, in total 2 pixels
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.