Step Motor Jitter

I'm just playing with basic stepper motor program where a Nintendo Wii nunchuck is used to dynamically change the speed and direction of a stepper motor hooked to a motor shield.

The problem I'm running into is running a command that for example says step 1000 steps, gives me a smooth motor movement over the 1000 motor steps.

But when I get into my program that keep adjusting the motor speed as the nunchucks input changes, I get a far less clean or "jittery" yet still successful/synchronized movement of the motor.

Included is the simple code I'm currently using.... Any suggestions to make it run smoother? My assumption is it has to do with the processing time it takes to check the nunchucks inputs.. but I don't know how to fix it.

#include <Wire.h>
#include <ArduinoNunchuk.h>
#include <Stepper.h>

#define BAUDRATE 19200

ArduinoNunchuk nunchuk = ArduinoNunchuk();
const int stepsPerRevolution = 96;
Stepper stepMotor1(stepsPerRevolution, 12,13);
int motorSpeed = 0;
int stepChunkSlow = 1;
int stepChunkFast = 5;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

void setup()
{
 //Serial.begin(BAUDRATE);
 nunchuk.init();
 
 pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
  stepMotor1.setSpeed(200);
  stepMotor1.step(1000);
  stepMotor1.step(-100);
  
}

void loop()
{
  nunchuk.update();
  int analogYraw = nunchuk.analogY;
  //Serial.print(nunchuk.analogY, DEC);Serial.print(' ');
  
  if (analogYraw > 130)
   {
     //Serial.print(' greater then 127');
    motorSpeed = map (analogYraw, 130, 226, 0, 200);
    stepMotor1.setSpeed(motorSpeed);
    if(motorSpeed < 20)
     {
      stepMotor1.step(stepChunkSlow);
     }
    else
     {
      stepMotor1.step(stepChunkFast);
     }
 
   }
  else if (analogYraw < 124)
   { 
     //Serial.print(analogYraw, DEC);
    motorSpeed = map (analogYraw, 124, 27, 0, 200);
     //Serial.print(motorSpeed, DEC);
    stepMotor1.setSpeed(motorSpeed);
    if(motorSpeed < 20)
     {
      stepMotor1.step(-stepChunkSlow);
     }
    else
     {
      stepMotor1.step(-stepChunkFast);
     }
   }
  else
  {
    //Serial.println("Neutural");
  }
  //delay (10);
  
  //Serial.print(analogYraw, DEC);
  //Serial.print(' ');
  //Serial.print(nunchuk.analogY, DEC);
  //Serial.print(' ');
  //Serial.println(motorSpeed, DEC);
 
 
 
 //Prints the Wii nunchucks raw outputs
  //Serial.print(nunchuk.analogX, DEC);
  //Serial.print(' ');
  //Serial.print(nunchuk.analogY, DEC);
  //Serial.print(' ');
  //Serial.print(nunchuk.accelX, DEC);
  //Serial.print(' ');
  //Serial.print(nunchuk.accelY, DEC);
  //Serial.print(' ');
  //Serial.print(nunchuk.accelZ, DEC);
  //Serial.print(' ');
  //Serial.print(nunchuk.zButton, DEC);
  //Serial.print(' ');
  //Serial.println(nunchuk.cButton, DEC);
  
  
}

I'm not familiar with the nunchuck, but I wonder if it is sending data too quickly for the steppers.

...R

Perhaps you are exceeding the max acceleration your motor and load can tolerate without
mis-stepping? You seem not to be doing any acceleration or deceleration in the code.

Seen AccelStepper library?

The output froma nunchuck is inherently noisy you are just seeing that noise on the stepping motor speed. The answer Is to smooth the data by taking a running average of the last n samples.

Thanks Mike, that is on my list of things yet to try.... I have just been discarding that as a solution up to this point, as when I hold the Nunchuck full forward, and monitor the data stream, it appears a solid smooth stream....

I will test by faking an input from the nunchuck via hard setting the variable and see how it acts.

 nunchuk.update();
  //int analogYraw = nunchuk.analogY;
  int analogYraw = 225;
  //Serial.print(nunchuk.analogY, DEC);Serial.print(' ');

Ok so I modified the nunchuck.update() to pretty much fake a perfect stream of 225 (in short full speed forward) The motor still acts identically. That being, it moves, stays in sync fine, just seems to have the ever slightest delay in between steps, that makes it just slightly jitter as it turns..... While during initialization of the script before void loop I just tell it to spin for 1000 steps straight, is very very smooth.

Back at the drawing board....

P.S. Mike, ultimately the nunchuck input will be run through a PID function to smooth it out as well as make it act like I will want. Haven't gotten to that part yet is all.

If this was my problem I would do something to make the code work without the nunchuck - for example using a potentiometer to "pretend" it's a nunchuck.

If that works then the problem is either with the nunchuck or with the way the data is obtained from it.

If the problem is still there using a potentiometer may provide a more stable basis for debugging.

...R

in my last code example above, taking the nunchuck out of the equation is exactly what I did. It had zero effect. The issue is 100% not the existance of a nunchuck.

meliudaj:
in my last code example above, taking the nunchuck out of the equation is exactly what I did. It had zero effect. The issue is 100% not the existance of a nunchuck.

So can you post the code with the nunchuck removed?

HI, can you post a picture of your project and a CAD or picture of your circuit diagram please.
Can you tell us what you are using to power the stepper motors thank you.

Tom........ :slight_smile:

So can you post the code with the nunchuck removed?

This code snippet is removing the input of nunchuck and replacing it with a constant for testing.

nunchuk.update();
  //int analogYraw = nunchuk.analogY;
  int analogYraw = 225;
  //Serial.print(nunchuk.analogY, DEC);Serial.print(' ');

HI, can you post a picture of your project and a CAD or picture of your circuit diagram please.
Can you tell us what you are using to power the stepper motors thank you.

Tom........ smiley

Thanks for the interest in my question. I will upload pictures when I get time.. but as stated above, this is most definitely NOT a hardware/wiring/power issue. The below code snippet from my program run during setup performs exactly as desired. The jittering issue occurs only in the operating loop.

  stepMotor1.setSpeed(200);
  stepMotor1.step(1000);
  stepMotor1.step(-100);

meliudaj , good work
I tried your code.
It work right of the box. I am not much familier with programming.
Your code is a good starting point.

My well played nunchuck is not so accurat anymore. It dont stop right away.
I tried to build at telescope with to motors to move it in to position, controlled by a nunchuck.

Greetings from Denmark