Accelstepper and Adafruit NeoPixel libraries interfering when using Atmega168p

I am currently trying to make a DMX moving light, essentially, I am trying to control two stepper motors and neopixels through DMX and am using an Atmega168p microcontroller. First, I want to get the neopixels working, and right now, the following code works. The neopixels all turn on and then off in the setup, and then I can control them through the DMX.


#include <DMXSerial.h>      // get the libary at:  https://github.com/mathertel/DMXSerial  by: mathertel
#include <Adafruit_NeoPixel.h>
#include <AccelStepper.h>
#include <MultiStepper.h>

// Constants for the program
#define PAN_DIR 9
#define PAN_STEP 10
#define TILT_DIR 4
#define TILT_STEP 3
#define NUM_LEDS 10
#define LED 5
#define STATUS 2

Adafruit_NeoPixel led = Adafruit_NeoPixel(NUM_LEDS, LED, NEO_GRB + NEO_KHZ800);
//AccelStepper pan = AccelStepper(1, PAN_STEP, PAN_DIR);
//AccelStepper tilt = AccelStepper(1, TILT_STEP, TILT_DIR);
//MultiStepper steppers;

int startAddress = 1;  

void setup () {
  DMXSerial.init(DMXReceiver);
  pinMode(STATUS, OUTPUT);
  
  delay(1000);

  led.begin();
  led.show();
  color(255,255,255);
  digitalWrite(STATUS,HIGH);
  delay(1000);
  color(0,0,0);
  digitalWrite(STATUS,LOW);
}


void loop() {
  unsigned long lastPacket = DMXSerial.noDataSince();

  startAddress=1;
  
  int dmxRed = DMXSerial.read(startAddress);
  int dmxGreen = DMXSerial.read(startAddress+1);
  int dmxBlue = DMXSerial.read(startAddress+2);
  int dmxIntensity = DMXSerial.read(startAddress+3);

  if (lastPacket < 10)
  {
    properties(dmxRed,dmxGreen,dmxBlue,dmxIntensity);
  }
  else
  {
    digitalWrite(STATUS,HIGH);
    delay(200);
    digitalWrite(STATUS,LOW);
  }
}

void properties(int red, int green, int blue, int intens)
{
  color(red, green, blue);
  intensity(intens);
}

void color(int redVal, int greenVal, int blueVal)
{
  for (int i=0; i<NUM_LEDS; i++)
  {
    led.setPixelColor(i,redVal,greenVal,blueVal);
  }
  led.show();
}

void intensity(int intens)
{
  led.setBrightness(intens);
  led.show();
}

However, as soon as I uncomment the below piece of code so that I can start implementing the stepper motors, it stops working. The neopixels do not turn on and then off as they are supposed to, and I also can't control the neopixels through the DMX.

AccelStepper pan = AccelStepper(1, PAN_STEP, PAN_DIR);
AccelStepper tilt = AccelStepper(1, TILT_STEP, TILT_DIR);`

When I try the same code using an Arduino Uno, however, it works fine. This makes me think that it's a problem with the microcontroller or how I wired it up to the microcontroller.

Here is my wiring schematic that I'm using

If I could get any assistance to get this working that'd be great.

The problem is most likely a conflict over timer usage (likely Timer1). Check the libraries for options to use a different timer.

Edit: I just remembered that the neopixel library turns off interrupts, which will create havoc with other libraries that depend on them. See The Issue | Using NeoPixels and Servos Together | Adafruit Learning System

I'm using stepper motors not servos, does this still apply, do you know? I have some A4988 stepper motor drivers that I'm going to use. They have a direction and a step pin that get connected to the Atmega168.

Another potential problem is the 168 only has half the ram of the 328. The neopixel library dynamically allocates memory based on the number of pixels. This can chew up significant amounts of memory. Since the processors have no memory protection things can go badly when overwriting memory.

So do you think using an Atmega328 would solve the problem since this is the same chip used in the Arduino Uno?

Don't know and a few minutes of research did not turn up an answer. Accelstepper does not obviously use interrupts, but it does use delayMicroseconds(). There are other stepper libraries, so try one, or do without. You don't need a stepper library with many motor drivers.

Memory problems might be solved with an ATmega328 chip, if you can find one.

I switched the 168 for a 328 (by taking it out of my Arduino Uno) and it worked, so I guess that was the problem.
@oldcurmudgeon @jremington Thank you both for your help

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