Change LED brightness during stepper motor loop

Hi there, I am using an ultrasonic sensor to monitor distance. When something gets close enough I trigger a stepper motor to run. At the same time I want a neopixels LED strip to get brighter.

Here is the code I use to run the stepper. I am turning some 26 revolutions Question is can I do the step.setBrightness() inside the for loop without changing the stepper behavior? Would there be a better way to increase the LED brightness over this same interval?

 for(int i = 0; i < 5313; i++)
  {
    digitalWrite(5, HIGH);
    delayMicroseconds(500);
    digitalWrite(5, LOW);
    delayMicroseconds(500);

    strip.setBrightness(.047 * i); 
  }

Would there be a better way to increase the LED brightness over this same interval?

Yes. Let the loop() function do the looping for you and use the BWoD principle for timing to prevent blocking execution of other code.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

leds.setBrightness( Brightness ); //  1 = min brightness (off), 255 = just below max brightness.

The issue with the Adafruit_NeoPixel setBrightness is that the brightness can be decreased but not increased. On the Adafruit discussion forum is a conversation about how to modify the cpp to have the setBrightness command increase and decrease brightness without a rest of the uController.

Thanks guys! The BWoD example was just what I needed. Instead of setBrightness() I just used strip.fill with a 0-255 color instead which seems to work well.

This leads to another question though - if I strip.fill() with just red at 0-255 and use 0 for green and blue like so:

strip.fill(strip.Color(rColor, 0, 0), 0, LED_COUNT - 1);
    strip.show();

The stepper turns noticeably rougher and slower. If I just change the fill to make purple, or white, like so, then the motor is very smooth.

strip.fill(strip.Color(rColor, 0, rColor), 0, LED_COUNT - 1);
strip.show();

Does this make any sense to anyone?