Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Last active May 11, 2017 16:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoargh/818874b8d1d37b5c2abc to your computer and use it in GitHub Desktop.
Save renatoargh/818874b8d1d37b5c2abc to your computer and use it in GitHub Desktop.
Controlling stepper motor w/ arduino and joystick https://www.youtube.com/watch?v=locew337i4M
// Subscribe to Renatuino Pi channel on YouTube!
// See it here; https://www.youtube.com/watch?v=locew337i4M
int DIRECTION_X = A0;
int COIL1_A = 4;
int COIL1_B = 5;
int COIL2_A = 6;
int COIL2_B = 7;
int DELAY = 2;
void setup() {
pinMode(COIL1_A, OUTPUT);
pinMode(COIL2_B, OUTPUT);
pinMode(COIL1_A, OUTPUT);
pinMode(COIL2_B, OUTPUT);
}
void setStep(int coil1a, int coil1b, int coil2a, int coil2b) {
digitalWrite(COIL1_A, coil1a);
digitalWrite(COIL1_B, coil1b);
digitalWrite(COIL2_A, coil2a);
digitalWrite(COIL2_B, coil2b);
delay(DELAY);
}
void forwards() {
setStep(HIGH, LOW, HIGH, LOW);
setStep(LOW, HIGH, HIGH, LOW);
setStep(LOW, HIGH, LOW, HIGH);
setStep(HIGH, LOW, LOW, HIGH);
}
void backwards() {
setStep(HIGH, LOW, LOW, HIGH);
setStep(LOW, HIGH, LOW, HIGH);
setStep(LOW, HIGH, HIGH, LOW);
setStep(HIGH, LOW, HIGH, LOW);
}
void loop() {
int directionX = analogRead(DIRECTION_X);
if(directionX == 0) {
forwards();
}
if(directionX >= 1000) {
backwards();
}
delay(1);
}
@abel65
Copy link

abel65 commented Sep 28, 2016

hello I have your set up here but I like to slow down my stepper motor 17 is going to fast for my mag loop antenna with variable capacitors
can you help ,

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