Joystick and Stepper Motor Question

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
HREFAB
 
Posts: 23
Joined: Wed Jul 15, 2015 10:19 pm

Joystick and Stepper Motor Question

Post by HREFAB »

I am looking for some help with interfacing a Joystick with a Stepper motor that will allow me to actuate the joystick (X+ / X-) causing the stepper to move a specific number of steps. I've looked around and muddled with my code (that Actuates the stepper continuous when the Joystick is moved) but can't figure what to code to get specific steps. Any help would be greatly appreciated and marked into the "Beer Payment" book.

Thanks in advance.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Joystick and Stepper Motor Question

Post by Franklin97355 »

Perhaps, if you post your code and a description or drawing of your connections between it all we can help. Please use the code button "</>" or

Code: Select all

 tags when posting code to the forums.

User avatar
HREFAB
 
Posts: 23
Joined: Wed Jul 15, 2015 10:19 pm

Re: Joystick and Stepper Motor Question

Post by HREFAB »

Thanks for the quick reply. I found this code on another site and chopped it up to work (partially) in the way I envision. I have it where the Joystick will actuate CW and CCW depending on the direction, but what I can't figure is how to get it to only move the stepper a certain number of steps. I tried commenting out some of the STEP++ comments and via trial and error I managed to get the joystick to work.

I'm sure that my code has superfluous bits and bobs in it, but I'm just baffled as to how to declare the number of steps (I should mention that I have a sketch that works without the joystick, but When I incorporate it into the sketch/schematic, I can't replicate that behavior.)

I"m also pretty sure that I have more code here than is necessary, but I left the body of it in as it's ALMOST doing what I want. Any suggestions or advice is much appreciated.

Here's the code that I am using; (I edited this tonight after I cleaned up some un-necessary bits...This is now running ALMOST the way I want, I just need to understand how to make the Joystick Control the Steps of the Stepper Motor).

Code: Select all

#define step_pin 3  // Pin 3 connected to Steps pin on EasyDriver
#define dir_pin 2   // Pin 2 connected to Direction pin
#define MS1 5       // Pin 5 connected to MS1 pin
#define MS2 4       // Pin 4 connected to MS2 pin
#define SLEEP 7     // Pin 7 connected to SLEEP pin
#define X_pin A0    // Pin A0 connected to joystick x axis
 
int direction;    // Direction CW/CCW
int steps = 1025; // Value from original Code
 
void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);
   
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up
   
 
/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/
 
   digitalWrite(MS1, LOW);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps
   
}
 
void loop() {
  while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
    if (steps > 0) {
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
     //steps--;
    }      
  }
  
   
    while (analogRead(X_pin) > 900 && analogRead(X_pin) <= 1024) {
      if (steps < 2050) {
        digitalWrite(dir_pin, LOW);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        //steps++;
      }}}

Last edited by HREFAB on Wed Aug 05, 2015 1:14 am, edited 3 times in total.

User avatar
HREFAB
 
Posts: 23
Joined: Wed Jul 15, 2015 10:19 pm

Re: Joystick and Stepper Motor Question

Post by HREFAB »

And here is the way the boards are configured; The UNO has to be powered by a %V supply while the Stepper gets its power independently from a 12VDC Lipo Pack.
Arduino UNO with Sparkfun EasyDriver Board and Thumb Joystick
Arduino UNO with Sparkfun EasyDriver Board and Thumb Joystick
JoyStickStepREV4_bb.jpg (476.64 KiB) Viewed 250 times

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Joystick and Stepper Motor Question

Post by Franklin97355 »

Your code is telling the stepper to move while the joystick is in a certain position but you have no code to do this for a set number of steps. If you want it to do 10 steps and wait your code needs to reflect that. Can you explain in detail what the code should be doing when you move the joystick?

User avatar
HREFAB
 
Posts: 23
Joined: Wed Jul 15, 2015 10:19 pm

Re: Joystick and Stepper Motor Question

Post by HREFAB »

What I am trying to accomplish is to have the stepper motor move a pre-determined number of steps when the joystick is pushed to the right or the left. Depending on the direction, I would like the stepper to run either CW or CCW. I uploaded some amended code (nothing really different than the original) that has some snipping done of code that wasn't necessary.

Now when I press the Joystick to the left, the motor runs counterclockwise until I release the joystick. If I press the Joystick to the right, it runs CW.

I may be asking the joystick to do something that it is incapable of doing and might be better off with a switch, but i thought I would try to make a go of it as I have gotten this far. If I have to code for a simple switch I may have to leap out the window...and as it's on the ground level, I'll just make the neighbors snicker....

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Joystick and Stepper Motor Question

Post by Franklin97355 »

Code: Select all

while (analogRead(X_pin) >= 0 && analogRead(X_pin) <= 100) {
    if (steps > 0) {
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
     //steps--;
    }      
  }
The code is doing what you are telling it to do. You need to tell it to move X number of steps and then stop until you release the joystick then check the joystick again. What are the values for the stick at right, left and center?

You need to check stick, do action and check stick until center then go back to the loop function.

User avatar
HREFAB
 
Posts: 23
Joined: Wed Jul 15, 2015 10:19 pm

Re: Joystick and Stepper Motor Question

Post by HREFAB »

OK then. I will figure out how to get the numbers ( I know there is a tutorial so I'll dig it up) and then see what I can come up with.
I appreciate you sticking with me here.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”