Stepper Joystick Control

Hi All,

I'm trying to control a stepper with a mini joystick via Adafruit motor sheld v2.3 on my Arduino uno.
I want it to stop/not turn when the joystick is centred and accelerate between 0-50rpm in ether direction the further across the joystick is moved. Hopefully that's clear as mud.

The code I have is spiced and hacked along with my joystick. My joystick only reads about +/-13 from 512, not sure why, it's out of an old ps3 controller.

My end goal is to control multiple steppers, dc motors and servos with joysticks and buttons but these steppers are proving to be more difficult than the others.

I managed to get this to compile after many many hours working at it but alas it doesn't run the stepper. Can anyone help me with it please?

#include <Stepper.h>
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"


Adafruit_MotorShield AFMS = Adafruit_MotorShield();


Adafruit_StepperMotor *myMotor = AFMS.getStepper(48, 2);

int num_step = 48;
int val();
int joystick = A1;


void setup()
{
myMotor->setSpeed(0);
}

void loop()
{
  int val=analogRead(joystick);
  if (val >= 509 || val <= 515)
  
  if (val > 515 )
  {
    int K= map (val, 515,527,0,50);  
     int S=map (K, 0,50,0,num_step);
     myMotor->setSpeed(K); //this set the speed of the stepper motor
     myMotor->step(S, MICROSTEP); //this set the direction and the number of step it would take
     
   
    }
   
 if (val < 509 )
  {
    int K= map (val, 498,508,0,50); 
     int S=map (K, 0,50,0,num_step);
     myMotor->setSpeed(K); //this set the speed of the stepper motor
     myMotor->step(-S, MICROSTEP); //this set the direction and the number of step it would take
   }
}

Why are you including these

#include <Stepper.h>
#include <AccelStepper.h>
#include <Wire.h>

when you are not using them ?

This line should NOT exist - I'm not sure why it is there or what it may do

if (val >= 509 || val <= 515)

Add some Serial.println() lines so you can see the values that are being used at different places.

...R

Hi Robin,

Thanks for your response. Not sure why I had all those libraries. They were part of another sketch I copied and pasted. It came up with a whole lot of errors when I deleted "wire.h" so I put it back in.

I removed the other line and added a dead zone for the joystick. I put in serial print and tried it. Still no action on the stepper but the monitor showed that as soon as the joystick moves out of the dead zone it freezes.

I'm still learning, haven't written my own code yet, just try to change other sketches around to work for me. I don't even know if this code is complete and I don't fully understand how this part works? Does there need to be a "write" command after that?

   int spd= map (val, 513,527,0,50);
     int dir=map (spd, 0,50,0,num_step);
     myMotor->setSpeed(spd); 
     myMotor->step(dir, MICROSTEP);

Here is my changed sketch.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();


// Connect a stepper motor with 48 steps per revolution (7.5 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(48, 2);

int num_step = 48;
int val = (0);
int joystick = A1;


void setup()
{
Serial.begin (115200);

myMotor->setSpeed(0);
}

void loop()
{
  val=analogRead(joystick);
  Serial.print( "Motor Speed  ");
  Serial.println(val);
  
  if (val<=513 && val>=509) val=511;
  
  if (val > 513 )
  {
    
    int spd= map (val, 513,527,0,50);
     int dir=map (spd, 0,50,0,num_step);
     myMotor->setSpeed(spd); //this set the speed of the stepper motor
     myMotor->step(dir, MICROSTEP); //this set the direction and the number of step it would take
     
     
   
    }
   
 if (val < 509 )
  {
    int spd= map (val, 498,509,0,50); 
     int dir=map (spd, 0,50,0,num_step);
     myMotor->setSpeed(spd); //this set the speed of the stepper motor
     myMotor->step(-dir, MICROSTEP); //this set the direction and the number of step it would take
   }
}

Ok just had a rethink on what you said and put the serial.println in the right spot. If I move the joystick to the right the serial monitor doesn't freeze but slows right down (prints a result every 750ms ) and I get these results.
spd 3
dir 2
spd 10
dir 9
spd 35
dir 33
spd 42
dir 40

It is uniform from dead zone right through to hard right stick but I cut the rest out to save space.

If I try to move the stick left it freezes. But I do get one print from the left before it does.
spd 44

gyrojeremy:
Ok just had a rethink on what you said and put the serial.println in the right spot.

If I try to move the stick left it freezes.

You haven't posted the code that produces this output, so how can I comment?

I don't know what you mean by "it freezes" either here or in your earlier post.

...R

