Libraries not playing nice : AccelStepper + Adafruit_NeoPixel

Hey All,

I'm creating a kinetic light piece with adafruit neopixels mounted to a stepper-motorized linear rail. I'm using Touchdesigner successfully to send an array of lighting and motor parameters via serial commands to the Arduino. Arduino parses the array, then sends the commands to the libraries of the lights and motors. I've gotten everything working great separately using their own respective libraries (Adafruit_NeoPixel.h and AccelStepper.h).

But when I combine their programming into one Arduino sketch, AccelStepper seems to hog the sketch, and my neopixels are super delayed, laggy, and glitchy. I can program pretty well, but
the inner workings of Arduino libraries, as well as chip architecture are a black box to me. I imagine it has something to do with the clock on the chip being overworked, and I assume both the stepper and light library utilize the clock in a big way, but I wanted to ask here if there were limitations or obvious tricks around the way that these libraries to work together.

My dirty solve would be to use to Arduino's for this, but I'd like to avoid that.

Thanks all,
B

Maybe you are using the blocking functions of AccelStepper, but we can't know for sure because your code seems to be a secret.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

A schematic or wiring diagram may be useful, as well.

Post the code, using code tags, and state which Arduino you are using.

LED animations in general are processor-intensive and can take up most or all of a typical Arduino CPU. AccelStepper doesn't necessarily add that much load, but your expectations may be completely unrealistic.

Sorry guys! abbreviated code below.

Using Arduino Mega2560

//setup neopixels
#include <Adafruit_NeoPixel.h>
#define LED_PIN    6
#define LED_COUNT 24
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGBW);


// setup steppers
#include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

char buffer[64];
int len = 0;
int pixel;

//neopixels
int p1;
int p2;
int p3;
int p4;
int p5;

void setup() {
  stepper.setMaxSpeed(100);
  stepper.setAcceleration(300);
  stepper.setCurrentPosition(0);

  Serial.begin(115200);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  //listen for touchdesigner serial command

  if (Serial.available() > 0) {

    // get incoming byte:
    int inByte = Serial.read();
    buffer[len++] = inByte;

    //check for newline
    if (inByte == '\n') {

      buffer[len++] = 0;
      len = 0;

      int n = sscanf(buffer, "%1d, %1d, %1d, %1d, %1d", &p1, &p2, &p3, &p4, &p5);

      //THIS DOESN'T WORK, MY LIGHTS WON'T CHANGE WHILE THE STEPPER IS IN MOTION
      stepper.moveTo(500);
      stepper.runToPosition();

      Serial.print("n=");
      Serial.print(n);
      Serial.println();

      //when entire message arrives, send to neopixels
      if (n == 5) {

        Serial.print(p1);
        pixel = map(p1, 0, 9, 0, 10);
        strip.setPixelColor(0, pixel, 0, 0, 0);

        Serial.print(p2);
        pixel = map(p2, 0, 9, 0, 10);
        strip.setPixelColor(1, pixel, 0, 0, 0);

        Serial.print(p3);
        pixel = map(p3, 0, 9, 0, 10);
        strip.setPixelColor(2, pixel, 0, 0, 0);

        Serial.print(p4);
        pixel = map(p4, 0, 9, 0, 10);
        strip.setPixelColor(3, pixel, 0, 0, 0);

        Serial.print(p5);
        pixel = map(p5, 0, 9, 0, 10);
        strip.setPixelColor(4, pixel, 0, 0, 0);

        strip.show();
      }
    }
  }
}

The nonblocking tip has enabled me to run the stepper at the same time, but it's not smooth, and clearly still fighting the other processes

I'm using

      currentp = stepper.currentPosition();

      if (currentp == 0) {
        stepper.moveTo(500);
      } else if (currentp == 500) {
        stepper.moveTo(0);
      }

  stepper.run();

instead of the "stepper.runToPosition();" which indeed was blocking.

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