i had help writing this code and its finally working so i thought i would post it here if it helps anyone.
using a sanwa 8way joystick with 4 micro switches to control speed on the y axis and direction on the x axis
#include <AccelStepper.h>
#define leftPin 6
#define rightPin 7
#define upPin 8
#define downPin 9
// define the pins for the stepper motor driver
#define step_pin 3
#define dir_pin 4
#define enable_pin 5
// create a stepper motor object
AccelStepper stepper(AccelStepper::DRIVER, step_pin, dir_pin);
// Joystick default positions
int X = 0;
int Y = 0;
int LastY = 0;
int speed = 500;
int lastPrint = 0;
void setup() {
// initialize the serial communication for debugging
Serial.begin(9600);
//Joystick setup
pinMode(downPin, INPUT_PULLUP);
digitalWrite(downPin, HIGH);
pinMode(upPin, INPUT_PULLUP);
digitalWrite(upPin, HIGH);
pinMode(rightPin, INPUT_PULLUP);
digitalWrite(rightPin, HIGH);
pinMode(leftPin, INPUT_PULLUP);
digitalWrite(leftPin, HIGH);
// set the pins for the stepper motor driver as outputs
pinMode(step_pin, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(enable_pin, OUTPUT);
// enable the stepper motor driver
digitalWrite(enable_pin, LOW);
// set the maximum speed and acceleration of the stepper motor
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
}
void loop() {
// Read joystick settings.
// 0 = no movement
// -1 = left or up
// 1 - right or down
joystick( &X, &Y );
// If the Y position of the joystick has changed from last time
if(LastY != Y)
{
// Update LastY for next time through the loop
LastY = Y;
// If Y is up
if(Y == -1)
speed = speed + 50;
// If Y is down
else if (Y == 1)
speed = speed - 50;
// Cap the speed range between 100 and 1000. so we don't go to nothing, or unstable speeds
if (speed < 100)
speed = 100;
if (speed > 1000)
speed = 1000;
Serial.print("new speed = ");
Serial.println(speed);
}
int direction = X;
// Print the new speed and direction
int printval = speed * direction;
// but only if it's changed
if(printval != lastPrint)
Serial.println(speed * direction);
// Update for next time through the loop
lastPrint = printval;
// control the stepper motor
if (direction == 0)
// if the joystick is in the middle, stop the motor
stepper.stop();
else {
// if the joystick is not in the middle, move the motor
stepper.setSpeed(speed * direction);
stepper.runSpeed();
}
// print the motor position for debugging
// Serial.print("Motor = ");
// Serial.println(stepper.currentPosition());
}
void joystick(int* X, int* Y){
*X = 0;
if(!digitalRead(leftPin))
{
*X = -1;
// Serial.println("left");
}
else if(!digitalRead(rightPin))
{
*X = 1;
// Serial.println("right");
}
*Y = 0;
if (!digitalRead(upPin))
{
*Y = -1;
// Serial.println("up");
}
else if(!digitalRead(downPin))
{
*Y = 1;
// Serial.println("down");
}
}
DM542T stepper driver is connected to motor and 12 volts
pul- to ground
dir- to ground
ena- to ground
pul+ to pin3 on uno
dir+ to pin4 on uno
ena + to pin5 on uno
ground on joystick to ground
other joystick pins to pins 6, 7, 8, 9
also can show speed and direction on serial monitor