Sorry about that.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();


// Connect a stepper motor with 48 steps per revolution (7.5 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(48, 2);

int num_step = 48;
int val = (0);
int joystick = A1;


void setup()
{
Serial.begin (115200);

myMotor->setSpeed(0);
}

void loop()
{
  val=analogRead(joystick);
  if (val<=513 && val>=509) val=511;
  
  if (val > 513 )
  {
    
    int Rspd= map (val, 513,525,0,50);  
     int Rdir= map (Rspd, 0,50,0,num_step);
     myMotor->setSpeed(Rspd); //this set the speed of the stepper motor
      Serial.print( "Rspd  ");
  Serial.println(Rspd);
     myMotor->step(Rdir, MICROSTEP); //this set the direction and the number of step it would take
      Serial.print( "     Rdir ");
  Serial.println(Rdir);     
   
    }
   
 if (val < 509 )
  {
    int Lspd= map (val, 509,497,0,50); 
     int Ldir= map (Lspd, 0,50,0,num_step);
     myMotor->setSpeed(Lspd); //this set the speed of the stepper motor
          Serial.print( "Lspd  ");
  Serial.println(Lspd);
     myMotor->step(-Ldir, MICROSTEP); //this set the direction and the number of step it would take
           Serial.print( "     Ldir ");
  Serial.println(-Ldir);
   }
}

I tried a couple different things but none of it worked.

By "Freezing" I mean, stopped printing out any results. I then need to close and reopen the serial monitor for it to work again on right stick, (all be it very slow).

I don't immediately see anything wrong with the code (apart from the awful formatting).

Your code had Lspd and Rspd which is not the same as the output in Reply #3 so I have no way to tell which "side" the output is coming from.

