Go Down

Topic: Led strip looping design question (Read 622 times) previous topic - next topic

wsmithmusic

Hey everyone, I'm relatively new to programming and need some basic led design advice.  I have some individually addressable dotstar led strips (apa102) and I'm using an Uno with the FastLed library to control them.  I've figured out how to design and run individual looped programs that make pretty colors and patterns.  Now I want to run 3 or 4 of these loops a few times each sequentially in a larger loop.  What's the best way to do this? 

Thank you for your time.

PaulRB

Best way or quickest/easiest way?

It depends what you want to do next. Will there be interactions with buttons or commands from elsewhere? If not, you might as well go the quick/easy route. But if there will be interaction of some kind, you need to do it in a different way, and you may also have to re-write the code you have now, if you have used delay().

Post your code here, using code tags so your code looks like
Code: [Select]
this
and answer my question above.


INTP

Copy/paste will work pretty well.
To keep it a little neater, you could simply replace the word 'loop' in each of those patterns with a name so each one is a function you can call, then in the one loop you get, just call the functions in the order and number you want.

wsmithmusic

Here's my code.  There are no planned interactions with button or external commands.  It does 5 runs down the strip as blue then switches to white.  I assume I could add another for(int x; x > 10; x ++) statement and do something else with the strip, but I don't understand how to get back to the beginning of the loop.  Or if perhaps there is a more elegant way to do it.  Thanks.


Code: [Select]
  #include <FastLED.h>
    #define NUM_LEDS 150
    #define DATA_PIN 4
    #define CLOCK_PIN 5

    CRGB leds[NUM_LEDS];



void setup() {
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS); // put your setup code here, to run once:

}

void loop() {
 for(int x = 0; x < 5; x++) {
 
 for(int dot = 0; dot < NUM_LEDS; dot++) {
            leds[dot] = CRGB::Blue;
            FastLED.show();
            // clear this led for the next time around the loop
            leds[dot] = CRGB::Black;
            delay(20);

 }}
 
 for(int x; x > 5; x ++) {
   for(int dot = 0; dot < NUM_LEDS; dot++) {
            leds[dot] = CRGB::White;
            FastLED.show();
            // clear this led for the next time around the loop
            leds[dot] = CRGB::Black;
            delay(20);
           
   } }    }

larryd

Welcome wsmithmusic.

loop() just loops to the beginning when it comes to the last } in loop()

Use     CTRL T      to format your code.


.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

INTP

Oh, you wanted someone to do it for you? Should've said so.

PaulRB

I don't understand how to get back to the beginning of the loop.
So... you have not actually run this code yet? If you had, you would already know that going back to the beginning is exactly what it does...

Go Up