Arduino Test code:
sample program for scanner x axis move: (change loop from 1000 to 250, for testing out the y axis move).
void setup() {
pinMode(2, OUTPUT); // blue
pinMode(3, OUTPUT); // yellow
pinMode(4, OUTPUT); // green
pinMode(5, OUTPUT); // orange
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
void loop() {
for (int i = 0; i < 1000; i++) {
pulse(2);
pulse(3);
pulse(4);
pulse(5);
}
for (int i = 0; i < 1000; i++) {
pulse(5);
pulse(4);
pulse(3);
pulse(2);
}
}
void pulse(int pinToPulse) {
digitalWrite(pinToPulse, HIGH);
delay(5);
digitalWrite(pinToPulse, LOW);
delay(5);
}
Launchpad listing:
Uploaded to the Launchpad using Energia, so this is actually an arduino C listing.
/*
Listing is for a M430G2533, using Energia as the programmer.
*/
// pins for motor 1
const byte M1_A = P1_4;
const byte M1_B = P1_5;
const byte M1_C = P2_0;
const byte M1_D = P2_1;
// pins for motor 2
const byte M2_A = P2_2;
const byte M2_B = P2_3;
const byte M2_C = P2_4;
const byte M2_D = P2_5;
// pin sequence for stepping motor1 and motor2
const byte M1PINS[] = {M1_A,M1_B,M1_C,M1_D};
const byte M2PINS[] = {M2_A,M2_B,M2_C,M2_D};
// direction constants
const byte D_LEFT = 1;
const byte D_RIGHT = 2;
const byte D_UP = 3;
const byte D_DOWN = 4;
// each step needs to be a pulse a few ms wide, or motor doesn't step.
byte stepDwell = 10;
// pause a few ms between repeating input triggers
byte iPause = 20;
// track the current pin index in the step sequence
byte m1Pin = 0;
byte m2Pin = 0;
// holds the command code, sent over serial
char commandValue = '\0';
// holds the data, sent over serial
int dataValue = 0;
//
void setup() {
Serial.begin(9600);
// setup pins for output
pinMode(M1_A, OUTPUT);
pinMode(M1_B, OUTPUT);
pinMode(M1_C, OUTPUT);
pinMode(M1_D, OUTPUT);
pinMode(M2_A, OUTPUT);
pinMode(M2_B, OUTPUT);
pinMode(M2_C, OUTPUT);
pinMode(M2_D, OUTPUT);
}
//
void loop() {
if (Serial.available()) {
char incomingChar = Serial.read();
if ('\n' == incomingChar) { // eol recvd, so process command and value collected so far
processCommand(commandValue, dataValue);
Serial.print("(");Serial.print(commandValue);Serial.print(dataValue);
Serial.println(") ok >");// reply back to calling device
commandValue = '\0'; // reset
dataValue = 0; // reset
} else if (isDigit(incomingChar)) {
dataValue = (dataValue * 10) + (incomingChar - '0'); // accumulate the data value
} else { // non-digit, so consider as a command code
commandValue = incomingChar; // set the current command
}
}
}
/* -------------- support routines -------------- */
// returns true if 'digit' represents an ascii digit between 0 and 9
boolean isDigit (char digit) {
return (digit >= '0' && digit <= '9');
}
//
void processCommand(char commandValue, int dataValue) {
if ('i' == commandValue) {// set iPause
iPause = dataValue;
return;
} else if ('s' == commandValue) {// set stepDwell
stepDwell = dataValue;
return;
}
// if "move" command, process it
byte direction = 0;
if ('L' == commandValue)
direction = D_LEFT;
else if ('U' == commandValue)
direction = D_UP;
else if ('D' == commandValue)
direction = D_DOWN;
else if ('R' == commandValue)
direction = D_RIGHT;
if (0 != direction) {
// move...
for (int i = 0; i < dataValue; i++)
step(direction);
}
}
//
void step(byte direction) {
switch (direction) {
case D_LEFT:
m1Pin = cShift(-1,m1Pin);
pulse(M1PINS[m1Pin]);
break;
case D_UP:
m2Pin = cShift(1,m2Pin);
pulse(M2PINS[m2Pin]);
break;
case D_DOWN:
m2Pin = cShift(-1,m2Pin);
pulse(M2PINS[m2Pin]);
break;
case D_RIGHT:
m1Pin = cShift(1,m1Pin);
pulse(M1PINS[m1Pin]);
break;
default:
// invalid value, ignore
break;
}
}
//
void pulse(byte pinToPulse) {
digitalWrite(pinToPulse,HIGH);
delay(stepDwell);
digitalWrite(pinToPulse,LOW);
delay(iPause);
}
/*
performs a circular shift of the index indexToShift, by shiftDirection.
operation is done on a 4-bit word. returns the index shifted to.
cShift(1,0) -> 1 (1000 -> 0100), cShift(1,1) -> 2 (0100 -> 0010)
cShift(1,2) -> 3 (0010 -> 0001), cShift(1,3) -> 0 (0001 -> 1000)
cShift(-1,0) -> 3 (1000 -> 0001), cShift(-1,1) -> 0 (0100 -> 1000)
cShift(-1,2) -> 1 (0010 -> 0100), cShift(-1,3) -> 2 (0001 -> 0010)
*/
byte cShift(char shiftDirection, byte indexToShift) {
char shiftedIndex = shiftDirection + indexToShift;
if (shiftedIndex > 3)
shiftedIndex = 0;
else if (shiftedIndex < 0)
shiftedIndex = 3;
return shiftedIndex;
}