Neopixel Lagging Loop

hi, my loop uses a rotary encoder to navigate a menu, then displays the info on a screen, after adding a neopixel my rotary encoder lags, if I turn the neopixel off I get the full response from the encoder back, is there a way I can give the rotary encoder priority, to stop the lag

void loop()
{
  if (digitalRead(BypassFSW) == HIGH)
  {
    if (current_patch == 0)
      pixels.setPixelColor(0, r0, g0, b0); // Yellow
    pixels.show();

    if (current_patch == 1)
      pixels.setPixelColor(0, r1, g1, b1); // Green
    pixels.show();

    if (current_patch == 2)
      pixels.setPixelColor(0, r2, g2, b2);  // Red
    pixels.show();

    if (current_patch == 3)
      pixels.setPixelColor(0, r3, g3, b3); // Grey
    pixels.show();

    if (current_patch == 4)
      pixels.setPixelColor(0, r4, g4, b4); // Blue
    pixels.show();

    if (current_patch == 5)
      pixels.setPixelColor(0, r5, g5, b5); // Magenta
    pixels.show();

    if (current_patch == 6)
      pixels.setPixelColor(0, r6, g6, b6); // Orange
    pixels.show();

    if (current_patch == 7)
      pixels.setPixelColor(0, r7, g7, b7); // Cyan
    pixels.show();
  }

  if (digitalRead(BypassFSW) == LOW)
  {
    pixels.setPixelColor(0, 0, 0, 0); // Off
    pixels.show();

  }
  const uint16_t * palette = patch_colors[current_patch];
  if (myButton.rose() == true) {
    if (es == None) {
      es = Select;
      draw_selection(palette, selection, false);
    } else if (es == Select) {
      es = Change;
      // emphasize bar that is being edited
      draw_selection(palette, selection, true);
    } else if (es == Change) {
      es = None;
      if (changed[current_patch]) {
        bg_col = MARKED;
        draw_panel();
        draw_patch();             // Redraw the whole screen
      }
      clear_selection(selection);
    }
  }



  int8_t dir = read_rotary();

  if (myButton.update() == true) {
    switch (myButton.event()) {

      // things to do if the button was held (Hold = Save)
      case (hold) : {
          if (changed[current_patch]) {
            save_current_patch();
            es = None;                               // Go to default mode
            clear_selection(selection);

            rainbow(1);
            pixels.show();
          }
          break;
        }
    }
  }

  footswitch.update();

  // Encoder turned
  if (dir) {
    if (es == None) { // first movement activates Select mode
      es = Select;
      draw_selection(palette, selection, false);
    } else if (es == Select) {
      change_selection(palette, dir);  // Move selection
    } else if (es == Change) {
      //  bool fast = (fast_button.read() == HIGH);
      edit_patch(selection, -dir);       // Change parameter
    }
  }

  // Footswitch pressed
  if (footswitch.pressed()) {
    if (es == Select) {
      clear_selection(selection);
      es = None;

    }
    if (es != Change) {  // Patch change disabled during editing
      change_patch(1);

    }
  }
}

Your code is incomplete and does not compile.

Only call "pixels.show()" once for each iteration of "loop()".

if (digitalRead(BypassFSW) == HIGH)
  {
    //pixels.setPixelColor(0, the_missing_color_array[current_patch]);
    if (current_patch == 0) pixels.setPixelColor(0, r0, g0, b0); // Yellow
    else if (current_patch == 1) pixels.setPixelColor(0, r1, g1, b1); // Green
    else if (current_patch == 2) pixels.setPixelColor(0, r2, g2, b2);  // Red
    else if (current_patch == 3) pixels.setPixelColor(0, r3, g3, b3); // Grey
    else if (current_patch == 4) pixels.setPixelColor(0, r4, g4, b4); // Blue
    else if (current_patch == 5) pixels.setPixelColor(0, r5, g5, b5); // Magenta
    else if (current_patch == 6) pixels.setPixelColor(0, r6, g6, b6); // Orange
    else if (current_patch == 7) pixels.setPixelColor(0, r7, g7, b7); // Cyan
    pixels.show();
  }

If you use an array for colors, all the if and else would be a one liner..

1 Like

You meant to use some brackets.

    if (current_patch == 4) {
      pixels.setPixelColor(0, r4, g4, b4); // Blue
      pixels.show();
    }

For any real advice, please post the entire program and an adequate description of its functions.

a7

1 Like

thanks guys, alto777's brackets fixed it

Yikes.

{ } braces
[ ] brackets
( ) parentheses

There is no excuse for ever using the wrong term. Sry.

And you don't need to call braces "curly". That is redundant.

a7

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