controlling 6 stepper motors from PC using arduino mega

I started building one project for which i need to connect and control 6 stepper motors. at a time only one stepper motor will be working, but all motors should be connected. i need to use the motors in combinations like... 1st motor 90degrees clockwise then 4th motor 90degrees anticlockwise then 5th motor 90 degrees anticlockwise and so on. these rotation info is passed through my pc to arduino on by one using ide (i don't exactly know what it is).
what i want to do is, make a program which will be having 12 cases for 6 motors clockwise and anticlockwise. and when a particular alphabet is passed to arduino, that very motor rotates and the next alphabet is passed and respectively motor rotates.
i don't know haw to program an arduino, i got one program from this forum only, which was to rotate 1 stepper motor 1st clockwise then anticlockwise.
also i am using pololu a4988 driver board which has direction and step pins.
thanks in advance.
here is the code

// testing a stepper motor with a Pololu A4988 driver board
// on an Uno the onboard led will flash with each step
// as posted on Arduino Forum at http://forum.arduino.cc/index.php?topic=208905.0

byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 50;
byte ledPin = 13;
int pulseWidthMicros = 50;  // microseconds
int millisbetweenSteps = 50; // milliseconds

void setup() 
{ 

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros);
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
  delay(3000);
  

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros);
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
}

void loop() 
{ 

}

what i want to do is, make a program which will be having 12 cases for 6 motors clockwise and anticlockwise. and when a particular alphabet is passed to arduino, that very motor rotates and the next alphabet is passed and respectively motor rotates.
i don't know haw to program an arduino,

What is your question ?

I wrote that sketch that you have included.

To do what you want you need to think of your problem in small pieces.

You need some code to receive characters from the PC. I wrote another demo here which might help with that).

You need some code to choose what happens for each different character. You can use a series of IF statements of a SWITCH statement followed by a series of CASE statements. Look up IF and SWITCH/CASE in the Arduino online reference.

You need a piece of code that makes stepper motor A move X steps in the appropriate direction. The basics for this are in the code you have already posted.

I would start by writing three small sketches, one for each task, so that you can learn without being confused with unnecessary complexity.

I am assuming you only want the motors to move in response to a character being received and more complicated timing of moves is not needed. But that could be incorporated if necessary.

...R

@raschemmel
I need help in programming arduino for rotating 6 stepper motors, one at a time that too when a particular character is passed to the board using usb.
@Robin2
I will check the code and post here the results.

Google "arduino + the topics listed by Robin2

So i finally made this program, but was only able to check it through the serial monitor. i have included some strings to print when a particular case is satisfied. i don't have 6 motors now but i can say that they will work when these arguments are passed.
please comment if any problem may occur.
i have given each motor a number like 1, 2,etc and each motor has to rotate in clockwise and anticlockwise direction. so i have chosen 12 alphabets. if you press f motor 1 will rotate clockwise, if you press F, motor 1 will rotate anticlockwise and similarly for all 6 motors. alphabets are f,F,r,R,b,B. I was not able to post the full code here, so i have attached it.

