Non blocking neopixel code is blocking bluetooth communication

So this code, which i’ve adapted from rainbow animation code i found online to be none blocking, seems to prevent bluetooth communication (or some aspect of it).
My other animations i’ve adapted/created don’t block bluetooth communication.
Is there anything wrong with this code?
This is just the code that handles the rainbow animation, not all the other parts. If needed i can supply that but it’s quite large…

int index = 0;
int subIndex = 0;
unsigned long lastRainbowUpdate;
void rainbowCycle() {
      if (subIndex < 256) {  
        if (index < NUMPIXELS) {
          int wheelPos = ( (index * 256 / NUMPIXELS) + subIndex) & 255;
          ledStrip.setPixelColor(index, Wheel(wheelPos) );
          index++;
          ledStrip.show();
          if ( (millis() - lastRainbowUpdate) > 2) {
            subIndex++;
            lastRainbowUpdate = millis();
          }
        } else {
         index = 0; 
        }
      } else {
        subIndex = 0;
      }
    }
uint32_t Wheel(int WheelPos) {
      int wheelPos3 = WheelPos * 3;
      int red;
      int blue;
      int green;
      if (WheelPos < 85) {
        green = wheelPos3;
        red = 255 - wheelPos3;
        blue = 0;
      } 
      else if (WheelPos < 170) {
        int newWheelPos = WheelPos - 85;
        wheelPos3 = newWheelPos * 3;
        green = 255 - wheelPos3;
        red = 0;
        blue = wheelPos3;
      } 
      else {
        int newWheelPos = WheelPos - 170;
        wheelPos3 = newWheelPos * 3;
        green = 0;
        red = wheelPos3;
        blue = 255 - wheelPos3;
      }
      return ledStrip.Color(green, red, blue);
    }

jacobpweb:
So this code, which i've adapted from rainbow animation code i found online to be none blocking, seems to prevent bluetooth communication (or some aspect of it).
My other animations i've adapted/created don't block bluetooth communication.
Is there anything wrong with this code?
This is just the code that handles the rainbow animation, not all the other parts. If needed i can supply that but it's quite large....

maybe post a reduced sketch that has the bluetooth and only the rainbow animation...

Yes, it always updates the LEDs, even if nothing has changed.

There are also many problems in the code you didn't post.

BulldogLowell:
maybe post a reduced sketch that has the bluetooth and only the rainbow animation…

This is the main file:

Adafruit_NeoPixel leftRing(16, 2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel rightRing(16, 3, NEO_GRB + NEO_KHZ800);
Animations strip1(leftRing, NUMPIXELS);
Animations strip2(rightRing, NUMPIXELS);
int bluetoothTx = 10;  // TX-O pin of bluetooth
int bluetoothRx = 9;  // RX-I pin of bluetooth
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  bluetooth.begin(9600);
  strip2.lightSetup();
  strip2.setActivePattern(RANDOMFILL);
  strip1.lightSetup();
  strip1.setActivePattern(RANDOMFILL);
  pinMode(rightIndicatorInput, INPUT);
  pinMode(leftIndicatorInput, INPUT);
}

void loop() {
   strip1.update();
   strip2.update();
   recieveWithEndMarker();
   parseCommand();
}
void recieveWithEndMarker() {
  static byte ndx = 0;
  char endMarker = ';';
  char rc;
  while (bluetooth.available() > 0 && newData == false) {
    rc = bluetooth.read();
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    } else {
      strip1.turnOffRing();
      strip2.turnOffRing();
      receivedChars[ndx] = '\0';
      ndx = 0;
      newData = true;
    }
  }
}

void parseCommand() {
  if (newData == true) {
    if (strcmp("color", receivedChars) == 0) {
      //readColorValues();
      //strip1.setActivePattern(SOLID);
      //strip2.setActivePattern(SOLID);
    } else if (strcmp("chase", receivedChars) == 0) {
      strip1.setActivePattern(CHASE);
      strip2.setActivePattern(CHASE);
    } else if (strcmp("rainbow", receivedChars) == 0) {
      strip1.setActivePattern(RAINBOW);
      strip2.setActivePattern(RAINBOW);
    } else if (strcmp("drl", receivedChars) == 0) {
      strip1.setActivePattern(DRL);
      strip2.setActivePattern(DRL);
    } else if (strcmp("off", receivedChars) == 0) {
      strip1.setActivePattern(OFF);
      strip2.setActivePattern(OFF);
      bluetoothOverride = false;
    } else if (strcmp("randomfill", receivedChars) == 0) {
      strip1.setActivePattern(RANDOMFILL);
      strip2.setActivePattern(RANDOMFILL);
    }
    newData = false;
    strip1.resetBaseOperations();
    strip1.update();
    strip2.resetBaseOperations();
    strip2.update();
  }
}

This is my animations class stipped out:

