Last active
August 29, 2015 13:57
-
-
Save dmadisetti/9924841 to your computer and use it in GitHub Desktop.
Quick Arduino script to run a stepper motor
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
/* | |
* | |
* Full step motor procedure | |
* | |
*/ | |
#include <elapsedMillis.h> | |
// Constants | |
#define STEP_PER_REVOLUTION 4 // Probably has more to do with current than steps, but will determine relationship at later point | |
#define RPM 100 | |
#define LOWEST_PIN 3 | |
// Set pins for colors | |
int blue = LOWEST_PIN; | |
int red = LOWEST_PIN + 1; | |
int green = LOWEST_PIN + 2; | |
int black = LOWEST_PIN + 3; | |
// Timer | |
elapsedMillis t; | |
// Wait to match RPM | |
int wait = 60000 / (RPM * STEP_PER_REVOLUTION); | |
// pin stage. initial assignment to blue, red, green and black respectively | |
int pinStage[]={3,1,2,0}; | |
// Pin stage matches the pattern for increment and i > 1 | |
// 1,0,1,0 | |
// 0,1,1,0 | |
// 0,1,0,1 | |
// 1,0,0,1 | |
//int pinStage[]={0,1,2,3}; | |
// Pin stage matches the pattern for increment and i > 1 | |
// 0,0,1,1 | |
// 1,0,0,1 | |
// 1,1,0,0 | |
// 0,1,1,0 | |
void setup(){ | |
pinMode(blue, OUTPUT); | |
pinMode(red, OUTPUT); | |
pinMode(green, OUTPUT); | |
pinMode(black, OUTPUT); | |
} | |
void loop(){ | |
if(t > wait){ | |
t -= wait; | |
int i; | |
for (i = 0; i < 4; i = i + 1) { | |
// Values of pinStage rotates from 0:3. Only turns on 2 highest values for max torque | |
digitalWrite(LOWEST_PIN + i, ((pinStage[i] > 1) ? HIGH : LOW)); | |
// Resets pinStage if too high otherwise adds 1 | |
pinStage[i] = (pinStage[i] == 3) ? 0 : pinStage[i] + 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment