Segmenting Neopixels into ranges from one pin

Hey everyone, I'm quite new to arduino and I'm having trouble understanding the format and remembering commands, as such I'm limited only to hard coding right now, which as you know takes forever to tweak.

I'm currently making a sword with an led strip (ws2812b) inside, but I need to run several different animations from one pin.

If anyone could tell me how to segment the strip digitally into multiple ranges so I can apply different animations for each part of the strip that would be really helpful!

TLDR:

1 pin controlling the led strip with 64 leds
need to segment into 0-15, 16-31, 32-41, 42-52 and 53-63

Thanks for your time! And sorry to be a total newb but we all start somewhere :slight_smile:

I’m not quite sure where the problem is. If you only want to update 0…15, only set those leds and next update the strip.

Do you store your patterns in an array?

Are you using a library such as FastLED? the .show() method always updates the entire string. But you can edit just the parts of the string that need to change and call .show() at any time.

I’m using the Adafruit_Neopixel.h library and I’m actually struggling to figure out how to write the code to define the parts of the led strip that I want to light up, I currently have (i=0; i<15(); i++) and (i=16; i<31(); i++) etc but I can’t get it to work like it does in the strand test example.

So I’m stuck figuring out what the code is to define a range of led’s if it isn’t (i=0; i<15(); i++) and if that is correct then why isn’t it working?

Post the code that you have written and describe what is wrong

i=0; i<15(); i++That is not how you write a for loop. What are those brackets doing in the middle of it ?

show us the current code.
you are on the right track, first fill up the parts with for loops using .setPixel() and then calling .show() to send the data package. Just keep in mind to use millis() for timing, have a look at the example at the top of the list of the subjects on this forum page.

xtektrix:
I currently have (i=0; i<15(); i++) and (i=16; i<31(); i++)

This is going to skip the LED at index 15 btw.

So my current (working) code looks like this, however I know I could condense it a lot by declaring a range of pixels I.e. 0-15 and controlling those in one go, as opposed to what I’m doing now which is…

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif
#include <SoftwareSerial.h>
#include <SD.h>

#define FADESPEED 60
#define NUMPIXELS 64
#define PIN 7

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

pixels.begin();
pixels.clear();
pixels.show();

}