What happens if you comment-out the lines that make the motor move in both of the IF sections (apart from the fact that the motor won't move)?

When you close and re-open the Serial Monitor it causes the Arduino to reset. You should get the same effect by leaving the Monitor open and pressing the reset button.

Put in a statement to print the value produced by analogRead() so that you can check that the mapping is working as you intend.

...R

Ha ha, yeah I don't claim to be a wiz at this by any means. I'm only just beginning.

I changed the spd to Lspd (left speed) and Rspd (right speed) to make it clear witch way I was pressing the joystick. Same as Ldir and Rdir.

Just tried what you suggested and commented out the IF's in different combinations. It seems the problem is with this piece. myMotor->step(Rdir, MICROSTEP);    myMotor->step(-Ldir, MICROSTEP); When I comment out this part and the serial monitor works like it should. Any mod suggestions would be greatly appreciated.

I used this sketch to find the values of the joystick. And I also tried serial print in the joystick value on post 3. Both yelded the same results, 511-512 centre and moves out to about 497 on left stick and up to about 526 right stick.

void setup() {
   // initialize serial communication at 115200 bits per second:
   Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
   // read the input on analog pin :
 analogRead(A0);
 analogRead(A1);
 
 analogRead(A2);  
 analogRead(A3); 
   
   // print out the value you read:
   Serial.print(analogRead(A0));
   Serial.print (" Vertical Left Toggle ");
   Serial.print(analogRead(A1));
   Serial.print (" Horizontal Left Toggle");
   
   
   Serial.print("          ");
   
   Serial.print(analogRead(A2));
   Serial.print (" Vertical Right Toggle ");
   Serial.print(analogRead(A3));
   Serial.println (" Horizontal Right Toggle ");

}

gyrojeremy:
It seems the problem is with this piece.

myMotor->step(Rdir, MICROSTEP);    myMotor->step(-Ldir, MICROSTEP);

Can you post a link to the instructions for that library so I can study it?

My guess is that you are inadvertantly asking it to move 5 million miles so it takes forever.

...R

This is where I downloaded the library from.

The instructions can be found here on the left hand side link list towards the bottom under "Download PDF"

I found this syntax for making the motor move a given number of steps. It is not like your syntax. Could that be the problem? Note that there are no negative values. RTFM ?

Stepper1.step(100, FORWARD, DOUBLE);

...R

I tried to use that to start with but it wouldn't compile. Coarse I don't know how to implement it ether?

Ok I tried this out, it Compiled but still no action on the stepper. The monitor is working (not sure how to explain this), but the monitor prints like it's on variable speed to. From just outside the dead zone the monitor prints out results very slowly, getting faster and faster till the joystick is fully across and the serial monitor prints at full speed. Not sure if that's what should be happening or not or if it's even relevant.

To use the FORWARD, BACKWARD functions I think I had to include the AccelStepper lib and "wrapper" it? with this.

   void forwardstep1() { 
  myStepper->onestep(FORWARD, SINGLE);
}
void backwardstep1() { 
  myStepper->onestep(BACKWARD, SINGLE);
} 
 AccelStepper stepper(forwardstep1, backwardstep1);
  #include <AccelStepper.h>
  #include <Wire.h>
  #include <Adafruit_MotorShield.h>
  #include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
  Adafruit_MotorShield AFMS = Adafruit_MotorShield();


// Connect a stepper motor with 48 steps per revolution (7.5 degree)
// to motor port #2 (M3 and M4)
  Adafruit_StepperMotor *myStepper = AFMS.getStepper(48, 2);

  int num_step = 48;
  int val = (0);
  int joystick = A1;

   void forwardstep1() { 
  myStepper->onestep(FORWARD, SINGLE);
}
void backwardstep1() { 
  myStepper->onestep(BACKWARD, SINGLE);
} 
 AccelStepper stepper(forwardstep1, backwardstep1); 

 void setup()
{
  Serial.begin (115200);
  AFMS.begin();
  myStepper->setSpeed(0);
}

 void loop()
{
  val=analogRead(joystick);
  if (val<=513 && val>=509) val=511;
  
  if (val > 513 )
  {
    
    int Rspd= map (val, 513,525,0,50);  
     int Rdir= map (Rspd, 0,50,0,num_step);
     
     myStepper->setSpeed(Rspd); //this set the speed of the stepper motor
  
  Serial.print( "Rspd  ");
  Serial.println(Rspd);
   
     myStepper->step(FORWARD, SINGLE); //this set the direction and the number of step it would take
  
  Serial.print( "     Rdir ");
  Serial.println(Rdir);     
   
    }
   
 if (val < 509 )
  {
    int Lspd= map (val, 509,497,0,50); 
     int Ldir= map (Lspd, 0,50,0,num_step);
   
     myStepper->setSpeed(Lspd); //this set the speed of the stepper motor
  
  Serial.print( "Lspd  ");
  Serial.println(Lspd);
    
     myStepper->step(BACKWARD,SINGLE); //this set the direction and the number of step it would take
  
  Serial.print( "     Ldir ");
  Serial.println(-Ldir);
   }
}

Just saw this "int Ldir= map (Lspd, 0,50,0,num_step);" in my code when it should something like this right? "FORWARD= map (Rspd, 0,50,0,num_step);" only I get the error "error: lvalue required as left operand of assignment"
what does that mean? or am I way off track?

gyrojeremy:
I tried to use that to start with but it wouldn't compile. Coarse I don't know how to implement it ether?

Let's go back to basics. There is nothing to be gained by adding complexity onto uncertainty.

Post the code that uses the advice straight from the Adafruit document - even if it won't compile.

...R

Ok that code was a mess. I found this one that works like I want but only in one direction, no matter which way I move the joystick it rotates clockwise and doesn't stop completely.

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->   http://www.adafruit.com/products/1438
*/

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 
Adafruit_MotorShield AFMStop(0x60); 
// Connect a stepper motor with 200 steps per revolution (7.5 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper = AFMStop.getStepper(48, 1);

  int LjoyV = A0;
  int direction = (0); 
  int speed = (0);            
  int LVval = (0);
  
 void forwardstep1() { 
  myStepper->onestep(FORWARD, SINGLE);
}
void backwardstep1() { 
  myStepper->onestep(BACKWARD, SINGLE);
} 
 AccelStepper stepper(forwardstep1, backwardstep1); 
 
void setup() {
  
  //Serial.begin(115200);
  AFMStop.begin();
  myStepper->setSpeed(0);
}

void loop() {
  
  LVval = analogRead(LjoyV);
  //Serial.print ("   Joy Val ");
  //Serial.print (LVval);
  
  if(LVval > 513)
  {
    direction = FORWARD; 
    speed = map(LVval, 513, 526, 0, 100);
  }
  
  if(LVval < 509)
  {
    direction = BACKWARD; 
    speed = map(LVval, 509, 496, 0, 100);
  }
  myStepper->setSpeed(speed);
  myStepper->step(direction,SINGLE);
  
  //if (LVval<=513 && LVval>=509) LVval=511;
  //if (LVval=511) myStepper->release();
 
 //Serial.print ("  Speed "); 
 //Serial.print(speed);
 //Serial.print("     Forward ");
 //Serial.print(FORWARD);
 //Serial.print("     Backward ");
 //Serial.println(BACKWARD);

Here is the monitor print out.

   Joy Val 511  Speed 0     Forward 1     Backward 2
   Joy Val 511  Speed 0     Forward 1     Backward 2
   Joy Val 511  Speed 0     Forward 1     Backward 2
   Joy Val 511  Speed 0     Forward 1     Backward 2
   Joy Val 510  Speed 0     Forward 1     Backward 2
   Joy Val 510  Speed 0     Forward 1     Backward 2
   Joy Val 510  Speed 0     Forward 1     Backward 2
   Joy Val 510  Speed 0     Forward 1     Backward 2
   Joy Val 509  Speed 0     Forward 1     Backward 2
   Joy Val 509  Speed 0     Forward 1     Backward 2
   Joy Val 509  Speed 0     Forward 1     Backward 2
   Joy Val 509  Speed 0     Forward 1     Backward 2
   Joy Val 508  Speed 7     Forward 1     Backward 2
   Joy Val 505  Speed 30     Forward 1     Backward 2
   Joy Val 504  Speed 38     Forward 1     Backward 2
   Joy Val 503  Speed 46     Forward 1     Backward 2
   Joy Val 503  Speed 46     Forward 1     Backward 2
   Joy Val 502  Speed 53     Forward 1     Backward 2
   Joy Val 502  Speed 53     Forward 1     Backward 2
   Joy Val 502  Speed 53     Forward 1     Backward 2
   Joy Val 501  Speed 61     Forward 1     Backward 2
   Joy Val 501  Speed 61     Forward 1     Backward 2
   Joy Val 500  Speed 69     Forward 1     Backward 2
   Joy Val 500  Speed 69     Forward 1     Backward 2
   Joy Val 499  Speed 76     Forward 1     Backward 2
   Joy Val 499  Speed 76     Forward 1     Backward 2
   Joy Val 499  Speed 76     Forward 1     Backward 2
   Joy Val 498  Speed 84     Forward 1     Backward 2
   Joy Val 498  Speed 84     Forward 1     Backward 2
   Joy Val 498  Speed 84     Forward 1     Backward 2
   Joy Val 498  Speed 84     Forward 1     Backward 2
   Joy Val 497  Speed 92     Forward 1     Backward 2
   Joy Val 497  Speed 92     Forward 1     Backward 2
   Joy Val 497  Speed 92     Forward 1     Backward 2
   Joy Val 496  Speed 100     Forward 1     Backward 2
   Joy Val 496  Speed 100     Forward 1     Backward 2
   Joy Val 496  Speed 100     Forward 1     Backward 2
   Joy Val 496  Speed 100     Forward 1     Backward 2
   Joy Val 496  Speed 100     Forward 1     Backward 2
   Joy Val 497  Speed 92     Forward 1     Backward 2
   Joy Val 498  Speed 84     Forward 1     Backward 2
   Joy Val 498  Speed 84     Forward 1     Backward 2
   Joy Val 499  Speed 76     Forward 1     Backward 2
   Joy Val 499  Speed 76     Forward 1     Backward 2
   Joy Val 500  Speed 69     Forward 1     Backward 2
   Joy Val 500  Speed 69     Forward 1     Backward 2
   Joy Val 501  Speed 61     Forward 1     Backward 2
   Joy Val 502  Speed 53     Forward 1     Backward 2
   Joy Val 503  Speed 46     Forward 1     Backward 2
   Joy Val 504  Speed 38     Forward 1     Backward 2
   Joy Val 505  Speed 30     Forward 1     Backward 2
   Joy Val 507  Speed 15     Forward 1     Backward 2
   Joy Val 510  Speed 15     Forward 1     Backward 2
   Joy Val 511  Speed 15     Forward 1     Backward 2
   Joy Val 514  Speed 7     Forward 1     Backward 2
   Joy Val 516  Speed 23     Forward 1     Backward 2
   Joy Val 516  Speed 23     Forward 1     Backward 2
   Joy Val 517  Speed 30     Forward 1     Backward 2
   Joy Val 517  Speed 30     Forward 1     Backward 2
   Joy Val 518  Speed 38     Forward 1     Backward 2
   Joy Val 518  Speed 38     Forward 1     Backward 2
   Joy Val 518  Speed 38     Forward 1     Backward 2
   Joy Val 518  Speed 38     Forward 1     Backward 2
   Joy Val 519  Speed 46     Forward 1     Backward 2
   Joy Val 519  Speed 46     Forward 1     Backward 2
   Joy Val 519  Speed 46     Forward 1     Backward 2
   Joy Val 519  Speed 46     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 526  Speed 100     Forward 1     Backward 2
   Joy Val 526  Speed 100     Forward 1     Backward 2
   Joy Val 526  Speed 100     Forward 1     Backward 2
   Joy Val 526  Speed 100     Forward 1     Backward 2
   Joy Val 526  Speed 100     Forward 1     Backward 2
   Joy Val 526  Speed 100     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 525  Speed 92     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 524  Speed 84     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 523  Speed 76     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 522  Speed 69     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 521  Speed 61     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 520  Speed 53     Forward 1     Backward 2
   Joy Val 519  Speed 46     Forward 1     Backward 2
   Joy Val 519  Speed 46     Forward 1     Backward 2
   Joy Val 518  Speed 38     Forward 1     Backward 2
   Joy Val 518  Speed 38     Forward 1     Backward 2
   Joy Val 517  Speed 30     Forward 1     Backward 2
   Joy Val 517  Speed 30     Forward 1     Backward 2
   Joy Val 516  Speed 23     Forward 1     Backward 2
   Joy Val 515  Speed 15     Forward 1     Backward 2
   Joy Val 514  Speed 7     Forward 1     Backward 2
   Joy Val 512  Speed 7     Forward 1     Backward 2
   Joy Val 511  Speed 7     Forward 1     Backward 2
   Joy Val 511  Speed 7     Forward 1     Backward 2
   Joy Val 511  Speed 7     Forward 1     Backward 2
   Joy Val 511  Speed 7     Forward 1     Backward 2

So the speed works great, matches the joystick output but there's something funny going on with the direction.

If the output is produced by the code you posted in Reply #14 then you are printing the wrong thing. FORWARD and BACKWARD are fixed values. What you should be printing is direction.

Just out of curiosity, what happens if you comment out the line

AccelStepper stepper(forwardstep1, backwardstep1);

...R

Tried serial monitor direction. It starts out as 0 (centre), joystick up changes direction to a val of 1 and stays on 1 even when joystick is centred. When I press joystick down the direction val changes to 2 and stays on 2 even when stick is centred. Forward = 1 and Backward = 2 And so on. It doesn't change back to 0.

I tried commenting out that line and made no difference.

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 
Adafruit_MotorShield AFMStop(0x60); 
// Connect a stepper motor with 200 steps per revolution (7.5 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper = AFMStop.getStepper(48, 1);

  int LjoyV = A0;
  int direction = (0); 
  int speed = (0);            
  int LVval = (0);
  
  
 void forwardstep1() { 
  myStepper->onestep(FORWARD, SINGLE);
}
void backwardstep1() { 
  myStepper->onestep(BACKWARD, SINGLE);
} 
 //AccelStepper stepper(forwardstep1, backwardstep1); 
 
void setup() {
  
  //Serial.begin(115200);
  AFMStop.begin();
  myStepper->setSpeed(0);
}

void loop() {
  
  LVval = analogRead(LjoyV);
  //Serial.print ("   Joy Val ");
  //Serial.print (LVval);
  
  if(LVval > 513)
  {
    direction = FORWARD; 
    speed = map(LVval, 513, 526, 0, 100);
  }
  
  if(LVval < 509)
  {
    direction = BACKWARD; 
    speed = map(LVval, 509, 496, 0, 100);
  }
  myStepper->setSpeed(speed);
  myStepper->step(direction,SINGLE);
  
  //if (LVval<=513 && LVval>=509) LVval=511;
  //if (LVval=511) myStepper->release();
 
 //Serial.print ("  Speed "); 
 //Serial.print(speed);
 //Serial.print("     Forward ");
 //Serial.print(FORWARD);
 //Serial.print("     Direction ");
 //Serial.println(direction);  
 

}
 Direction 0
     Direction 0
     Direction 0
     Direction 0
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 1
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2
     Direction 2

gyrojeremy:
Tried serial monitor direction. It starts out as 0 (centre), joystick up changes direction to a val of 1 and stays on 1 even when joystick is centred. When I press joystick down the direction val changes to 2 and stays on 2 even when stick is centred. Forward = 1 and Backward = 2 And so on. It doesn't change back to 0.

That's what I would expect. There are only two directions. Direction is irrelevant when you are stationary.

I tried commenting out that line and made no difference.

That's what I thought. It does not inspire confidence when there is unnecessary code in a program.

...R

I ordered a Pololu driver that should be here tomorrow, so will have a tinker with some new (hopefully simpler) code. There seems to be a bit more info/codes out there for those.