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.

  • Where is the loop for going through each color? – Ignacio Vazquez-Abrams Sep 7 '15 at 20:40
  • @IgnacioVazquez-Abrams I was trying to do it with an if statement and n++ instead of a loop. – Brooke. Sep 7 '15 at 21:18
  • if is not a looping statement. – Ignacio Vazquez-Abrams Sep 7 '15 at 21:19
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
  • 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 ? – Brooke. Sep 7 '15 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. – Ignacio Vazquez-Abrams Sep 7 '15 at 21:37

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.