1

I'm using the NeoPixel Ring from Adafruit and trying to pulse though and array of colors. However, I'm having trouble with having the loop go to the next color at the end of each pulse (end of loop). Here is what I currently have:

uint32_t color[] = {red, green, blue,yellow,teal,magenta};
void pulseUp(uint8_t speed){
 int fadeAmount = 10; //how much to increase each loop
 int n = 0; //get first color
 for(int b = 100; b >= 10; b = b - fadeAmount) { //start pulse for loop
 for (int i = 0; i < strip.numPixels(); i++) { //select all pixels
 strip.setPixelColor(i, color[n]); //set color from our array
 }
 strip.setBrightness(b); //set brightness based on b
 strip.show(); // send the changes to the strip
 delay(speed); //wait before running again
 //if at the end of the loop (brightness is 10) change color 
 if (b == 10){
 if( n > 6){ //if we are at last color in our loop start over
 n = 0; 
 }
 else{ 
 n++; //auto increment color value
 }
 }
 }
}

I'm sure the problem is with the if statements but not sure how to fix it.

asked Sep 7, 2015 at 20:34
3
  • Where is the loop for going through each color? Commented Sep 7, 2015 at 20:40
  • @IgnacioVazquez-Abrams I was trying to do it with an if statement and n++ instead of a loop. Commented Sep 7, 2015 at 21:18
  • if is not a looping statement. Commented Sep 7, 2015 at 21:19

1 Answer 1

1

I can't quite make out what your if test is supposed to be doing, but don't you want a loop within a loop? Like this?

uint32_t color[] = {red, green, blue,yellow,teal,magenta};
void pulseUp(uint8_t speed)
 {
 const int fadeAmount = 10; //how much to increase each loop
 for (int currentColor = 0; currentColor < 6; currentColor++)
 {
 for (int i = 0; i < strip.numPixels(); i++)
 { //select all pixels
 strip.setPixelColor(i, color[currentColor]); //set color from our array
 }
 for(int brightness = 100; brightness >= 10; brightness -= fadeAmount)
 { //start pulse for loop
 strip.setBrightness(brightness); //set brightness based on brightness
 strip.show(); // send the changes to the strip
 delay(speed); //wait before running again
 } // end of brightness loop
 } // end of colour loop
 } // end of pulseUp
answered Sep 7, 2015 at 20:55
2
  • Yep, that's what I was trying to do. I tried something similar but must of had something off. Also what's the difference between int and const int ? Commented Sep 7, 2015 at 21:22
  • 1
    @BandonRandon: One can be changed, the other can't. Which means that the compiler can do things such as inlining the value. Commented Sep 7, 2015 at 21:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.