Created
April 4, 2013 23:54
-
-
Save bitbckt/5315460 to your computer and use it in GitHub Desktop.
Teensy 3.0, Easy Driver and Small Stepper from SparkFun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WProgram.h" | |
/* EasyDriver direction and step trigger pins */ | |
#define DIR_PIN 22 | |
#define STEP_PIN 23 | |
/* SparkFun's "Small stepper motor" - stride angle = 7.5 deg.*/ | |
#define STEPS_PER_REVOLUTION 48.0 | |
#define USTEPS_PER_STEP 8.0 | |
#define USTEPS_PER_DEGREE (360.0 / (STEPS_PER_REVOLUTION * USTEPS_PER_STEP)) | |
#define USECS_PER_USTEP 70 | |
void | |
__rotate(unsigned stp, uint8_t dir, float del) | |
{ | |
int i; | |
digitalWrite(DIR_PIN, dir); | |
for (i = 0; i < stp; i++){ | |
digitalWrite(STEP_PIN, HIGH); | |
delayMicroseconds(del); | |
digitalWrite(STEP_PIN, LOW); | |
delayMicroseconds(del); | |
} | |
} | |
/* | |
* Rotate a number of usteps at a certain speed. Negative steps move in reverse. | |
* Speed may be between [0,1], with 1 being fastest. | |
*/ | |
void | |
rotate(int usteps, float speed) | |
{ | |
int dir, steps, i; | |
float usec; | |
steps = abs(usteps); | |
dir = (usteps > 0) ? HIGH : LOW; | |
usec = (1/speed) * USECS_PER_USTEP; | |
__rotate(steps, dir, usec); | |
} | |
/* | |
* Rotate a number of degrees at a certain speed. Negative degrees move in | |
* reverse. Speed may be between [0,1], with 1 being fastest. | |
*/ | |
void | |
rotateDeg(float deg, float speed) | |
{ | |
int dir, steps, i; | |
float usec; | |
dir = (deg > 0) ? HIGH : LOW; | |
steps = abs(deg) * (1 / USTEPS_PER_DEGREE); | |
usec = (1 / speed) * USECS_PER_USTEP; | |
__rotate(steps, dir, usec); | |
} | |
int | |
main(void) | |
{ | |
pinMode(DIR_PIN, OUTPUT); | |
pinMode(STEP_PIN, OUTPUT); | |
for (;;) { | |
rotateDeg(360, .01); | |
rotateDeg(-360, 1); | |
delay(1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment