Time based loop/repeating using APA102 & ws2812 question

Status
Not open for further replies.

DJ Namaste

Active member
Hello

I am using apa102 and ws2812 leds for some projects using adafruit's neopixel and dotstar libraries. I want to do a colorwipe of one color then a second color to get strobe effects. A for loop works but every time I change the led strand length I have to change the for loop duration to get it to exit the loop after say 50 colorwipe sequences. I would like to make it work based on millis. I found the blink example and attempted to make it work with the digital led strips but the code is skipped over and the next sequence is displayed.

Below is my attempt to make a time based loop that will continue a strobe until the time elapses.

Any idea what im doing wrong?


#include <Adafruit_DotStar.h>

#include <SPI.h>


#define NUMPIXELS 32 // Number of LEDs in strip


#define DATAPIN 6
#define CLOCKPIN 8
Adafruit_DotStar strip = Adafruit_DotStar(
NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);


long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)

void setup() {

strip.begin(); // Initialize pins for output
strip.show(); // Turn all LEDs off ASAP
}


void loop() {
int j;

unsigned long currentMillis = millis();



if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;

colorWipe(strip.Color(0, 0, 100), 1);
colorWipe(strip.Color(0, 10, 0), 1);
}


for (j = 0; j < 5; j++) {
colorWipe(strip.Color(0, 0, 0), 1);
colorSwipe(strip.Color(100, 0, 0), 1);
}


}



// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);



delay(wait);
}
strip.show();

}

// Fill the dots one after the other with a color
void colorSwipe(uint32_t c, uint8_t wait) {
for(uint16_t i=strip.numPixels(); i>0; i--) {
strip.setPixelColor(i, c);

delay(wait);
}
strip.show();

}
 
I understand the code you posted, but I do not understand your description of what you want it to do.

Here's the description, without talk of the code.

I want to do a colorwipe of one color then a second color to get strobe effects.
....
but the code is skipped over and the next sequence is displayed.
....
a time based loop that will continue a strobe until the time elapses.

This just doesn't paint a complete enough picture to offer useful help. I can see you want to wipe between 2 colors many times, but then what? When does "next sequence is displayed" mean? What's a "sequence" you want? You said "continue a strobe until the time elapses", which I assume means keep wiping between 2 colors, but then what?

If you could explain more clearly what you actually intend to happen, without mixing talk of the code, I could try to help. But without knowing what this code is supposed to do, it's impossible to tell what's wrong.
 
Paul, I am creating a bunch of sequences of color wipes, rainbow chases, etc. I want to do each sequence for 5 seconds which strobes really fast. For example, red then blue colorwipe repeated for 5 seconds, then on to green/purple colorwipes for 5 seconds.

I have used for loops in the past which has worked but the length of the strip changes the length of each strobe sequence. So 20 leds takes half as long as 40 leds to do the colorwipe strobe sequence.
 
This piece of the code is how ive done things previously. It repeats the strobe effect until 50 is reached, then it goes to the next strobe or rainbow chase. I want to make it do the color wipes until 5 seconds pass, then move to the next strobe color combinations.

for (j = 0; j < 50; j++) {
colorWipe(strip.Color(0, 0, 0), 1);
colorSwipe(strip.Color(100, 0, 0), 1);
}
 
Maybe do something like this:

Code:
elapsedMillis mytimer;
const uint32_t colorA[4] = {0xFF0000, 0xFFFF00, 0x00FF00, 0x888888};
const uint32_t colorB[4] = {0x00FF00, 0x0000FF, 0x800060, 0x0066DE};
int colorIndex=0;

void loop() {
  if (mytimer > 5000) {
    mytimer = 0;
    colorIndex = colorIndex + 1;
    if (colorIndex >= 4) {
      colorIndex = 0;
    }
  }
  colorWipe(colorA[colorIndex], 1);
  colorSwipe(colorB[colorIndex], 1);
}

Every time loop() runs, it does a pair of wipes with 2 colors. When mytimer increments past 5 seconds, the index into those arrays increments, so a new pair of colors are used. Is that roughly what you're trying to do?
 
Yes that works. Interesting how you are using color index to create the strobes. Is there any more examples like this?
 
Status
Not open for further replies.
Back
Top