Neopixel LED strip modified code from zelda sword project

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Neopixel LED strip modified code from zelda sword project

Post by dre_force »

trying to modify some code for the zelda sword project to suit the led strip i have.

the original project calls for 2m led strip, i only have a meter which has 144 leds.

when i use the original code a bunch of leds on the end of the strip dont light up i get the first 14 show as orange the next lot show blue and then some more on the end show as orange and a bunch of strips are on the end dont light up at all.

i modified the code so that the first 21 show up as orange and the rest as blue but again it wont activate a bunch of leds at the end of the strip.

any suggestion. i have included the original code below. would also be great to reduce the time delay from sliding the switch to the leds lighting up.

Code: Select all

// SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            4

// Color Segments
#define APIXELS      14 // number of first orange pixels 
#define BPIXELS      84 // number of blue pixels
#define CPIXELS      93 // second orange pixels

// 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(93, PIN, NEO_GRB + NEO_KHZ800);

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

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

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

void loop() {

  // For the first 14 pixels, make them orange, starting from pixel number 0.
  for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(255,50,0)); // Set Pixels to Orange Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }

  // Fill up 84 pixels with blue, starting with pixel number 14.
  for(int i=14;i<BPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,250,200)); // Set Pixels to Blue Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).

  }
  
  // Fill up 9 pixels with orange, starting from pixel number 84.
  for(int i=84;i<CPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(250,50,0)); //Set Pixels to Orange Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}
Last edited by adafruit_support_carter on Mon Oct 18, 2021 1:24 pm, edited 1 time in total.
Reason: added [code] tags

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dastels »

To use all 144 pixels you need to instantiate the NeoPixel object for 144 pixels.

Code: Select all

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(93, PIN, NEO_GRB + NEO_KHZ800);
is instantiating it for 93 pixels. It is ignoring any beyond that number. Change that 93 to 144 to have it use all the pixels.

Dave

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

Thanks for the reply Dave, Ive posted below the modified code i tried before posting this as you can see i tried changing this number but when i did this nothing happened as in no leds lit up at all. i will try again but am quite sure i tried it multiple times the other day.

modified code -

Code: Select all

// SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries

//

// SPDX-License-Identifier: MIT



#include <Adafruit_NeoPixel.h>

#ifdef __AVR__

  #include <avr/power.h>

#endif



// Which pin on the Arduino is connected to the NeoPixels?

// On a Trinket or Gemma we suggest changing this to 1

#define PIN            4



// Color Segments

#define APIXELS      21 // number of first orange pixels 

#define BPIXELS      110 // number of blue pixels

#define CPIXELS      13 // second orange pixels



// 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(144, PIN, NEO_GRB + NEO_KHZ800);



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



void setup() {

  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket

#if defined (__AVR_ATtiny85__)

  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);

#endif

  // End of trinket special code



  pixels.begin(); // This initializes the NeoPixel library.

}



void loop() {



  // For the first 21 pixels, make them orange, starting from pixel number 0.

  for(int i=0;i<APIXELS;i++){

    pixels.setPixelColor(i, pixels.Color(255,50,0)); // Set Pixels to Orange Color

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }



  // Fill up 110 pixels with blue, starting with pixel number 21.

  for(int i=21;i<BPIXELS;i++){

    pixels.setPixelColor(i, pixels.Color(0,250,200)); // Set Pixels to Blue Color

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).



  }

  

  // Fill up 13 pixels with orange, starting from pixel number 131.

  for(int i=131;i<CPIXELS;i++){

    pixels.setPixelColor(i, pixels.Color(250,50,0)); //Set Pixels to Orange Color

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }

}

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dastels »

One problem I see is in your for loops. Here;s the orage (I believe the same problem is in the blue one):

Code: Select all

 for(int i=131;i<CPIXELS;i++)
I believe the limit should be 131 + CPIXELS As CPIXELS, BPIXELS, etc are the number of pixels, not the index of the last one in the group.

Dave

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

