# CRANE GAME

## Step 2: Control a stepper with joy stick

Next we need to control a stepper motor the potentiometer on the joystick.

Hardware:
Joystick -> Arduino (analog in) -> Arduino (digital out) -> Stepper driver -> stepper motor

I ran this using the EasyDriver from Sparkfun by www.SchmalzHaus.com/EasyDriver
It is well labeled and can run a medium sized stepper motor from the USB power supply. (in this example I am using a NEM17 from Keling Technology).

The screw sheild is by ZachHoeken: http://reprap.org/bin/view/Main/Arduino_Breakout_1_4

Code to control stepper motor from joystick potentiometer

///////////////////////   Arduino ///////////////////////

//******* POT to control a stepper speed and direction ******//

//declare pins
int potPin = 1;
int Step_X = 13;
int Dir_X = 12;
int Enable_X = 8;

//declare values
int Speed_X = 0;       //step speed (delay between steps)
int val= 0;
int j = 0;

void setup() {
pinMode(Step_X, OUTPUT);
pinMode(Dir_X, OUTPUT);
pinMode(Enable_X, OUTPUT);

//Serial.begin(9600); // note that serial comm can be used to debug
// but it will slow down the code and slow down the stepper motor alot
//(and be confusing to me)
}

void loop() {

j = val - 517;  // 517 is center positions - how far from center?
j = abs(j);      //absolute value
Speed_X = 70000/j;  //This math inverts the value and scales as needed
//(value found through trial and error)
// The delay between steps will determine the speed of the motor
// So, delay up = speed down

if (val >= 520){
digitalWrite(Enable_X,LOW); // enable
digitalWrite(Dir_X, HIGH);  // Set direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);

}
if (val <= 510) {
digitalWrite(Enable_X,LOW);// enable
digitalWrite(Dir_X, LOW);  // Other direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);
}

if (val <=520 && val >= 510) {
digitalWrite(Enable_X,HIGH);  // disable the stepper motor if the joystic is in the center
}
}

Remove these ads by Signing Up
bstacy3711 months ago
So Marc, this is almost inline with a project I'm starting. I would like to create a 2 servo mechanism that controls pitch and yaw for a telescope. I have a couple of easy step controllers and a joystick ( 27800 2 axis ), I'm new to arduino so I am unsure if I need to incorporate that into the design - such as the coding. Have you come across a design for this sort of project? Thanks

bill
ammorphis1 year ago
help me
can you send me the code for two stepper; Stepper X, Stepper Y for pan and tilt http://www.youtube.com/watch?v=ZulNF2enrd0
i have 2 stepper motors, 2 EasyDriver, Arduino uno and Joistick
black_daysro@yahoo.com
yel-shourafa2 years ago
sorry i forgot to say : As you know the two POTs will be the joystick ;)
yel-shourafa2 years ago
yeah me too i have to control two steppers which have 20 n.m torque '' big motors '' and i have the stepper drivers.
the motors are installed in a 4 wheeler cart.
i made a code to control the cart forward and backward using the motors+drivers+Arduino uno+potentiometer
and then i made another code without the potentiometer
using 3 Arduinos ( 1 master and two slaves) each slave is connected to a motor
the code is to apply speed to one of the motors for ex 10 rpm and the other motor
5 rpm .. just to make the cart turns left or right ...
THE QUESTION NOW WHAT IS THE CODE FOR THE STEERING USING THE POTENTIOMETER ??
there will be 2 POTs one for steering and the other for moving.
juande30032 years ago
Hello,

I need your help, I'm doing a project similar to yours, a pan tilt head and I need to control two stepper motors with a joystick.
Can you help me with the code? How would two stepper motors?

I'd appreciate if you can send photos or a wiring diagram.

Thank you very much.
marc.cryan (author)  juande30032 years ago
Do you have to use stepper motors?

There are alot of examples of pan-and-tilt using hobby servo motors.

Here is an example of pan/tilt using servos and joystick: http://www.practicalarduino.com/news/id/411
the code is here: https://github.com/practicalarduino/PanTiltControl/blob/master/PanTiltControl.pde

spark fun has parts: http://www.sparkfun.com/products/10335
2 years ago
Yes, I need to do it with stepper motors, is difficult to code for two engines?
marc.cryan (author)  juande30032 years ago
Yeah -- hobby servos have built in feedback, you just need to tell them the angle and they go. If you use stepper motors you need a circuit to drive the motors and some sensors to get feedback on the position of the motors. On the other hand, stepper motors are usually sturdy with more power; so it might be worth it.

Good luck!
2 years ago
Thanks for responding,
I like to use stepper motors because they have strength and are accurate. Project where you use your joystick, you use a stepper motor, could you help me with the code to use 2 stepper motors?