byte directionPin1 = 1;
byte directionPin2 = 3;
byte directionPin3 = 5;
byte directionPin4 = 7;
byte directionPin5 = 9;
byte directionPin6 = 11;
byte stepPin1 = 2;
byte stepPin2 = 4;
byte stepPin3 = 6;
byte stepPin4 = 8;
byte stepPin5 = 10;
byte stepPin6 = 12;
int numberOfSteps = 50;
byte ledPin = 13;
int pulseWidthMicros = 50;  // microseconds
int millisbetweenSteps = 1; // milliseconds

  void setup()
  {
     Serial.begin(9600);
     
     for (int pins = 1; pins < 13; pins++)
     {
       pinMode(pins, OUTPUT);
     }
  }
  
  void loop()
  {
    if (Serial.available() > 0)
    {
      int inByte = Serial.read();
      
      switch (inByte)
      {
        case 'f':  {   
                      Serial.begin(9600); 
                      Serial.println("Starting Stepper Motor 1 CW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin1, OUTPUT);
                      pinMode(stepPin1, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin1, HIGH);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin1, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin1, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                     
                     delay(20);
                    }
                    break;
        
        case 'F':   {
                      Serial.begin(9600);
                      Serial.println("Starting Stepper Motor 1 CCW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin1, OUTPUT);
                      pinMode(stepPin1, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin1, LOW);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin1, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin1, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                     
                     delay(20);
                    }
                    break;
                    
         case 'r':  {
                      Serial.begin(9600); 
                      Serial.println("Starting Stepper Motor 2 CW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin2, OUTPUT);
                      pinMode(stepPin2, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin2, HIGH);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin2, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin2, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                    }
                    break;
        
         case 'R':  {
                      Serial.begin(9600);
                      Serial.println("Starting Stepper Motor 2 CCW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin2, OUTPUT);
                      pinMode(stepPin2, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin2, LOW);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin2, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin2, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                    }
                    break;
                    
         case 'b':  {
                      Serial.begin(9600); 
                      Serial.println("Starting Stepper Motor 3 CW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin3, OUTPUT);
                      pinMode(stepPin3, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin3, HIGH);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin3, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin3, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                    }
                    break;
                    
         case 'B':  {
                      Serial.begin(9600);
                      Serial.println("Starting Stepper Motor 3 CCW");
                      digitalWrite(ledPin, LOW);
  
                      delay(1);

                      pinMode(directionPin3, OUTPUT);
                      pinMode(stepPin3, OUTPUT);
                      pinMode(ledPin, OUTPUT);
                      
                      digitalWrite(directionPin3, LOW);
                      for(int n = 0; n < numberOfSteps; n++) 
                      {
                        digitalWrite(stepPin3, HIGH);
                        delayMicroseconds(pulseWidthMicros);
                        digitalWrite(stepPin3, LOW);
    
                        delay(millisbetweenSteps);
    
                        digitalWrite(ledPin, !digitalRead(ledPin));
                      }
                    }
                    break;

         default:   {
                    }
                    break;
      }
    }
  }

Stepper_Motor_Switch_Case_Full.ino (11.7 KB)

I haven't studied you code closely but it seems at first glance that the code in each CASE statement is the same except that it addresses a different motor.

Wouldn't it be much easier to have a single version of that code in a function that is called with the appropriate motor ID as a parameter. And instead of directionPin1, directionPin2 etc, but those items in an array.

I strongly suspect that you are going to have to replace delay(millisBetweenSteps) with the Blink Without Delay technique. Using delay() is fine for a simple demo but it will hardly be appropriate with 6 motors.

If you put the motor code in a single function and if the motor IDs and other parameters are in arrays it will be easy to arrange it so that any or all motors just move a single step for each iteration of loop. Which also seems more appropriate when you have multiple motors.

...R

making a function is really a good idea. But i didn't get the part

I strongly suspect that you are going to have to replace delay(millisBetweenSteps) with the Blink Without Delay technique. Using delay() is fine for a simple demo but it will hardly be appropriate with 6 motors.

and i don't want my motors to move a single step at a time, so i will just program it for 50 steps at a time.

palashrc0945:
and i don't want my motors to move a single step at a time, so i will just program it for 50 steps at a time.

A motor moving 50 steps is just moving 50 single steps whatever way you code it. All I have in mind is code that would allow all the motors to move at the same time rather than having to wait until another motor finishes. I didn't mean that the gaps between the single steps would be perceptible.

If you want the motors to move at the same time the use of delay() will be a problem.

If it's not necessary to have 2 or more motors moving at the same time these issues may be irrelevant.

...R

i strictly don't want to move more than one motor. and i really need the delay after each motor finishes moving 50 steps.
apart from that is there any problem?

If your code does what you want it is fine.

I am just trying to draw your attention to the difference between .A. causing a "wait" period using the delay() function (which paralyses the Arduino until the delay period finishes) and .B. causing the "wait" by using the Blink Without Delay technique which allows the Arduino to do other stuff while working through the "wait" period.

...R

Sorry for replying this late but i completed my project using these 6 Nema 17 stepper motors.

I want to thank everyone for your help and support.
Here's the link for the video.