Stepper Motors with Arduino Mega 2560

hi,

i have arduino mega 2560 and ramp 1.4 board (using driver a4988). i am trying to move 2 or more steppers without merlin or. i want my stepper x move forward and come back and then move y stepper forward then come back and so on. but the problem is my x moves then y moves then x moves then y moves. can anyone help me to find out either ramp board is faulty or some thing else because i have 2 ramp 1.4 they both behave same.
thank you in advance.

Here is the Ramps board to Mega pin mapping. >> RAMPS 1.4 and Mega 2650 Arduino Pin numbers

Use a library to control the stepper motors. I like the MobaTools stepper library. There is decent documentation on the use of the library and several example codes.

Post your test code. Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

1 Like

Welcome back.

I think that you should post you sketch (don't forget to use code tags :wink:)

It sounds like it's exactly doing what you described. Or do you want it to do the move of X, next the move of Y and then stop?

If you have something in loop() like

void loop()
{
  moveX(20);
  moveX(0);
  moveY(50);
  moveY(0);
}

That will indeed be repeated as you have described.

Note:
Your topic has been moved to a more suitable location on the forum. Please read the sticky topics in Uncategorized - Arduino Forum.

thank you,

will try and see if this works

regards.

sajid

| sterretje Karma: 1700+
October 1 |

  • | - |

Welcome back.

I think that you should post you sketch (don't forget to use code tags :wink:)

ausajid:

but the problem is my x moves then y moves then x moves then y moves.

ausajid:

i want my stepper x move forward and come back and then move y stepper forward then come back and so on

It sounds like it's exactly doing what you described. Or do you want it to do the move of X, next the move of Y and then stop?

If you have something in loop() like

void loop(){  moveX(20);  moveX(0);  moveY(50);  moveY(0);}

That will indeed be repeated as you have described.

Note:
Your topic has been moved to a more suitable location on the forum. Please read the sticky topics in Uncategorized - Arduino Forum.

hi,

thank you

will see

Here is a tested (on real hardware) program showing one way to do what you say (if I understand what you want). Uses a Mega board with Ramps 1.4 shield, 3 steppers and using the MobaTools stepper library.

The program moves the x stepper to position 800 and right back to 0 then the y stepper moves to position 1600 and right back to 0 then the x stepper moves to position 1000, pauses for 5 seconds and moves back to 0. All of this is using non-blocking code so you can do other things while the sequence is running. I had to mess with the attachEnable() functions a bit to make it work and don't know why. Perhaps @MicroBahner can answer.

// stepper sequence program by groundFungus aka c.goulding

#include <MobaTools.h>

const byte   X_STEP_PIN = 54;
const byte   X_DIR_PIN  = 55;
const byte   X_ENABLE_PIN = 38;

const byte   Y_STEP_PIN  = 60;
const byte   Y_DIR_PIN  =  61;
const byte   Y_ENABLE_PIN =  56;

const byte   Z_STEP_PIN  =  46;
const byte   Z_DIR_PIN  =   48;
const byte   Z_ENABLE_PIN =  62;


const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;
const float adcCountsPerStep = 1023 / STEPS_REVOLUTION;

MoToStepper xStepper( STEPS_REVOLUTION, STEPDIR );
MoToStepper yStepper( STEPS_REVOLUTION, STEPDIR );
MoToStepper zStepper( STEPS_REVOLUTION, STEPDIR );

byte step = 0;
unsigned long timer = 0;  // used to pause at a position.

void setup()
{
   Serial.begin(115200);

   xStepper.attach( X_STEP_PIN, X_DIR_PIN );
   xStepper.attachEnable(X_ENABLE_PIN, 0, 1);
   xStepper.setSpeedSteps(5000); // steps /10
   //xStepper.setSpeed(2000);  // rpm /10
   xStepper.setRampLen(20);
   xStepper.setZero();

   yStepper.attach( Y_STEP_PIN, Y_DIR_PIN );
   xStepper.attachEnable(Y_ENABLE_PIN, 0, 1);
   yStepper.setSpeedSteps(10000); // steps /10
   //yStepper.setSpeed(1000);  // rpm /10
   yStepper.setRampLen(20);
   yStepper.setZero();

   zStepper.attach( Z_STEP_PIN, Z_DIR_PIN );
   zStepper.attachEnable(Z_ENABLE_PIN, 0, 0);
   zStepper.setSpeedSteps(8000); // steps /10
   //zStepper.setSpeed(1000);  // rpm /10
   zStepper.setRampLen(20);
   zStepper.setZero();
}

void loop()
{
   if (step == 0)
   {
      xStepper.moveTo(800);
      step = 1;
   }
   else if (step == 1)
   {
      if (xStepper.distanceToGo() == 0)
      {
         step = 2;
      }
   }
   else if (step == 2)
   {
      xStepper.moveTo(0);
      step = 3;
   }
   else if (step == 3)
   {
      if (xStepper.distanceToGo() == 0)
      {
         step = 4;
      }
   }

   if (step == 4)
   {
      yStepper.moveTo(1600);
      step = 5;
   }
   else if (step == 5)
   {
      if (yStepper.distanceToGo() == 0)
      {
         step = 6;
      }
   }
   else if (step == 6)
   {
      yStepper.moveTo(0);
      step = 7;
   }
   else if (step == 7)
   {
      if (yStepper.distanceToGo() == 0)
      {
         step = 8;
      }
   }

   if (step == 8)
   {
      zStepper.moveTo(1000);
      step = 9;
   }
   else if (step == 9)
   {
      if (zStepper.distanceToGo() == 0)
      {
         step = 10;
         timer = millis();
      }
   }
   else if (step == 10) // pause at position
   {
      if (millis() - timer > 5000)
      {
         step = 11;
      }
   }
   else if (step == 11)
   {
      zStepper.moveTo(0);
      step = 12;
   }
   else if (step == 12)
   {
      if (zStepper.distanceToGo() == 0)
      {
         step = 0;
      }
   }
}

What has been the problem / did not work? I would suggest that the delay between the creation of the steps and the enable pin is not zero ( to be honest, I never tested that :thinking: ). I think it should be at least 1ms. And whether 'active' is low or high depends on the driver ( last parameter ).

hi,

thank you, this is what i want. i will modify it as per my movement duration.

thanks a lot.

regards.

sajid

If your question has been answered to your satisfaction please mark the thread as solved so that other users that may want to help will not waste time opening the thread to help only to find the question has been answered.

And @MicroBahner did answer. He found a typo in my code and once fixed the code works fine with the enable states all set to 0 as it should be. Totally my fault and I apologize to MicroBahner for doubting his library and wasting his time. Here is the corrected code:

// stepper sequence program by groundFungus aka c.goulding

#include <MobaTools.h>

const byte   X_STEP_PIN = 54;
const byte   X_DIR_PIN  = 55;
const byte   X_ENABLE_PIN = 38;

const byte   Y_STEP_PIN  = 60;
const byte   Y_DIR_PIN  =  61;
const byte   Y_ENABLE_PIN =  56;

const byte   Z_STEP_PIN  =  46;
const byte   Z_DIR_PIN  =   48;
const byte   Z_ENABLE_PIN =  62;


const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;
const float adcCountsPerStep = 1023 / STEPS_REVOLUTION;

MoToStepper xStepper( STEPS_REVOLUTION, STEPDIR );
MoToStepper yStepper( STEPS_REVOLUTION, STEPDIR );
MoToStepper zStepper( STEPS_REVOLUTION, STEPDIR );

byte step = 0;
unsigned long timer = 0;  // used to pause at a position.

void setup()
{
   Serial.begin(115200);

   xStepper.attach( X_STEP_PIN, X_DIR_PIN );
   xStepper.attachEnable(X_ENABLE_PIN, 0, 0);
   xStepper.setSpeedSteps(5000); // steps /10
   //xStepper.setSpeed(2000);  // rpm /10
   xStepper.setRampLen(20);
   xStepper.setZero();

   yStepper.attach( Y_STEP_PIN, Y_DIR_PIN );
   yStepper.attachEnable(Y_ENABLE_PIN, 0, 0);  // changed from xStepper
   yStepper.setSpeedSteps(10000); // steps /10
   //yStepper.setSpeed(1000);  // rpm /10
   yStepper.setRampLen(20);
   yStepper.setZero();

   zStepper.attach( Z_STEP_PIN, Z_DIR_PIN );
   zStepper.attachEnable(Z_ENABLE_PIN, 0, 0);
   zStepper.setSpeedSteps(8000); // steps /10
   //zStepper.setSpeed(1000);  // rpm /10
   zStepper.setRampLen(20);
   zStepper.setZero();
}

void loop()
{
   if (step == 0)
   {
      xStepper.moveTo(800);
      step = 1;
   }
   else if (step == 1)
   {
      if (xStepper.distanceToGo() == 0)
      {
         step = 2;
      }
   }
   else if (step == 2)
   {
      xStepper.moveTo(0);
      step = 3;
   }
   else if (step == 3)
   {
      if (xStepper.distanceToGo() == 0)
      {
         step = 4;
      }
   }

   if (step == 4)
   {
      yStepper.moveTo(1600);
      step = 5;
   }
   else if (step == 5)
   {
      if (yStepper.distanceToGo() == 0)
      {
         step = 6;
      }
   }
   else if (step == 6)
   {
      yStepper.moveTo(0);
      step = 7;
   }
   else if (step == 7)
   {
      if (yStepper.distanceToGo() == 0)
      {
         step = 8;
      }
   }

   if (step == 8)
   {
      zStepper.moveTo(1000);
      step = 9;
   }
   else if (step == 9)
   {
      if (zStepper.distanceToGo() == 0)
      {
         step = 10;
         timer = millis();
      }
   }
   else if (step == 10) // pause at position
   {
      if (millis() - timer > 2000)
      {
         step = 11;
      }
   }
   else if (step == 11)
   {
      zStepper.moveTo(0);
      step = 12;
   }
   else if (step == 12)
   {
      if (zStepper.distanceToGo() == 0)
      {
         step = 0;
      }
   }
}

Never trust software completely - a little doubt is always allowed. There is no such thing as completely error-free software :innocent: :joy:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.