# Bit of help converting for loops?

Hi, I’d like to ask for a bit of assistance … basically I have the below functions, that I need to be non-blocking:

``````// 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);
strip.show();
delay(wait);
}
}

void rainbow(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
``````

My attempt was like so:

``````uint16_t k, j = 0;
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint16_t wait) {
if (k < strip.numPixels()){
k++;
strip.setPixelColor(k, c);
strip.show();
delay(wait);
} else {
k = 0;
}
}

void rainbow(uint8_t wait) {
if (j < 256) {
j++;
if (k < strip.numPixels()) {
strip.setPixelColor(k, Wheel((k+j) & 255));
} else {
k = 0;
}
strip.show();
delay(wait);
} else {
j = 0;
}
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
if (j < 256*5) { // 5 cycles of all colors on wheel
j++;
if (k < strip.numPixels()) {
strip.setPixelColor(k, Wheel(((k * 256 / strip.numPixels()) + j) & 255));
} else {
k = 0;
}
strip.show();
delay(wait);
} else {
j = 0;
}
}
``````

However this doesn’t seem to be doing what I wanted. Is there a better way to go about this?

Thanks

Usual advice. You need to get rid of the delay()s and use the technique in the Blink Without Delay example sketch.

I wrote an extended demo in the first post of this Thread.

...R

Without seeing your whole program it is difficult to give full help, but all of the functions you included in your post, both original and revised, use the delay() function so will be blocking to some extent.

The for loop versions will block longer of course but we need to see how and where the functions are called in order to provide help. For instance, what are the values of j and k when the revised version of the rainbow() function is called and how frequently is it called ?

Thanks guys, I actually got it working to the point I needed it to. Turns out I was forgetting to increment k, oops!

Ended up like this:

``````// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint16_t wait) {
if (k < strip.numPixels()){
strip.setPixelColor(k, c);
strip.show();
delay(wait);
k++;
} else {
k = 0;
colwipe++;
if (colwipe > 3){
colwipe = 1;
}
}
}

void rainbow(uint8_t wait) {
if (j < 256) {
j++;
if (k < strip.numPixels()) {
strip.setPixelColor(k, Wheel((k+j) & 255));
k++;
} else {
k = 0;
iterated = true;
}
strip.show();
if (iterated == true){
delay(wait);
iterated = false;
}
} else {
j = 0;
}
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
if (j < 256*5) { // 5 cycles of all colors on wheel
j++;
if (k < strip.numPixels()) {
strip.setPixelColor(k, Wheel(((k * 256 / strip.numPixels()) + j) & 255));
k++;
} else {
k = 0;
iterated = true;
}
strip.show();
if (iterated == true){
delay(wait);
iterated = false;
}
} else {
j = 0;
}
}
``````

Good for you for getting it working, but you are still using delay() which will be blocking to some extent. What sort of value does the wait variable have when the functions are called ?