////////////////////////////////////////////////////////////////// //©2011 bildr //Released under the MIT License - Please reuse change and share //Using the easy stepper with your arduino //speed is any number from .01 -> 1 with 1 being the fastest // //Modified by R.J. Kreindler Oct 2016 // Rotate 360 degrees clockwise, pause 1 second // then reverse direction and rotate 360 degrees // First use rotateDeg to specify degrees to rotate // then use rotate to specify the number of steps to rotate ///////////////////////////////////////////////////////////////// #define DIR_PIN 2 #define STEP_PIN 3 int clockwiseLED = 6; int counterclockwiseLED = 7; void setup() { pinMode(DIR_PIN, OUTPUT); pinMode(STEP_PIN, OUTPUT); pinMode(clockwiseLED, OUTPUT); pinMode(counterclockwiseLED, OUTPUT); } void loop(){ //Rotate 360 degrees clockwise then 360 degrees counter clockwise // Rotate at 1/10 maximum speed digitalWrite(clockwiseLED, HIGH); //rotate 360 degrees rotateDeg(360, .1); digitalWrite(clockwiseLED, LOW); delay(1000); // Rotate at 1/10 maximum speed digitalWrite(counterclockwiseLED, HIGH); rotateDeg(-360, .1); //Reverse direction, rotate 360-degrees digitalWrite(counterclockwiseLED, LOW); delay(1000); //Rotate 360 degrees clockwise then 360 degrees counter clockwise // The specific number of microsteps can be determined // using (8 microsteps per step) //A 200 step stepper would take 1600 micro steps for one full revolution // Rotate at the fatest speed digitalWrite(clockwiseLED, HIGH); rotate(1600, 1); digitalWrite(clockwiseLED, LOW); delay(1000); // Rotate at the fastest speed digitalWrite(counterclockwiseLED, HIGH); rotate(-1600,1); //reverse digitalWrite(counterclockwiseLED, LOW); delay(1000); } // Functions rotate() and rotateDeg() used in this sketch void rotate(int steps, float speed){ //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement) //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger int dir = (steps > 0)? HIGH:LOW; steps = abs(steps); digitalWrite(DIR_PIN,dir); float usDelay = (1/speed) * 70; for(int i=0; i < steps; i++){ digitalWrite(STEP_PIN, HIGH); delayMicroseconds(usDelay); digitalWrite(STEP_PIN, LOW); delayMicroseconds(usDelay); } } void rotateDeg(float deg, float speed){ //rotate a specific number of degrees (negitive for reverse movement) //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger int dir = (deg > 0)? HIGH:LOW; digitalWrite(DIR_PIN,dir); int steps = abs(deg)*(1/0.225); float usDelay = (1/speed) * 70; for(int i=0; i < steps; i++){ digitalWrite(STEP_PIN, HIGH); delayMicroseconds(usDelay); digitalWrite(STEP_PIN, LOW); delayMicroseconds(usDelay); } }