Skip to content

Instantly share code, notes, and snippets.

@joelongstreet
Created July 11, 2014 16:55
Show Gist options
  • Save joelongstreet/c23d568fdb6a1019bd43 to your computer and use it in GitHub Desktop.
Save joelongstreet/c23d568fdb6a1019bd43 to your computer and use it in GitHub Desktop.
Short example on how to control a stepper motor
int M1dirpin = 4;
int M1steppin = 5;
void setup()
{
pinMode(M1dirpin,OUTPUT);
pinMode(M1steppin,OUTPUT);
digitalWrite(M1dirpin,LOW);
}
void loop()
{
delay(3000);
step(50, 1000);
}
// Where steps is total amount of steps taken
// and time is the total amount of time in microseconds
// the steps should take to complete
void step(int steps, int time){
int delayTime = time/steps;
for(int i=0; i<steps; i++){
digitalWrite(M1steppin, LOW);
delayMicroseconds(2);
digitalWrite(M1steppin, HIGH);
delay(delayTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment