0

Lag with Button pin x Neopixel strip
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Lag with Button pin x Neopixel strip

by avelaz on Mon Jan 08, 2018 10:02 pm

I made a modification to the original Neopixel button cycler code so each of the commands run on a continuous loop. Per another post on this forum, I moved the startShow(showType) command outside of the if statement and that did the trick.

But now I'm running into a significant lag when I press the button to change the modes. I have to hold it down for 5-10 seconds for it to switch - and in some cases it won't change at all. I'm not sure what went wrong as I only made that one adjustment to the code.

Help?

Disclaimer: I'm very new to Arduino so I'm still trying to make sense of the code. See below-

Code: Select all | TOGGLE FULL SIZE
bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
    }
  }
        startShow(showType);

// For looping:
//You need to separate counting switch presses from using the count.
//You use the count in the call to startShow(). Move that call outside of the body of the if statement.

  // Set the last button state to the old state.
  oldState = newState;
}
// Button sequences
void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1:  colorWipe(strip.Color(255, 0, 0), 50); // Red
             colorWipe(strip.Color(0, 255, 0), 50); // Green
             colorWipe(strip.Color(0, 0, 255), 50); // Blue
            break;
    case 2: theaterChase(strip.Color(127, 127, 127), 50); // White
            break;
    case 3: theaterChase(strip.Color(127,   0,   0), 50); // Red
            break;
    case 4: theaterChase(strip.Color(  0,   0, 127), 50); // Blue
            break;
    case 5: rainbow(20);
            break;
    case 6: rainbowCycle(20);
            break;
    case 7: theaterChaseRainbow(50);
            break;
  }
}

// Sequennce breakdown



// 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(25);
  }
}

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);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

avelaz
 
Posts: 5
Joined: Tue Dec 12, 2017 6:15 pm

Re: Lag with Button pin x Neopixel strip

by caitlinsdad on Mon Jan 08, 2018 10:46 pm

If you are a beginner, I would suggest you go through the Adafruit tutorials on "Multi-tasking the Arduino" https://learn.adafruit.com/search?q=multi-task&
You will find code samples there to use and find the explanation of some of the problems you are running in to. When I was first programming Arduino I thought I did things correctly but it acted differently and now I know why. Good luck.

caitlinsdad
 
Posts: 531
Joined: Fri Feb 24, 2012 4:23 pm

Please be positive and constructive with your questions and comments.