enum pattern { RAINBOW, INDICATE, DRL, DEMO, SOLID, NONE, EMPTY, CHASE, OFF, RANDOMFILL };

class Animations {
    int NUMPIXELS;
    boolean colorChanged = false;
    Adafruit_NeoPixel ledStrip;
    int interval = 100;
    int green = 0;
    int red = 0;
    int blue = 0;
    unsigned long lastUpdate;
    unsigned long lastRainbowUpdate;
    pattern activePattern;
    int index = 0;
    int subIndex = 0;

    Animations (Adafruit_NeoPixel strip, int pixelStripSize) {
      NUMPIXELS = pixelStripSize;
      ledStrip = strip;
    }
    void update() {   
      if (!colorChanged) {
        if ( (millis() - lastUpdate) > interval) {
          lastUpdate = millis();
          switch (activePattern) {
            case DRL:
              changeColors(255, 255, 255);
              fillUpRing();
              break;
            case SOLID:
              fillUpRing();
              break;
            case INDICATE:
              changeColors(255, 100, 0);
              fillUpRing();
              break;
            case DEMO:
              break;
            case EMPTY:
              emptyRing();
              break;
            case RANDOMFILL:
              randomColorFill();
              break;
            case CHASE:
              chasingAnimation();
              break;
            case OFF:
              turnOffRing();
              break;
            case RAINBOW:
              rainbowCycle();
              break;
            default:
              break;
          }
        }
      }
    }
    void setActivePattern (pattern patternValue) {
      activePattern = patternValue;
      switch (activePattern) {
        case DRL:
          interval = 50;
          break;
        case SOLID:
          interval = 75;
          break;
        case INDICATE:
          interval = 50;
          break;
        case DEMO:
          break;
        case EMPTY:
          interval = 100;
          break;
        case RAINBOW:
          interval = 0;
          break;
        case RANDOMFILL:
          interval = 30;
          break;
        case CHASE:
          interval = 30;
          break;
        case OFF:
          break;
        default:
          return;
          break;
      }     
    }
    void rainbowCycle() {
      if (subIndex < 256) {  
        if (index < NUMPIXELS) {
          int wheelPos = ( (index * 256 / NUMPIXELS) + subIndex) & 255;
          ledStrip.setPixelColor(index, Wheel(wheelPos) );
          index++;
          ledStrip.show();
          if ( (millis() - lastRainbowUpdate) > 2) {
            subIndex++;
            lastRainbowUpdate = millis();
          }
        } else {
         index = 0; 
        }
      } else {
        subIndex = 0;
      }
    }
  private:
    uint32_t Wheel(int WheelPos) {
      int wheelPos3 = WheelPos * 3;
      int red;
      int blue;
      int green;
      if (WheelPos < 85) {
        green = wheelPos3;
        red = 255 - wheelPos3;
        blue = 0;
      } 
      else if (WheelPos < 170) {
        int newWheelPos = WheelPos - 85;
        wheelPos3 = newWheelPos * 3;
        green = 255 - wheelPos3;
        red = 0;
        blue = wheelPos3;
      } 
      else {
        int newWheelPos = WheelPos - 170;
        wheelPos3 = newWheelPos * 3;
        green = 0;
        red = wheelPos3;
        blue = 255 - wheelPos3;
      }
      return ledStrip.Color(green, red, blue);
    }
}

MorganS:
There are also many problems in the code you didn’t post.

There are none…

This is just the code that handles the rainbow animation...

If you look here you will see that you set the refresh interval to zero for RAINBOW:

      case DRL:
          interval = 50;
          break;
        case SOLID:
          interval = 75;
          break;
        case INDICATE:
          interval = 50;
          break;
        case DEMO:
          break;
        case EMPTY:
          interval = 100;
          break;
        case RAINBOW:
          interval = 0;
          break;
        case RANDOMFILL:
          interval = 30;
          break;
        case CHASE:
          interval = 30;
          break;
        case OFF:
          break;
        default:
          return;
          break;

Seems to me that would mean that the rainbowCycle() function is constantly running (ergo Blocking).

   void update() {   
      if (!colorChanged) {
        if ( (millis() - lastUpdate) > interval) {
          lastUpdate = millis();

Is the neopixel library turning off interrupts while it runs? This makes it hard for anything using serial interfaces to run if the neopixel updates run too often.

marco_c:
Is the neopixel library turning off interrupts while it runs?

yes...

Then you should make sure that you throttle it so other things get a chance to run as well.

marco_c:
Then you should make sure that you throttle it so other things get a chance to run as well.

exactly, OP left no room in between calls to RAINBOW effectively disabling interrupts all the time; therefore my suggestion to look a interval as an opportunity to allow some breathing room.

It makes sense, as he says, the other functions are able to process new commands...

marco_c:
Then you should make sure that you throttle it so other things get a chance to run as well.

Ok, i'll try that. Thanks for the reply. The only downside to this is it makes the animation run alot slower :confused: