Hi!
I'm working on a project to use multiple stepper motors to move a gantry using joysticks in multiple axis (X, Y, Z, and a rotation). I am using an Arduino Mega 2560 and TB6600 Stepper Motors for this project for reference. I am also using both the AccelStepper and MultiStepper header files.
Currently, I am trying to use 1 joystick(3-axis joystick, with X, Y, and a rotation) to move two individual stepper motors. So in a sense, I'm using it to move in the "X" and "Y" planes. After multiple failed attempts, I came across the YouTube channel, Brainy-Bits, who provided what I thought I needed to move the gantry with the joystick. Here is the video:Arduino Stepper 2 Axis Camera Slider - Set IN and OUT points using a Joystick - Tutorial Part 2 - YouTube
Based on that video, I created this code:
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper X(1, 22, 26);
AccelStepper Y(1, 28, 32);
MultiStepper StepperControl;
//joystick set up
int X_Pin = A0; //analog pin for X
int Y_Pin = A1;
int JoyX =0;
int JoyY =0;
long joystep_x = 0;
long joystep_y = 0;
void setup() {
X.setEnablePin(24);
Y.setEnablePin(30);
X.setMaxSpeed(50000);
X.setAcceleration(50000.0);
X.enableOutputs();
Y.setMaxSpeed(50000);
Y.setAcceleration(50000.0);
Y.enableOutputs();
StepperControl.addStepper(X);
StepperControl.addStepper(Y);
pinMode (X_Pin, INPUT);
pinMode (Y_Pin, INPUT);
}
void loop() {
JoyX = analogRead(X_Pin);
JoyY = analogRead(Y_Pin);
if(JoyX < 450){
joystep_x = X.currentPosition();
joystep_x = joystep_x -20;
}
if(JoyX > 550){
joystep_x = X.currentPosition();
joystep_x = joystep_x +20;
}
if(JoyY < 450){
joystep_y = Y.currentPosition();
joystep_y = joystep_y -20;
}
if(JoyY > 550){
joystep_y = Y.currentPosition();
joystep_y = joystep_y +20;
}
X.moveTo(joystep_x);
Y.moveTo(joystep_y);
while ((X.distanceToGo() != 0) || (Y.distanceToGo() != 0)){
X.runSpeedToPosition();
Y.runSpeedToPosition();
}
}
My current problem:
Before I added the Y, X could go in either direction. Once I added Y, this was not the case. My X-axis only goes in 1 direction, and the Y can go in both directions, but to change directions, the joystick has to go diagonal to do so,which gets the X involved, which is ok, if they could change directions on their own, or in X case, just change directions. When I delete all the code for Y, however, X will now only go in 1 direction, even though it is the same code that originally had it going either way with movement from the joystick.
I have checked how I wired the circuit, and so has other people, and we concluded that it is probable it is not the wiring, but the code.
Thank you to those who read and help me in advance! ![]()