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