Appreciate the response Dave, not having much luck. When i change the 93 number to anything 144 no LEDs light up. Even if i remove the B and C pixels and just try and fill the strip with the one colour. ive recharged the battery and will try again as i was getting some random results from the low battery. blue was turning green and such. also if you could clarify what you mean by the your statement "i belive the limit should be 131 + CPIXELS ... etc are the number of pixels, not the index of the last one in the group."

Also ive noted in the code it says - :Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values."

do you know what this ment by this.

who would have thought getting leds to light up would be so hard.

Thanks again.
Attachments
zelda.jpg
zelda.jpg (220.56 KiB) Viewed 207 times

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

results of original code
Attachments
IMG_20211017_205317.jpeg
IMG_20211017_205317.jpeg (232.48 KiB) Viewed 207 times

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dastels »

The original code specifies 93 pixels so it will never light up more than that.

The APIXELS, BPIXELS, and CPIXELS in the original code are the index of the last pixel in each group. In your code they are the number of pixels in each ground. For example, your second orang loop is equivalent to

Code: Select all

for (int i = 131; i < 13; i++)
So the loop never loops because i starts out greater than 13.

Since the original code worked, the erd argument to the NeoPixel constructor is correct. Some NeoPixels have R, G, and B is a different order. and expect a control signal a different frequency. That's hat the 3rd argument is for.

I find it useful to have the circuit attached to USB for power when working on the code. That will eliminate battery issues as a source of problems. Once you know everything works, then you can deal with battery powering it.

Dave

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

Code: Select all

// SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            4

// Color Segments
#define APIXELS      21 // number of first orange pixels 
#define BPIXELS      110 // number of blue pixels
#define CPIXELS      13 // second orange pixels

// 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(144, PIN, NEO_GRB + NEO_KHZ800);

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

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

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

void loop() {

  // For the first 21 pixels, make them orange, starting from pixel number 0.
  for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(255,50,0)); // Set Pixels to Orange Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }

  // Fill up 110 pixels with blue, starting with pixel number 21.
  for(int i=110;i<BPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,250,200)); // Set Pixels to Blue Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).

  }
  
  // Fill up 13 pixels with orange, starting from pixel number 131.
  for(int i=13;i<CPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(250,50,0)); //Set Pixels to Orange Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}
Last edited by adafruit_support_carter on Mon Oct 18, 2021 1:23 pm, edited 1 time in total.
Reason: added [code] tags

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

this is the error message i get when loading this modified code
Attachments
Screen Shot 2021-10-18 at 1.01.10 pm.png
Screen Shot 2021-10-18 at 1.01.10 pm.png (151.13 KiB) Viewed 189 times

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dastels »

I've never seen that one. I'll see what I can find out.

Dave

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by adafruit2 »

what board are you using? a trinket 5V?

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

yes trinket 5v

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

// SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 4

// Color Segments
#define APIXELS 21 // number of first orange pixels
#define BPIXELS 110 // number of blue pixels
#define CPIXELS 13 // second orange pixels

// 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(144, PIN, NEO_GRB + NEO_KHZ800);

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

void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code

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

void loop() {

// For the first 21 pixels, make them orange, starting from pixel number 0
for(int i=0;i<APIXELS;i++){
pixels.setPixelColor(i, pixels.Color(255,50,0)); // Set Pixels to Orange Color
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}

// Fill up 110 pixels with blue, starting with pixel number 21.
for(int i=110;i<BPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,250,200)); // Set Pixels to Blue Color
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).

}

// Fill up 13 pixels with orange, starting from pixel number 131.
for(int i=13;i<CPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(250,50,0)); //Set Pixels to Orange Color
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}

User avatar
dre_force
 
Posts: 16
Joined: Thu Oct 14, 2021 9:08 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dre_force »

Managed to load the code above with no error message but when i test no leds light up at all.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: Neopixel LED strip modified code from zelda sword projec

Post by dastels »

First, consider upgrading to a Trinket M0. The older Trinket models are known to have problems with more modern USB implementations. There's a warning to that effect on the product page.

Secondly, your for loops are still incorrect. Please reread my previous responses regarding the code.

Dave

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

Return to “General Project help”