Using two joysticks to control two steppers simultaneously on the same Uno

Hello all,

I originally got my code to work with one joystick controlling one stepper motor. From that point I thought it would be just simple modification to control another stepper with another joystick. However, this has been stumping me for some time now.

My setup:

Motor Drivers:

1 ULN2003
1 A4988

Motors:

1 5V 28BYJ-48 Stepper
1 12V D8-MOTOR80

Joysticks:

2 Arduino Joystick with switch


Code:

// stepper motor library
#include <Stepper.h>

// # of steps per revolution
#define STEPS 64

// IN1 connected to pin 2, etc etc
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5

#define sleepPin 7
#define stepPin 8
#define dirPin 9

//Initializes Stepper.h by defining pins used and their function(STEPS).
Stepper stepperk(STEPS, IN4, IN2, IN3, IN1);
Stepper stepperg(STEPS, dirPin, stepPin);

#define x_pin A0
#define x_pin_1 A1

#define SW_pin 12 // the pin that the joystick button is attached to
#define solPin 13 // the pin that the solenoid is attached to

int val_sol = 0;

void setup() {
// put your setup code here, to run once:

pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(sleepPin, OUTPUT);

digitalWrite(sleepPin, HIGH);
delay(5);

pinMode(SW_pin, INPUT);
Serial.begin(9600);
digitalWrite(SW_pin, HIGH);
pinMode (solPin, OUTPUT);
}

void loop()
{
//
int val = analogRead(x_pin);

//deadzone so not constantly running

if( (val > 500) && (val < 523) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

else
{
// move the motor in the one direction
while (val >= 523)
{
// defining speed as an int and mapping it to the value of the
// x_pin from 523 to 1023 from 5 rpm to 500
int speed_ = map(val, 523, 1023, 50, 500);

// defining motor speed as above
stepperk.setSpeed(speed_);

//direction of step (really is the step amount but using -1 vs. 1 changes the direction
stepperk.step(-1);

val = analogRead(x_pin);
}

while (val <= 500)
{

int speed_ = map(val, 500, 0, 50, 500);

stepperk.setSpeed(speed_);

stepperk.step(1);

val = analogRead(x_pin);
}

}
// read the Switch on the joystick
val_sol = digitalRead(SW_pin);
if (val_sol == 1) // Change this to a do while if we want sound to play while the “kick” is winding up. Sound should go in the do part theoretically.
{
digitalWrite(solPin, LOW); // Inverted because it is a pull-solenoid
}
else
{

digitalWrite(solPin, HIGH);
delay(100); //delay between wind up and kick. Need to change if using a lever to increase speed.
//digitalWrite(solPin, LOW);
}

int val_1 = analogRead(x_pin_1);

if( (val_1 > 500) && (val_1 < 523) )
{
digitalWrite(sleepPin, LOW);
}
else
{
// move the motor in the one direction

digitalWrite(sleepPin, HIGH);
delay(5);

while (val_1 >= 523)
{
// defining speed as an int and mapping it to the value of the
// x_pin from 523 to 1023 from 5 rpm to 500
int speed_1 = map(val_1, 523, 1023, 50, 500);

// defining motor speed as above
stepperg.setSpeed(speed_1);

//direction of step (really is the step amount but using -1 vs. 1 changes the direction
stepperg.step(-1);

val_1 = analogRead(x_pin_1);
}

while (val_1 <= 500)
{

int speed_1 = map(val_1, 500, 0, 50, 500);

stepperg.setSpeed(speed_1);

stepperg.step(1);

val_1 = analogRead(x_pin_1);
}

}
}


Assuming my wiring is correct, what are the reasons for it not running either motor (also the solenoid doesn’t activate when the switch is pressed anymore. It did with my original code for just one joystick to one stepper)? Any and all suggestions are very much appreciated.

Best,

CarrotNub

28BYJ-48 has 2048 steps per revolution of the output shaft, no idea of D8-MOTOR80, post a link to the datasheet and/or seller's web page.

Hi all,

I got to work by switching the while statements to if and if else statements. the problem was that when it was in a while loops, it wasn’t “listening” to the other motor because it was still in the while loop. This doesn’t happen with if statements.