void loop() {

//these control the spinning disc lights
pixels.setPixelColor(0, 60, 0, 240);
pixels.setPixelColor(1, 60, 0, 240);
pixels.setPixelColor(16, 60, 0, 240);
pixels.setPixelColor(17, 60, 0, 240);

pixels.setPixelColor(3, 20, 0, 50);
pixels.setPixelColor(19, 20, 0, 50);

pixels.setPixelColor(4, 60, 0, 240);
pixels.setPixelColor(5, 60, 0, 240);
pixels.setPixelColor(20, 60, 0, 240);
pixels.setPixelColor(21, 60, 0, 240);

pixels.setPixelColor(7, 20, 0, 50);
pixels.setPixelColor(23, 20, 0, 50);

pixels.setPixelColor(8, 60, 0, 240);
pixels.setPixelColor(9, 60, 0, 240);
pixels.setPixelColor(24, 60, 0, 240);
pixels.setPixelColor(25, 60, 0, 240);

pixels.setPixelColor(11, 20, 0, 50);
pixels.setPixelColor(27, 20, 0, 50);

pixels.setPixelColor(12, 60, 0, 240);
pixels.setPixelColor(13, 60, 0, 240);
pixels.setPixelColor(28, 60, 0, 240);
pixels.setPixelColor(29, 60, 0, 240);

pixels.setPixelColor(15, 20, 0, 50);
pixels.setPixelColor(31, 20, 0, 50);

//these control the gems
pixels.setPixelColor(32, 20, 0, 220);
pixels.setPixelColor(33, 20, 0, 220);
pixels.setPixelColor(34, 20, 0, 220);
pixels.setPixelColor(35, 20, 0, 220);
pixels.setPixelColor(36, 20, 0, 220);
pixels.setPixelColor(37, 20, 0, 220);
pixels.setPixelColor(38, 20, 0, 220);
pixels.setPixelColor(39, 20, 0, 220);
pixels.setPixelColor(40, 20, 0, 220);
pixels.setPixelColor(41, 20, 0, 220);

//these control the symbols
pixels.setPixelColor(42, 50, 0, 240);
pixels.setPixelColor(43, 50, 0, 240);
pixels.setPixelColor(44, 50, 0, 240);
pixels.setPixelColor(45, 0, 0, 240);
pixels.setPixelColor(46, 0, 0, 240);
pixels.setPixelColor(47, 0, 0, 240);
pixels.setPixelColor(48, 0, 0, 240);
pixels.setPixelColor(49, 0, 0, 240);
pixels.setPixelColor(50, 0, 0, 240);
pixels.setPixelColor(51, 0, 0, 240);
pixels.setPixelColor(52, 0, 0, 240);

pixels.setPixelColor(63, 50, 0, 240);
pixels.setPixelColor(62, 50, 0, 240);
pixels.setPixelColor(61, 50, 0, 240);
pixels.setPixelColor(60, 0, 0, 240);
pixels.setPixelColor(59, 0, 0, 240);
pixels.setPixelColor(58, 0, 0, 240);
pixels.setPixelColor(57, 0, 0, 240);
pixels.setPixelColor(56, 0, 0, 240);
pixels.setPixelColor(55, 0, 0, 240);
pixels.setPixelColor(54, 0, 0, 240);
pixels.setPixelColor(53, 0, 0, 240);

pixels.show();
delay(FADESPEED);

//these control the spinning disc lights
pixels.setPixelColor(1, 60, 0, 240);
pixels.setPixelColor(2, 60, 0, 240);
pixels.setPixelColor(17, 60, 0, 240);
pixels.setPixelColor(18, 60, 0, 240);

pixels.setPixelColor(0, 20, 0, 50);
pixels.setPixelColor(16, 20, 0, 50);

pixels.setPixelColor(5, 60, 0, 240);
pixels.setPixelColor(6, 60, 0, 240);
pixels.setPixelColor(21, 60, 0, 240);
pixels.setPixelColor(22, 60, 0, 240);

pixels.setPixelColor(4, 20, 0, 50);
pixels.setPixelColor(20, 20, 0, 50);

pixels.setPixelColor(9, 60, 0, 240);
pixels.setPixelColor(10, 60, 0, 240);
pixels.setPixelColor(25, 60, 0, 240);
pixels.setPixelColor(26, 60, 0, 240);

pixels.setPixelColor(8, 20, 0, 50);
pixels.setPixelColor(24, 20, 0, 50);

pixels.setPixelColor(13, 60, 0, 240);
pixels.setPixelColor(14, 60, 0, 240);
pixels.setPixelColor(29, 60, 0, 240);
pixels.setPixelColor(30, 60, 0, 240);

pixels.setPixelColor(12, 20, 0, 50);
pixels.setPixelColor(28, 20, 0, 50);

//these control the gems
pixels.setPixelColor(32, 20, 0, 180);
pixels.setPixelColor(33, 20, 0, 180);
pixels.setPixelColor(34, 20, 0, 180);
pixels.setPixelColor(35, 20, 0, 180);
pixels.setPixelColor(36, 20, 0, 180);
pixels.setPixelColor(37, 20, 0, 180);
pixels.setPixelColor(38, 20, 0, 180);
pixels.setPixelColor(39, 20, 0, 180);
pixels.setPixelColor(40, 20, 0, 180);
pixels.setPixelColor(41, 20, 0, 180);

//these control the symbols
pixels.setPixelColor(42, 50, 0, 240);
pixels.setPixelColor(43, 50, 0, 240);
pixels.setPixelColor(44, 50, 0, 240);
pixels.setPixelColor(45, 50, 0, 240);
pixels.setPixelColor(46, 50, 0, 240);
pixels.setPixelColor(47, 50, 0, 240);
pixels.setPixelColor(48, 0, 0, 240);
pixels.setPixelColor(49, 0, 0, 240);
pixels.setPixelColor(50, 0, 0, 240);
pixels.setPixelColor(51, 0, 0, 240);
pixels.setPixelColor(52, 0, 0, 240);

pixels.setPixelColor(63, 50, 0, 240);
pixels.setPixelColor(62, 50, 0, 240);
pixels.setPixelColor(61, 50, 0, 240);
pixels.setPixelColor(60, 50, 0, 240);
pixels.setPixelColor(59, 50, 0, 240);
pixels.setPixelColor(58, 50, 0, 240);
pixels.setPixelColor(57, 0, 0, 240);
pixels.setPixelColor(56, 0, 0, 240);
pixels.setPixelColor(55, 0, 0, 240);
pixels.setPixelColor(54, 0, 0, 240);
pixels.setPixelColor(53, 0, 0, 240);

pixels.show();
delay(FADESPEED);

The following code:

//these control the gems
 pixels.setPixelColor(32, 20, 0, 220);
 pixels.setPixelColor(33, 20, 0, 220);
 pixels.setPixelColor(34, 20, 0, 220);
 pixels.setPixelColor(35, 20, 0, 220);
 pixels.setPixelColor(36, 20, 0, 220);
 pixels.setPixelColor(37, 20, 0, 220);
 pixels.setPixelColor(38, 20, 0, 220);
 pixels.setPixelColor(39, 20, 0, 220);
 pixels.setPixelColor(40, 20, 0, 220);
 pixels.setPixelColor(41, 20, 0, 220);

 //these control the symbols
 pixels.setPixelColor(42, 50, 0, 240);
 pixels.setPixelColor(43, 50, 0, 240);
 pixels.setPixelColor(44, 50, 0, 240);
 pixels.setPixelColor(45, 0, 0, 240);
 pixels.setPixelColor(46, 0, 0, 240);
 pixels.setPixelColor(47, 0, 0, 240);
 pixels.setPixelColor(48, 0, 0, 240);
 pixels.setPixelColor(49, 0, 0, 240);
 pixels.setPixelColor(50, 0, 0, 240);
 pixels.setPixelColor(51, 0, 0, 240);  
 pixels.setPixelColor(52, 0, 0, 240);

Can be replaced with:

byte i; //Global counter variable

for (i = 32; i <= 41; i++) pixels.setPixelColor(i, 20, 0, 220);
for (i = 42; i <= 44; i++) pixels.setPixelColor(i, 50, 0, 240);
for (i = 45; i <= 52; i++) pixels.setPixelColor(i, 0, 0, 240);

Now you try! :slight_smile:

And you should go for using millis() for your timing.

UKHeliBob:
Post the code that you have written and describe what is wrong

i=0; i<15(); i++That is not how you write a for loop. What are those brackets doing in the middle of it ?

15 is a number, not a function, so the compiler will complain about this!

Danois90 That’s it!

byte i; //Global counter variable

for (i = 32; i <= 41; i++) This is the code that I was after! Thank you!!