Hello all.
I'm a bit confused on what happened when I added some code to my sketch and was hoping someone can tell me what I did to get the strange reaction that I got from my stepper motor.
I have a stepper motor project in which I'm controlling a bi-polar stepper motor (NEMA 23 sized) with an ESP8266 NodeMCU controller and DM556T stepper driver. I have the motor working fine using a joystick (although it's not as fast as I would like it). I am trying to add a couple of hall effect sensors to it to use as end stops. Because I'm also controlling speed (and direction) with the joystick, I have four "sections" in my code that instruct the motor to take a step. There are two of these sections for calculating a "slower" speed and two for calculating a "faster" speed and there is one for each direction. I added a little 'if' statement to each of these to check the sensor value and if tripped, stop the motor. What I got for this sketch is when the joystick hits the "faster" speed section, the motor spins super fast (more like I wanted it to, but wasn't expecting) and then seems to "hang" in one spot, making a whining noise and vibrating. I'm assuming this is because it is getting signals to both coils at the same time, but why? Also why did it all of a sudden go super fast? I didn't change anything to do with the speed at all. I just checked a sensor!
Both sketches are below, but the extra code that I added to each section was this:
if (upStopVal == 0) {
motorStop();
} else {
my original code that didn't change
}
Original sketch:
/* StepperZ_Project_rev28 */
/* Program to control stepper motor with joystick */
#include <AccelStepper.h>
/* ------- Defining Pins --------------------------------- */
const byte stepPin = D1;
const byte dirPin = D2;
const byte enaPin = D5;
const byte upStop = D6;
const byte downStop = D7;
const byte xJoyPin = A0;
/* ------- Pin Variables --------------------------------- */
int xJoy = 0;
int dirVal = -1;
int upStopVal = 1;
int downStopVal = 1;
/* ------- Variables for Calculations & Settings --------- */
long speedVal = 0.0;
long fastSpeed = 50000.0;
long slowSpeed = 100.0;
long accelFast = 500.0;
long accelSlow = 50.0;
long newPos = 0.0;
AccelStepper xStepper(AccelStepper::DRIVER, stepPin, dirPin);
/* ------------------------------------------------------- */
void setup()
/* ------------------------------------------------------- */
{
Serial.begin(115200);
xStepper.setMinPulseWidth(1);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enaPin, OUTPUT);
pinMode(xJoyPin, INPUT);
pinMode(upStop, INPUT);
pinMode(downStop, INPUT);
pinMode(enaPin, LOW);
pinMode(upStop, LOW);
pinMode(downStop, LOW);
delay(5);
xStepper.setMaxSpeed(fastSpeed);
xStepper.setAcceleration(accelFast);
xStepper.setSpeed(0);
}
/* ======================================================= */
void loop()
/* ======================================================= */
{
xJoy = analogRead(xJoyPin); /* check initial value of joystick */
upStopVal = digitalRead(upStop);
downStopVal = digitalRead(downStop);
/*............................................checks for joystick to be "neutral" (no imput) */
if (xJoy >= 520 && xJoy <= 570)
{
digitalWrite(enaPin, HIGH); /* shuts down driver - i.e. no reaction from input */
Serial.println("");
Serial.print("Neutral statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
/*........JOYSTICK PUSHED DOWN................as long as joystick value is between 525 and 25
the motor will run the slow rate in conjuction with
the throw of the joystick. */
if (xJoy <= 520 && xJoy >= 25)
{
dirVal = -1; /* "down" direction */
speedVal = 520 - xJoy;
digitalWrite(enaPin, LOW); /* re-initiates stepper driver */
slowStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Down slow statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
/*........JOYSTICK DOWN ALL THE WAY...........once the joystick value drops below 25, the motor
will run at the fast rate until it reaches max speed. */
if (xJoy < 520 && xJoy < 25)
{
dirVal = -1; /* "down" direction */
speedVal = ((520 - xJoy) * 100);
digitalWrite(enaPin, LOW);
fastStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Down fast statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
/*........JOYSTICK PUSHED UP..................as long as joystick value is between 550 and 1000
the motor will run the slow rate in conjuction with
the throw of the joystick. */
if (xJoy >= 570 && xJoy <= 999)
{
dirVal = 1; /* "up" direction */
speedVal = xJoy - 570;
digitalWrite(enaPin, LOW);
slowStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Up slow statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
/*........JOYSTICK UP ALL THE WAY.............once the joystick value rises above 1000, the motor
will run at the fast rate until it reaches max speed. */
if (xJoy > 570 && xJoy > 999)
{
dirVal = 1; /* "up" direction */
speedVal = ((xJoy - 570) * 100);
digitalWrite(enaPin, LOW);
fastStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Up Fast statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
}
/* ================= SLOW STEP FUNCTION ================== */
void slowStep()
/* ======================================================= */
{
xStepper.setMaxSpeed(slowSpeed);
xStepper.setAcceleration(accelSlow);
xStepper.move(dirVal);
xStepper.setSpeed(speedVal);
}
/* ================== FAST STEP FUNCTION ================= */
void fastStep()
/* ======================================================= */
{
xStepper.setMaxSpeed(fastSpeed);
xStepper.setAcceleration(accelFast);
xStepper.move(dirVal);
xStepper.setSpeed(speedVal);
}
/* ============== MOTOR STOP FUNCTION ==================== */
void motorStop()
/* ======================================================= */
{
xStepper.stop();
speedVal = 0.0;
xStepper.runSpeed();
digitalWrite(enaPin, HIGH); /* shuts down driver */
}
With sensor check:
/* StepperZ_Project_rev28 */
/* Program to control stepper motor with joystick */
#include <AccelStepper.h>
/* ------- Defining Pins --------------------------------- */
const byte stepPin = D1;
const byte dirPin = D2;
const byte enaPin = D5;
const byte upStop = D6;
const byte downStop = D7;
const byte xJoyPin = A0;
/* ------- Pin Variables --------------------------------- */
int xJoy = 0;
int dirVal = -1;
int upStopVal = 1;
int downStopVal = 1;
/* ------- Variables for Calculations & Settings --------- */
long speedVal = 0.0;
long fastSpeed = 50000.0;
long slowSpeed = 100.0;
long accelFast = 500.0;
long accelSlow = 50.0;
long newPos = 0.0;
AccelStepper xStepper(AccelStepper::DRIVER, stepPin, dirPin);
/* ------------------------------------------------------- */
void setup()
/* ------------------------------------------------------- */
{
Serial.begin(115200);
xStepper.setMinPulseWidth(1);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enaPin, OUTPUT);
pinMode(xJoyPin, INPUT);
pinMode(upStop, INPUT);
pinMode(downStop, INPUT);
pinMode(enaPin, LOW);
pinMode(upStop, LOW);
pinMode(downStop, LOW);
delay(5);
xStepper.setMaxSpeed(fastSpeed);
xStepper.setAcceleration(accelFast);
xStepper.setSpeed(0);
}
/* ======================================================= */
void loop()
/* ======================================================= */
{
xJoy = analogRead(xJoyPin); /* check initial value of joystick */
upStopVal = digitalRead(upStop);
downStopVal = digitalRead(downStop);
/*............................................checks for joystick to be "neutral" (no imput) */
if (xJoy >= 520 && xJoy <= 570)
{
digitalWrite(enaPin, HIGH); /* shuts down driver - i.e. no reaction from input */
Serial.println("");
Serial.print("Neutral statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
/*........JOYSTICK PUSHED DOWN................as long as joystick value is between 525 and 25
the motor will run the slow rate in conjuction with
the throw of the joystick. */
if (xJoy <= 520 && xJoy >= 25)
{
if (upStopVal == 0) {
motorStop();
} else {
dirVal = -1; /* "down" direction */
speedVal = 520 - xJoy;
digitalWrite(enaPin, LOW); /* re-initiates stepper driver */
slowStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Down slow statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
}
/*........JOYSTICK DOWN ALL THE WAY...........once the joystick value drops below 25, the motor
will run at the fast rate until it reaches max speed. */
if (xJoy < 520 && xJoy < 25)
{
if (upStopVal == 0) {
motorStop();
} else {
dirVal = -1; /* "down" direction */
speedVal = ((520 - xJoy) * 100);
digitalWrite(enaPin, LOW);
fastStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Down fast statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
}
/*........JOYSTICK PUSHED UP..................as long as joystick value is between 550 and 1000
the motor will run the slow rate in conjuction with
the throw of the joystick. */
if (xJoy >= 570 && xJoy <= 999)
{
if (upStopVal == 0) {
motorStop();
} else {
dirVal = 1; /* "up" direction */
speedVal = xJoy - 570;
digitalWrite(enaPin, LOW);
slowStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Up slow statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
}
/*........JOYSTICK UP ALL THE WAY.............once the joystick value rises above 1000, the motor
will run at the fast rate until it reaches max speed. */
if (xJoy > 570 && xJoy > 999)
{
if (upStopVal == 0) {
motorStop();
} else {
dirVal = 1; /* "up" direction */
speedVal = ((xJoy - 570) * 100);
digitalWrite(enaPin, LOW);
fastStep();
xStepper.runSpeedToPosition();
Serial.println("");
Serial.print("Up Fast statement Joystick = ");
Serial.println(xJoy);
Serial.print("Speed is ");
Serial.println(speedVal);
Serial.println("");
}
}
}
/* ================= SLOW STEP FUNCTION ================== */
void slowStep()
/* ======================================================= */
{
xStepper.setMaxSpeed(slowSpeed);
xStepper.setAcceleration(accelSlow);
xStepper.move(dirVal);
xStepper.setSpeed(speedVal);
}
/* ================== FAST STEP FUNCTION ================= */
void fastStep()
/* ======================================================= */
{
xStepper.setMaxSpeed(fastSpeed);
xStepper.setAcceleration(accelFast);
xStepper.move(dirVal);
xStepper.setSpeed(speedVal);
}
/* ============== MOTOR STOP FUNCTION ==================== */
void motorStop()
/* ======================================================= */
{
xStepper.stop();
speedVal = 0.0;
xStepper.runSpeed();
digitalWrite(enaPin, HIGH); /* shuts down driver */
}