controlling 2 stepper motors with a joystick in 2 axis

Hello everyone,
I'm new to the Arduino world.. recently started programming.
I am doing a project right now which needs to control 2 stepper motor with a finger joystick, so that when I move the joystick only 1 stepper will move according to his axis.
I wrote the code, but when I move the joystick the steppers move together and I can't figure out why.

My system built of:
2 stepper motor bipolar
2 l298n
1 finger joystick
Arduino Uno

This is the code:

int DIRECTION_X = A1;
int COIL1_A = 8;
int COIL1_B = 9;
int COIL2_A = 10;
int COIL2_B = 11;

int DIRECTION_Y = A2;
int COIL1_C = 4;
int COIL1_D = 5;
int COIL2_C = 6;
int COIL2_D = 7;
int DELAY = 2;

void setup() {
pinMode(COIL1_A, OUTPUT);
pinMode(COIL2_B, OUTPUT);
pinMode(COIL1_A, OUTPUT);
pinMode(COIL2_B, OUTPUT);
pinMode(COIL1_C, OUTPUT);
pinMode(COIL2_D, OUTPUT);
pinMode(COIL1_C, OUTPUT);
pinMode(COIL2_D, OUTPUT);
}

void setStep(int coil1a, int coil1b, int coil2a, int coil2b) {
digitalWrite(COIL1_A, coil1a);
digitalWrite(COIL1_B, coil1b);

digitalWrite(COIL2_A, coil2a);
digitalWrite(COIL2_B, coil2b);

digitalWrite(COIL1_C, coil1a);
digitalWrite(COIL1_D, coil1b);

digitalWrite(COIL2_C, coil2a);
digitalWrite(COIL2_D, coil2b);

delay(DELAY);
}

void forwards() {
setStep(HIGH, LOW, HIGH, LOW);
setStep(LOW, HIGH, HIGH, LOW);
setStep(LOW, HIGH, LOW, HIGH);
setStep(HIGH, LOW, LOW, HIGH);
}

void backwards() {
setStep(HIGH, LOW, LOW, HIGH);
setStep(LOW, HIGH, LOW, HIGH);
setStep(LOW, HIGH, HIGH, LOW);
setStep(HIGH, LOW, HIGH, LOW);
}

void loop() {
int directionX = analogRead(DIRECTION_X);
int directionY = analogRead(DIRECTION_Y);

if(directionX == 0) {
forwards();
}

if(directionX == 1023) {
backwards();
}

if(directionY == 0) {
forwards();
}

if(directionY == 1023) {
backwards();
}

delay(1);
}

I really appriciate if you guys can help me with that. I really don't know how to solve this. :slight_smile:

stepper motor program exp.txt (1.46 KB)

An easy beginning would be to write a simple program that only reads the joystick values and Serial.print() them on the IDE screen. Then you can easily see the problem with your code logic.

Paul

ArduinoArt:
I wrote the code, but when I move the joystick the steppers move together and I can't figure out why.

You seem to have only two functions to move the motors - forwards() and backwards(). If you want the motors to move separately then you will need separate pairs of functions for both motors - perhaps forward() backwards() moveLeft() and moveRight()

And you will also need to create a separate version of the setStep() function for each motor.

...R
Stepper Motor Basics
Simple Stepper Code

You could save a lot of coding by using the Stepper library. See File->Examples->Stepper->stepper_oneStepAtATime