I am trying to drive multiple stepper motors in what will hopefully become a CNC machine one day. I first got one spinning using this driver and some NEMA-17 stepper motors. (the step pulses are low active on this controller) Here is the working code for a single motor which is a slightly modified version of the code I found here:
byte directionPin = 4;
byte stepPin = 3;
int maxSpeed = 400;
int steps = 400; //steps in a full rotation
bool dir = true;
unsigned long curMicros;
unsigned long prevStepMicros = 0;
unsigned long microsBetweenSteps = 2000; // microsecond pulse width
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with micros()");
if (microsBetweenSteps < maxSpeed){ //check if max speed has been exceeded
Serial.print("Max Speed Exceeded, speed set to ");
Serial.println(maxSpeed);
microsBetweenSteps = maxSpeed;
}
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
}
void loop() {
curMicros = micros();
if (steps >= 0){ //if a full rotation has not yet been achieved
if (singleStep(microsBetweenSteps,dir)){ //try to turn the motor
steps = steps - 1; //if successful, decrement steps left
}
}
else{ //after a full rotation, wait 1 second and switch directions
steps = 400;
dir = !dir;
delay(1000);
}
}
bool singleStep(int stepspeed, bool direct) {
if (curMicros - prevStepMicros >= stepspeed) { //check if enough time has passed since last movement
prevStepMicros = curMicros;
if (direct){ //set motor direction pin
digitalWrite(directionPin,LOW);
}
else{
digitalWrite(directionPin,HIGH);
}
digitalWrite(stepPin, LOW); //pulse the pin low
digitalWrite(stepPin, HIGH);
return true; //return true if motor moved
}
else{
return false; //return false if the motor did not move
}
}
However, after making some of the variables into arrays, the motor slows down substantially. Here is the code that I am trying to get running:
byte directionPin[2] = {4,7};
byte stepPin[2] = {3,6};
int maxSpeed = 400;
int steps[2] = {400,200};
bool dir[2] = {true,true};
int motorToAdress = 0;
bool currentState[2] = {true,true};
unsigned long curMicros;
unsigned long prevStepMicros[2] = {0,0};
unsigned long microsBetweenSteps[2] = {2000,200}; // microseconds
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with micros()");
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
if (microsBetweenSteps[0] < maxSpeed){
Serial.print("Max Speed Exceeded on motor 0, speed set to ");
Serial.println(maxSpeed);
microsBetweenSteps[0] = maxSpeed;
}
else{
Serial.print("Motor 0 speed: ");
Serial.println(microsBetweenSteps[0]);
}
if (microsBetweenSteps[1] < maxSpeed){
Serial.print("Max Speed Exceeded on motor 1, speed set to ");
Serial.println(maxSpeed);
microsBetweenSteps[1] = maxSpeed;
}
else{
Serial.print("Motor 1 speed: ");
Serial.print(microsBetweenSteps[1]);
Serial.println();
}
Serial.print("Talking to Motor ");
Serial.println(motorToAdress);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
}
void loop() {
curMicros = micros();
if (steps[motorToAdress] >= 0){
if (singleStep(microsBetweenSteps[motorToAdress],dir[motorToAdress])){
steps[motorToAdress] = steps[motorToAdress] - 1;
Serial.println("move");
}
else{
Serial.println("no move");
}
}
else{
steps[motorToAdress] = 400;
dir[motorToAdress] = !dir[motorToAdress];
delay(1000);
}
}
bool singleStep(int stepspeed, bool direct) {
if (curMicros - prevStepMicros[motorToAdress] >= stepspeed) {
prevStepMicros[motorToAdress] = curMicros;
if (direct){
digitalWrite(directionPin[motorToAdress],LOW);
}
else{
digitalWrite(directionPin[motorToAdress],HIGH);
}
digitalWrite(stepPin[motorToAdress],LOW);
digitalWrite(stepPin[motorToAdress],HIGH);
return true;
}
else{
return false;
}
}
Are the array accesses simply taking too long or is there something else that I am missing? Should I switch to an object-oriented approach? Thanks for any light you can shed on this mystery.