How to use do...while

image

Please describe your question in more detail.

and review do while() syntax.

do {
  // code block to be executed
}
while (condition);
2 Likes

Help us help you.

I want to run both pixels 1 and pixels 2 but before i added "do" and "while" it only run pixels 1 -> pixels 2 -> pixels 1 -> pixels 2....
What code can be to run both 2 pixels

Yep thats it. Thats how do while works.

What did i forget in here?

The error is like this: You wrote

loop{
 //do something
}//Here is the error about
do{
//somehting 
}
while
{
}
```
Like it should look:

```
do{
//somehting
}
while(condition)

Can u fix for me?
I can post my code in here

I can at least try it.

#include <Adafruit_NeoPixel.h>

int pin_1 = 2; int pin_2 = 3;
int numPixels_1 = 8; int numPixels_2 =16;
int pixelFormat = NEO_GRB + NEO_KHZ800;
Adafruit_NeoPixel *pixels_1; Adafruit_NeoPixel *pixels_2;

#define DELAYVAL 255

void setup() {
pixels_1 = new Adafruit_NeoPixel(numPixels_1, pin_1, pixelFormat);
pixels_1->begin();

pixels_2 = new Adafruit_NeoPixel(numPixels_2, pin_2, pixelFormat);
pixels_2->begin();
pixels_2->show();
pixels_2->setBrightness(50);
}

void loop() {
// PIXELS 1
do
pixels_1->clear();
for(int i=0; i<numPixels_1; i++) {
pixels_1->setPixelColor(i, pixels_1->Color(0, 150, 0));
pixels_1->show();
delay(DELAYVAL);

}
while
// PIXELS 2
colorWipe(pixels_2->Color(255, 0, 0), 50); // Red
colorWipe(pixels_2->Color( 0, 255, 0), 50); // Green
colorWipe(pixels_2->Color( 0, 0, 255), 50); // Blue

rainbow(10);
theaterChaseRainbow(50);

}

void colorWipe(uint32_t color, int wait) {
for(int j=0; j<pixels_2->numPixels(); j++) {
pixels_2->setPixelColor(j, color);
pixels_2->show();
delay(wait);
}
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
pixels_2->rainbow(firstPixelHue);
pixels_2->show();
delay(wait);
}
}
void theaterChaseRainbow(int wait) {
int firstPixelHue = 0;
for(int a=0; a<30; a++) {
for(int b=0; b<3; b++) {
pixels_2->clear();
for(int c=b; c<pixels_2->numPixels(); c += 3) {
int hue = firstPixelHue + c * 65536L / pixels_2->numPixels();
uint32_t color = pixels_2->gamma32(pixels_2->ColorHSV(hue));
pixels_2->setPixelColor(c, color);
}
pixels_2->show();
delay(wait);
firstPixelHue += 65536 / 90;
}
}
}

What symbol always comes right after the do? Look at the examples

   for(int i=0; i<numPixels_1; i++) {
       pixels_1->setPixelColor(i, pixels_1->Color(0, 150, 0));
       pixels_1->show();
       delay(DELAYVAL);
   }

i'm not familiar with AdaFruit_NeoPixles. shouldn't you execute the above for both "pixels"(?), pixels_1 and pixels_2?

maybe

    for(int i=0; i<numPixels_1; i++) {
        pixels_1->setPixelColor(i, pixels_1->Color(0, 150, 0));
        pixels_1->show();
        delay(DELAYVAL);
    }
    for(int i=0; i<numPixels_2; i++) {
        pixels_2->setPixelColor(i, pixels_2->Color(0, 150, 0));
        pixels_2->show();
        delay(DELAYVAL);
    }
#include <Adafruit_NeoPixel.h>

int pin_1 = 2; int pin_2 = 3;
int numPixels_1 = 8; int numPixels_2 =16;
int pixelFormat = NEO_GRB + NEO_KHZ800;
Adafruit_NeoPixel *pixels_1; Adafruit_NeoPixel *pixels_2;

#define DELAYVAL 255

void setup() {
pixels_1 = new Adafruit_NeoPixel(numPixels_1, pin_1, pixelFormat);
pixels_1->begin();

pixels_2 = new Adafruit_NeoPixel(numPixels_2, pin_2, pixelFormat);
pixels_2->begin();
pixels_2->show();
pixels_2->setBrightness(50);
}

void loop() {
// PIXELS 1
do {
pixels_1->clear();
for(int i=0; i<numPixels_1; i++) {
pixels_1->setPixelColor(i, pixels_1->Color(0, 150, 0));
pixels_1->show();
delay(DELAYVAL);

}

// PIXELS 2
colorWipe(pixels_2->Color(255, 0, 0), 50); // Red
colorWipe(pixels_2->Color( 0, 255, 0), 50); // Green
colorWipe(pixels_2->Color( 0, 0, 255), 50); // Blue

rainbow(10);
theaterChaseRainbow(50);
while(//!!!!!!!!!!!!!!!!Custom Condition)
}

void colorWipe(uint32_t color, int wait) {
for(int j=0; j<pixels_2->numPixels(); j++) {
pixels_2->setPixelColor(j, color);
pixels_2->show();
delay(wait);
}
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
pixels_2->rainbow(firstPixelHue);
pixels_2->show();
delay(wait);
}
}
void theaterChaseRainbow(int wait) {
int firstPixelHue = 0;
for(int a=0; a<30; a++) {
for(int b=0; b<3; b++) {
pixels_2->clear();
for(int c=b; c<pixels_2->numPixels(); c += 3) {
int hue = firstPixelHue + c * 65536L / pixels_2->numPixels();
uint32_t color = pixels_2->gamma32(pixels_2->ColorHSV(hue));
pixels_2->setPixelColor(c, color);
}
pixels_2->show();
delay(wait);
firstPixelHue += 65536 / 90;
}
}
}

I dont kow this is gonna work because im not familiar with NeoPixel. Just try it i think.

1 Like

That didn't work, i maybe use fastled instead of neopixel

Changing to FastLED is unlikely to make any difference.

It is not exactly clear what you want to do, or what the do/while is needed for.

As it is, the code (if you remove the do/while), will do the following sequence:

clear pixel_1
progressively light all the LEDs in pixel_1
light pixel_2 all RED
light pixel_2 all GREEN
light pixel_2 all BLUE
run rainbow on pixel_2
run theaterChaseRainbow on pixel_2

do you understand "fastled" better than you understand "neo_pixel"?

If you don't know the condition of the while, then you likely don't need a do/while loop.
You are already in a looping function. Consider just eliminating the do/while loop and just coding directly what you want done.

It sounds like you want to have different patterns showing on the two strips at the same time. To do that you have to get rid of delay() in all of your patterns. For example:

This has to be re-written using a millis() timer;

  static unsigned long lastChangeTime = 0;
  int currentPixel = 0;
  if (millis() - lastChangeTime >= DELAYVAL)
  {
    lastChangeTime = millis();

    if (currentPixel == 0) 
      pixels_1->clear();

    pixels_1->setPixelColor(currentPixel, pixels_1->Color(0, 150, 0));
    pixels_1->show();
    currentPixel++;
    if (currentPixel >= numPixels_1)
      currentPixel = 0;
  }

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