Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active August 29, 2015 14:21
Show Gist options
  • Save rlemon/d4c5f298f423cabc00ef to your computer and use it in GitHub Desktop.
Save rlemon/d4c5f298f423cabc00ef to your computer and use it in GitHub Desktop.
/*
* stepper pins
*/
int Pin0 = 2;
int Pin1 = 3;
int Pin2 = 4;
int Pin3 = 5;
int timeout = 1;
int _step = 0;
int dir = LOW; // I use it as a HIGH/LOW check later, so this makes more sense in my head
int motorpos[9][4] = {
{0,0,0,1},
{0,0,1,1},
{0,0,1,0},
{0,1,1,0},
{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{1,0,0,1},
{0,0,0,0} // no state
};
void setup() {
/*
* Settintg up the output pins
*/
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
}
void loop() {
// some shit
// if ( digitalRead(blah) ) dir = HIGH
moveMotor();
}
void moveMotor() {
/*
* basically we need to pulse through a pattern to move the motor. sucks doesn't it.
*/
digitalWrite(Pin0, motorpos[_step][0]);
digitalWrite(Pin1, motorpos[_step][1]);
digitalWrite(Pin2, motorpos[_step][2]);
digitalWrite(Pin3, motorpos[_step][3]);
/*
* just toggle dir from LOW to HIGH or vice versa and you'll change directions.
*/
if(dir == HIGH) {
_step++;
} else {
_step--;
}
if(_step>7) {
_step=0;
}
if(_step<0) {
_step=7;
}
delay(timeout); // speed of the 'steps'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment