Is it possible to use a secondary sub loop in active void loop

So first off. Thanks for any advise I can get on this. I have some neopixel lights in a gaming console and I am trying to Frankenstein two pieces of code together. There are 2 lights on the chassis. There are also 4 lights in the ports. I want the 2 lights to stay on all the time and the 4 lights to start one by one until finished and then start a breathing effect. My question is, The breathing effect doesn't start after lights are finished with its one by one sequence. How can I write the breathing effect into my code? P.S. The first loop is fine I want to add the void sub_loop1() to the sequence. I really need advise!

#include "FastLED.h"  
#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN2, NEO_GRB + NEO_KHZ800);

#define PIN 2
#define MAX_FRAMES 6

//
constexpr uint16_t ledPin1 {2};
constexpr uint16_t numLeds1 {4};
//
constexpr uint16_t ledPin2 {3};
constexpr uint16_t numLeds2 {2};
//
CRGB leds_1[numLeds1];
CRGB leds_2[numLeds2];
//
int frameIndex = 1;     // skips 0 at start up leds.
int nextFrameIndex() {
if (frameIndex < MAX_FRAMES - 1) 
  {
      return frameIndex + 1;    //Each Frame leds turn on 1 by 1.
    }  
    {
    }
}    
//
void setup(){             //Abilities.
//   Addresses LED Boot.
FastLED.addLeds<WS2812B, ledPin1, GRB>(leds_1, numLeds1);
//   Addresses Memory Pak LED.
FastLED.addLeds<WS2812B, ledPin2, GRB>(leds_2, numLeds2);
fill_solid(leds_2, numLeds2, CRGB::Purple);
//
  strip.begin();
  strip.setBrightness(85);  // Lower brightness and save eyeballs!
  strip.show(); // Initialize all pixels to 'off'
}
//
void loop(){
    EVERY_N_MILLISECONDS(790) {    // Time in between Port LEDs Turing on.
        frameIndex = nextFrameIndex();
      setColors();         // Turning this off breaks the Start leds Function.            
    }
    FastLED.show();   
    {
    }
}

//
void setColors(){
    switch (frameIndex) {
		case 1:
			break;
		case 2:
			leds_1[0].setRGB(192, 4, 205);
			break;
		case 3:
			leds_1[1].setRGB(192, 4, 205);
			break;
		case 4:
			leds_1[2].setRGB(192, 4, 205);
			break;
		case 5:
			leds_1[3].setRGB(192, 4, 205);
			break;
    }    
}
//
 void sub_loop1(){
 int TOTAL_LEDS = 4;
 float MaximumBrightness = 255;
 float SpeedFactor = 0.008; // I don't actually know what would look good
 float StepDelay = 5; // ms for a step delay on the lights

// Make the lights breathe
 for (int i = 0; i < 65535; i++) {

// Intensity will go from 10 - MaximumBrightness in a "breathing" manner
 float intensity = MaximumBrightness /2.0 * (1.0 + sin(SpeedFactor * i));
 strip.setBrightness(intensity);

// Now set every LED to that color
 for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {
 strip.setPixelColor(ledNumber, 255, 0, 255);
 }
 strip.show();
//Wait a bit before continuing to breathe
 delay(StepDelay);          // - Time in between Port LEDs Turning on.
}
}

Your code follows a sequence of 6 frames, but the 6th frame does not appear to be used for anything. You could add your breathing effect as the 6th frame.

		case 6:
			sub_loop1();
			break;
1 Like

Thank you so much! I knew I had one more thing I was missing and didn't think of using it as a case #. This solved my dilemma. I tweaked just a few things to make it work right. You pointed me in the right direction. I wrote max frames = 7 and added a delay() between case 5 and 6. It's Perfect!

1 Like

It may do what you wanted, but your code is far from perfect! If you have time and want to know, just ask.

I want to learn as much as I can. What would you do differently?

I would ensure the indentation is always correct by frequently using Tools->Auto Format. This can be really helpful when tracking down bugs and compiler errors, and helps to avoid them in the first place.

I would remove any extraneous braces {}. I see a couple of those.

But perhaps more importantly than those things would be to figure out if it is worthwhile to use "non-blocking code style" or not.

Non-blocking code has some important advantages. But it is also more complex. If you are not going to make use of those advantages, then it would be better to use blocking code style and this will allow you to keep the code really short and simple.

Right now, your code is a mix of blocking and non-blocking styles. That means it doesn't have the advantages it could have of it were completely non-blocking. It also doesn't have the simplicity and reduced size it could have of it was completely blocking code.

Right now, I don't see any advantage to using the more complex non-blocking style. But maybe you have further enhancements in mind which would require it's use?

1 Like

Ok I understand. frequently auto format.

When in the future I write more code, I will choose either blocking or non-blocking scenarios depending on what advantages it needs if it does need them. I will have to study more but you gave me a lot to work with. Thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.