Hello all.
I'm hoping someone can guide me down the right path. I'm fairly new to the microcontroller world and I've been struggling with this project for too long now. After re-writing this code a couple of dozen times, I am looking for help completing it or at least "showing me the light".
The project I'm working requires a stepper motor on a linear rail to be controlled via a joystick. The customer wants to be able to move the joystick slightly to make the carriage move slowly. The more the joystick is pushed, the faster it should go. The carriage needs to home at start-up, then the operator can move the carriage with the joystick. The system will have a limit switch at each end of the linear rail and if the operator accidently trips one of the switches, the carriage/motor is to stop and back off of the switch (25 steps).
I am using an ESP8266 NodeMCU controller, a small logic leveler (to bring the 3.3v signals up to 5v for the stepper driver), a Stepperonline DM556T stepper driver, with a FUYU stepper motor (24v, 2amp) and linear rail package. I am powering joystick through the ESP8266, the switches through a 5v breadboard power board, and a 24v laboratory power supply is powering the motor and driver.
So for starters, the system will home okay but the motor will only move in one direction regardless of joystick direction. I do have control of going from slow to faster speed, but when I release the joystick the motor "slows" to a stop instead of just stopping. I'm not sure where I went wrong. If anyone has any suggestions, I would appreciate them. My code is below with a wiring photo below that.
/* This code uses an Arduino Uno to operate a NEMA23-sized stepper motor using a joystick and DM556T digital stepper driver.
The joystick movement is proportional to the speed in which the motor turns (more movement = faster RPM). */
#include <AccelStepper.h>
// change pins to match your setup.
const byte stepPin = D1;
const byte dirPin = D2;
const byte xJoyPin = A0;
const byte stopDownPin = D6;
const byte stopUpPin = D7;
int stopValUp = 1;
int stopValDown = 1;
int xJoy = 0;
int topSpeed = 2000;
long dirVal = 0;
float highSpeed = 1000;
float highAccel = 500;
float lowSpeed = 125;
float lowAccel = 50;
long initialHome = -1;
long endStopUp;
long endStopDown;
long location1;
long location2;
long location3;
long location4;
long location5;
AccelStepper xStepper(AccelStepper::DRIVER, stepPin, dirPin);
/* ======================================================= */
void setup() {
Serial.begin(115200);
xStepper.setMaxSpeed(highSpeed);
xStepper.setAcceleration(highAccel);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(xJoyPin, INPUT);
pinMode(stopDownPin, INPUT_PULLUP);
pinMode(stopUpPin, INPUT_PULLUP);
delay(5);
xStepper.setMinPulseWidth(20);
/* Serial.println(" ");
Serial.println("Stepper motor is homing....."); */
// motorHome();
xStepper.setMaxSpeed(highSpeed);
xStepper.setAcceleration(highAccel);
}
/* ======================================================= */
/* (((((((((((((((((((((((((((())))))))))))))))))))))))))) */
void loop() {
Serial.println("1");
jStick();
}
/* (((((((((((((((((((((((((((())))))))))))))))))))))))))) */
/* _______________________________________________________ */
void motorHome() {
xStepper.setMaxSpeed(lowSpeed);
xStepper.setAcceleration(lowAccel);
while (digitalRead(stopDownPin)) {
xStepper.moveTo(initialHome);
initialHome--;
xStepper.run();
}
delay(750);
xStepper.setCurrentPosition(0);
xStepper.setMaxSpeed(lowSpeed);
xStepper.setAcceleration(lowAccel);
initialHome = 50;
xStepper.moveTo(initialHome);
xStepper.run();
initialHome = -1;
delay(750);
xStepper.setMaxSpeed(75);
xStepper.setAcceleration(50);
while (digitalRead(stopDownPin)) {
xStepper.moveTo(initialHome);
initialHome--;
xStepper.run();
}
while (!digitalRead(stopDownPin)) {
xStepper.moveTo(initialHome);
xStepper.run();
initialHome++;
}
delay(750);
xStepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
xStepper.setMaxSpeed(highSpeed);
xStepper.setAcceleration(highAccel);
}
/* _______________________________________________________ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This function reads the limit switches and joystick value. After reading the switches, sends
the code to the checkStop function where it will be determined whether or not the motor has
tripped a switch and if so will stop the motor and move off of the stop 25 steps.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
void jStick() {
Serial.println("2");
checkStop(); /* checkStop function will handle any limit switches
that are tripped. If limit switches are fine, code
will continue and motor will run based on Joystick values */
if (stopValUp == 1 && stopValDown == 1) {
xJoy = analogRead(xJoyPin); /* reads the joystick values */
if (xJoy > 520 && xJoy < 550) { /* if joystick is @ neutral postition, motor does not run */
dirVal = 0;
moveMotor();
}
if (xJoy < 521 || xJoy > 550) { /* if joystick is out of neutral position.... */
xStepper.setMaxSpeed(lowSpeed); /* set speed and acceleration to slow speed */
xStepper.setAcceleration(lowAccel);
if (xJoy < 521 && xJoy > 150) { /* steps motor once relative to current position */
dirVal = (xStepper.currentPosition() - 1);
moveMotor();
}
if (xJoy > 550 && xJoy < 850) { /* steps motor once relative to current position */
dirVal = (xStepper.currentPosition() + 1);
moveMotor();
}
}
if (xJoy < 151 || xJoy > 849) {
/* if joystick is located all of the way to one side
this 'if' statement will increase the speed of the motor */
xStepper.setMaxSpeed(highSpeed);
xStepper.setAcceleration(highAccel);
if (xJoy < 151) {
dirVal = (xStepper.currentPosition() - 1);
moveMotor();
}
if (xJoy > 849) {
dirVal = (xStepper.currentPosition() + 1);
moveMotor();
}
} /* return to loop */
}
}
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This function moves the stepper motor one step
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
void moveMotor() {
Serial.println("3");
xStepper.move(dirVal);
xStepper.run();
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
/* **************************************************************************************
This function will determine if either limit switch has been tripped. If one has been
tripped, the function stops the motor, changes it's direction, and decreases the speed.
The code is then send to the 'moveFromStop' function.
************************************************************************************** */
void checkStop() {
Serial.println("4");
stopValUp = (digitalRead(stopUpPin)); /* reads limit switches */
stopValDown = (digitalRead(stopDownPin));
if (stopValUp == 0 || stopValDown == 0) {
dirVal = 0;
moveMotor();
delay(500);
xStepper.setMaxSpeed(lowSpeed);
xStepper.setAcceleration(lowAccel);
moveFromStop();
}
}
/* ******************************************************* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This function moves the carriage 25 steps from the limit switch that was tripped.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
void moveFromStop() {
Serial.println("5");
if (stopValUp == 0) {
endStopUp = xStepper.currentPosition();
dirVal = (endStopUp - 25);
xStepper.moveTo(dirVal);
xStepper.run();
delay(500);
}
if (stopValDown == 0) {
endStopDown = xStepper.currentPosition();
dirVal = (endStopDown + 25);
xStepper.moveTo(dirVal);
xStepper.run();
delay(500);
}
jStick();
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++ */