Created
February 29, 2016 13:06
-
-
Save rofr/e8c41219b780ac3497b3 to your computer and use it in GitHub Desktop.
Rubik: Arduino sketch testing a single 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
/* | |
* Delay between high/low pulses to the motors. | |
* A 90 degree rotation with a 10 millisecond delay will take 1 second. | |
*/ | |
const int STEPDELAY_MICROS = 1500; | |
/* | |
* Motors 0-5 are represented by the letters for the faces of the cube. | |
* Up, Face, Right, Back, Left, Down | |
*/ | |
enum Motor { U, F, R, B, L, D }; | |
enum PinOffset { ENABLE, DIRECTION, STEP, SLEEP, RESET, UNUSED }; | |
/* | |
* The number of arduino pins allocacted per motor, used to calculate the first pin for a particular motor | |
*/ | |
const int PINS_PER_MOTOR = 6; //one for each possible pin + one extra for spacing | |
/* | |
* Direction is important when doing quarter turns. | |
* TODO: verify that this is correct for each motor | |
*/ | |
enum Direction{ CLOCKWISE = HIGH, COUNTERCLOCKWISE = LOW }; | |
/* | |
* Number of steps in full step mode required to make a 90 degree turn | |
* 200 steps per revolution in full step mode | |
*/ | |
const int STEPS_90 = 50; | |
/* | |
* Number of steps to make a 180 degree turn | |
*/ | |
const int STEPS_180 = 100; | |
/* | |
* Macro for calculating first pin of a particular motor | |
* Usage: int pin = BASE_PIN(U) + STEP; | |
*/ | |
#define BASE_PIN(x) (((int)(x)) * PINS_PER_MOTOR + 2) | |
void setup() { | |
int numberOfPinsToConfigure = PINS_PER_MOTOR * 6; | |
for(int i = 0; i < numberOfPinsToConfigure; i++ ) | |
pinMode(i + 2, OUTPUT); | |
Serial.begin(9600); | |
setEnabled(U, true); | |
Serial.println("Starting in 5 seconds"); //allow time to plug in motor power | |
delay(5000); | |
} | |
/* | |
* Turn the first motor 90 degrees forward and back, then 180 degrees forward and back | |
* | |
*/ | |
void loop() { | |
smokeTest(); | |
} | |
void smokeTest(){ | |
Motor motor = U; //the motor to turn | |
int delayBetweenTurns = 1000; | |
int delayBetweenLoops = 5000; | |
Serial.println("turn(U, CLOCKWISE, STEPS_90)"); | |
turn(motor, CLOCKWISE, STEPS_90); | |
delay(delayBetweenTurns); | |
Serial.println("turn(U, COUNTERCLOCKWISE, STEPS_90)"); | |
turn(motor, COUNTERCLOCKWISE, STEPS_90); | |
delay(delayBetweenTurns); | |
Serial.println("turn(U, CLOCKWISE, STEPS_180)"); | |
turn(motor, CLOCKWISE, STEPS_180); | |
delay(delayBetweenTurns); | |
Serial.println("turn(U, COUNTERCLOCKWISE, STEPS_180)"); | |
turn(motor, COUNTERCLOCKWISE, STEPS_180); | |
delay(delayBetweenLoops); | |
} | |
/* | |
* Enable or disable a single motor. | |
* Note that motor is enabled when signal is LOW | |
*/ | |
void setEnabled(Motor motor, boolean value) { | |
int pin = BASE_PIN(motor) + ENABLE; | |
digitalWrite(pin, value ? LOW : HIGH); | |
} | |
/* | |
* Enable or disable all 6 motors | |
*/ | |
void setAllEnabled(boolean value) { | |
for(int i = 0; i < 6; i++) setEnabled((Motor)i, value); | |
} | |
/* | |
* Turn a single motor, a given number of steps in a given direction | |
*/ | |
void turn(Motor motor, Direction direction, int steps) { | |
long startTime = micros(); | |
int step_pin = BASE_PIN(motor) + STEP; | |
int dir_pin = BASE_PIN(motor) + DIRECTION; | |
digitalWrite(dir_pin, direction); | |
for(int step = 0; step < steps; step++) { | |
digitalWrite(step_pin, HIGH); | |
delayMicroseconds(STEPDELAY_MICROS); | |
digitalWrite(step_pin, LOW); | |
delayMicroseconds(STEPDELAY_MICROS); | |
} | |
long duration = micros() - startTime; | |
Serial.print("duration in micros: "); | |
Serial.println(duration); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment