Skip to content

Instantly share code, notes, and snippets.

@jerivas
Last active December 20, 2022 23:18
Show Gist options
  • Save jerivas/5659118 to your computer and use it in GitHub Desktop.
Save jerivas/5659118 to your computer and use it in GitHub Desktop.
Control two stepper motors' speed and direction with two analog inputs (joystick). By Eduardo Rivas, David Escobar and Ángel Moreno for Universidad Don Bosco de El Salvador, 2013.
/*
Dual Stepper Motor Control (Arduino UNO R3)
Controls the speed and direction of two stepper motors
via two analog inputs. Used to achieve two-dimensional
movement controlled by a joystick. End-of-track sensors
prevent damage to the structure and motors.
Based on "Speed Control" example of the Stepper lib.
Please note the Stepper library produces a bipolar sequence.
By swapping the two middle cables, a unipolar seq. is obtained.
By Eduardo Rivas, David Escobar y Ángel Moreno
for Universidad Don Bosco of El Salvador, 2013.
*/
#include <Stepper.h>
// Set steps per revolution
const int PPR = 200;
// Motor Y in pins 10-13
Stepper motor1(PPR, 10,11,12,13);
// Motor X in pins 6-9
Stepper motor2(PPR, 6,7,8,9);
void setup() {
// Pins 2-5 set up as inputs with pullup resistors. Sensors are active-low.
for (int i = 2; i <= 5; i++)
{
pinMode(i, INPUT_PULLUP);
}
}
void loop() {
// MOTOR 1 CONTROL
int speed = 0;
int direction = 1;
// Get joystick position on A1
int position = analogRead(1);
/*
Values between 562 and 1023 mean forward movement. Speed gets
mapped from 0 to 10 and direction stays positive. Sensors will
block any movement if HIGH.
*/
if (position > 562 and digitalRead(2) == HIGH) {
speed = map(position, 562, 1023, 0, 10);
}
/*
Values between 0 and 462 mean backwards movement. Speed gets
mapped from 10 to 0 and direction is negative. Sensors will
block any movement if HIGH.
*/
if (position < 462 and digitalRead(3) == HIGH) {
speed = map(position, 0, 462, 10, 0);
direction = -1;
}
// Values between 462 and 562 mean no movement (joystick center zone)
if (speed > 0) {
motor1.setSpeed(speed);
motor1.step(direction);
}
// MOTOR 2 CONTROL
speed = 0;
direction = 1;
position = analogRead(2);
if (position > 562 and digitalRead(4) == HIGH) {
speed = map(position, 562, 1023, 0, 10);
}
if (position < 462 and digitalRead(5) == HIGH) {
speed = map(position, 0, 462, 10, 0);
direction = -1;
}
if (speed > 0) {
motor2.setSpeed(speed);
motor2.step(direction);
}
}
@mlinarictilen
Copy link

Can I use this code without resitors?

@Altoidian
Copy link

This is great. A beautiful script..clean and simple. Thanks.

@xyz1024
Copy link

xyz1024 commented Sep 21, 2015

I do not know electrons.but My boyis vey interesting in this kind of project.I can only do as what diagrams say.would you pls give the diagrams of how to connect joystick to arduino board and how to connect motors to arduino board.
thanks
My mail :eddie5492001t@yahoo.com.tw

@hatesf
Copy link

hatesf commented Dec 24, 2016

hello ,jerivas
i understand it but if u can how it will be the connection or to check before on the hardware test on proteus 8 .
so post the proteus simulation diagram!

@headHUB
Copy link

headHUB commented Jun 25, 2017

Simple, effective and descriptive. Thank you.

@nandhini12345
Copy link

nandhini12345 commented Jan 9, 2018

hi i want simulation in proteus for the above mentioned program as soon as possible.

thanks

and one more program i want

analog input - stepper motor speed in rpm
using arduino uno
convert it into digital frquency pulses as a output

send me the arduino uno coding with simulation in proteus soonnn
mail id -nandhinipriyass94@gmail.com

@benjami2004
Copy link

Would you please send me the interconnection diagram, or inform where I can find it?
benjami@superig.com.br

@sigmaplastics1
Copy link

I am going to use your code with one stepper. can you tell me how to set up the resistors
Thanks

@majidnezafati
Copy link

Would you please send me schematic of this project
mn.nezafati@gmail.com

@mena1249
Copy link

mena1249 commented Jul 2, 2022

This is a great one. any one can rewrite the code automatically without joystick.
if any comment. mebituassefa12@gmail.com is my address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment