I am trying to run two stepper motors in cynch.

I am working on another project, where two step sticks are connected to the Arduino Nano.

I want to run two motors synchronously, so no step that one motor undertakes is not taken by another motor.
I want to modulate a rather weird mathematical function into both motors for my experiment.


(I got a pair of StepSticks not Pololus)
Are they using Software Serial to step motors? Is this really the best way?

So ? What's the big deal ? I took two RAMPS A4988 stepper drivers and wired the STEP & DIR pins together so both got the same
STEP signal and DIR signal and they ran "synchronously" The only way for them to be "synchronous" is to be driven by the same STEP signal. That should be obvious don't you think ?

True. But I may want to separate the two in the future. I should do it on the hardware level.

void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

I should just use a digital write function for all my needs?

No one uses digital writes to drive steppers unless they don't have a REAL stepper driver that takes a STEP signal and DIR signal like the RAMPS or POLOLU A4988. Real men use REAL stepper drivers for multiple reasons (requires fewer pins , easier to change direction, easier to change the speed , etc..) Of course if you are driving a stepper using dgital writes and you map your delay to a
pot connected to an analog input, you can control the speed using the pot.. There is no excuse not to use a REAL stepper driver
at $4.20 each:
http://www.ebay.com/itm/A4988-RAMPS-Pololu-StepStick-stepper-motor-driver-with-heatsink-Prusa-Mendel-/201084529309?ssPageName=ADME:L:OU:US:1120

Step and Dir pins still need to receive input from 2 digital pins, each.
This is what I need.

One is stepped with a pulse
One is held high or low for direction.
Am I correct about that?

Step and Dir pins still need to receive input from 2 digital pins, each.

I don't think so. Not if both drivers are sharing the same arduino output for STEP.
It would be 1 STEP and 2 DIRs, for a total of 3 pins, not 4.
You haven't really explained what you mean by "in sync". (meaning you haven't explained the DIFFERENCE between the motion of one and the motion of the other. If by "in synch" you mean the motors turn in opposite directions but at exactly the same speed, step for step ? If so , that means they are running off the same output pin.

One is stepped with a pulse
One is held high or low for direction.
Am I correct about that?

Yes. One is pulsed , the other is HIGH for one direction, LOW for the other.

You can simply wire the motor coils in parallel. This is what is done on the Z axis of the rep rap to keep the system balanced.
However you can parallel up the step and direction or you can have them seprate and write to both of them at the same time. The time differance between one digital write and another is insignificant when dealing with mechanical movement.

raschemmel:
No one uses digital writes to drive steppers

I do.

It seems an easy way to create the pulses needed for the Step pin and the high or low needed by the Dir pin.

@VT91 if you digitalWrite(motor1, HIGH) immediately followed by digitalWrite(motor2, HIGH) etc they will step together.

Note that a very short the step pulse is needed (perhaps 10 microseconds) so your code should be something like this (for 1 motor).

digitalWrite(motor1, HIGH);
delayMicroseconds(10);
digitalWrite(motor1, LOW);
delay(2000); // assuming you want 1 step every 2 seconds.

In practice the delayMicroseconds() probably isn't needed at all as the time taken by digitalWrite() is sufficient.

If you want long delays (above a few millisecs) you would probably be better to use the timing technique in the Blink Without Delay example sketch.

...R

I forgot to clarify that when I said " noone uses digital writes to drive stpper motors" I meant to drve them usinv Only IN1,IN2,IN3 & IN4
INSTEAD of a Step and Dir pin to drive a dedicated stepper driver like the A4988.

raschemmel:
No one uses digital writes to drive steppers unless they don't have a REAL stepper driver that takes a STEP signal and DIR signal like the RAMPS or POLOLU A4988. Real men use REAL stepper drivers for multiple reasons (requires fewer pins , easier to change direction, easier to change the speed , etc..) Of course if you are driving a stepper using dgital writes and you map your delay to a
pot connected to an analog input, you can control the speed using the pot.. There is no excuse not to use a REAL stepper driver
at $4.20 each:
http://www.ebay.com/itm/A4988-RAMPS-Pololu-StepStick-stepper-motor-driver-with-heatsink-Prusa-Mendel-/201084529309?ssPageName=ADME:L:OU:US:1120

So i am trying to run two stepper motors using the following parts:
http://www.ebay.com/itm/RAMPS-1-4-REPRAP-3D-PRINTER-CONTROLLER-Mega-2560-R3-A4988-driver-5-Heatsink-/141196786542?pt=LH_DefaultDomain_0&var=&hash=item20dffbfb6e

Its the mega arduino RAMPS controller and a few of the A4 drivers, i'm trying to see if there is generic code out there because i am a mega newbie at this stuff and dont really understand what my pin setup is or how to write code that well, any clue?

1-Adjust current limiting IREF
2- Make sur you know the step angle of your motors
3-Pick a direction and set DIR onput accordingly .
4-Pick a speed and use Timer1 or delay(microseconds) with digitalWrites to clock the Step input.
5- Count steps with a stepcounter to create a "position" variable.

raschemmel:
No one uses digital writes to drive steppers unless they don't have a REAL stepper driver that takes a STEP signal and DIR signal like the RAMPS or POLOLU A4988. Real men use REAL stepper drivers for multiple reasons (requires fewer pins , easier to change direction, easier to change the speed , etc..) Of course if you are driving a stepper using dgital writes and you map your delay to a
pot connected to an analog input, you can control the speed using the pot.. There is no excuse not to use a REAL stepper driver
at $4.20 each:
http://www.ebay.com/itm/A4988-RAMPS-Pololu-StepStick-stepper-motor-driver-with-heatsink-Prusa-Mendel-/201084529309?ssPageName=ADME:L:OU:US:1120

I have two StepStick drivers. They send in Step and Dir. But to send Step and Dir commands I still need digital writes?

Not with the Accelstepper Library (#include <AccelStepper.h>)(see below)
This is a program I use with my RAMPS A4988

  //This is an example of how you would control 1 stepper

#include <AccelStepper.h>
byte ledPin = 13;
int motorSpeed = 9680; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 80000; //steps/second/second to accelerate
int mposition=32000;

int motorDirPin = 7; //digital pin 7
int motorStepPin = 8; //digital pin 8

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 



void setup()
{
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  stepper.moveTo(mposition); //move 32000 steps (should be 10 rev)
}

void loop()
{
  
  //if stepper is at desired location
  if (stepper.distanceToGo() == 0)
  {
    //go the other way the same amount of steps
    //so if current position is 400 steps out, go position -400
    stepper.moveTo(-stepper.currentPosition()); 
    for (int i=0;i<4;i++)
    {
     digitalWrite(ledPin, !digitalRead(ledPin));
     delay(150);
    }
   
  }
  
  //these must be called as often as possible to ensure smooth operation
  //any delay will cause jerky motion
  stepper.run();
}

If you DIDN’T have a dedicated stepper driver like you do, you WOULD need to use digitalWrites, like the example below for the L293 H-BRIDGE. (you do not need this code because you HAVE a stepper driver. This is just shown as an example to show you why using stepper driver iinstead of an H-BRIDGE makes programming easier;

 /* Stepper Copal
 * -------------
 *
 * Program to drive a stepper motor coming from a 5'25 disk drive
 * according to the documentation I found, this stepper: "[...] motor 
 * made by Copal Electronics, with 1.8 degrees per step and 96 ohms 
 * per winding, with center taps brought out to separate leads [...]"
 * [http://www.cs.uiowa.edu/~jones/step/example.html]
 *
 * It is a bipolar stepper motor with 5 wires:
 * 
 * - red: power connector, I have it at 5V and works fine
 * - orange and black: coil 1
 * - brown and yellow: coil 2
 *
 * (cleft) 2005 DojoDave for K3
 * http://www.0j0.org | http://arduino.berlios.de
 *
 * @author: David Cuartielles
 * @date: 20 Oct. 2005
 */
int ENA=9; //Connect on Arduino, Pin 9
int ENB=10; //Connect on Arduino, Pin 10
int motorPin1 = 2;
int motorPin2 = 3;
int motorPin3 = 5;
int motorPin4 = 6;
int delayTime = 100;

void setup() 
{
  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  digitalWrite(ENA,HIGH);// Activer moteur A
  digitalWrite(ENB,HIGH);// Activer moteur B
}

void loop() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
}

raschemmel:

Step and Dir pins still need to receive input from 2 digital pins, each.

I don't think so. Not if both drivers are sharing the same arduino output for STEP.
It would be 1 STEP and 2 DIRs, for a total of 3 pins, not 4.

You meant 2 STEP pins and 1 DIR pin, gives separate control as DIR pin is
only sensed on the STEP pin leading edge. For complete synchrony only two
pins are needed of course, but watch out for overloading the pin if driving
several opto-couplers.

Well see that's the thing. The only way I know to make them SYNCHRONOUS is the drive them both with the SAME STEP pin, as I said.(since syncronous seems to imply moving at the same moment, which , the way I see it, means sharing the same step signal.) Maybe you know another way that they can have SEPARATE STEP pins and STILL be SYNCHRONOUS.

Can you explain the numbering a little bit:
#include <AccelStepper.h>
byte ledPin = 13;
int motorSpeed = 9680; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 80000; //steps/second/second to accelerate
int mposition=32000;

What i know is that i have a 200 step, stepper motor. But i’d want to give it different rpms, and always run it continuously until i tell it to stop…

Thanks!

and always run it continuously until i tell it to stop...

The Accel library is not designed to do this. You have to know an end position so you know when to decelerate before a stop. Just stopping a fast moving motor will cause over run, just like trying to run a motor too fast from a stand still will skip steps.

int motorSpeed = 9680; //maximum steps per second (about 3rps / at 16 microsteps)

As you can see, this speed is for microstepping (My driver is configured for 1/16th step )

If you are not microstepping then you need to divide that speed by 16. Everything is STEPS PER SECOND. You already know there are 200 steps per revolution so you should be able to do the conversion to RPM.

You can see from the code I posted that the Accelstepper Library example has a "position" variable that it uses . If you watch this program run in microstepping mode you will see it runs fast and then decelerates before stopping and reversing direction.

this line is giving me an error:
AccelStepper stepper(1, motorStepPin, motorDirPin);

'AccelStepper' does not name a type

???

Did you install the library ?
https://www.pjrc.com/teensy/td_libs_AccelStepper.html