Welcome! Log In Create A New Profile

Advanced

Ramps 1.6 and AccelStepper Library

Posted by bkarpuz 
Ramps 1.6 and AccelStepper Library
May 20, 2023 06:48PM
Dear RepRap forum members,

I would like to run my Nema 17 motors connected to Arduino Mega 2560+Ramps 1.6+Drv8825 with the AccelStepper library.
I have the mechanical connections set properly as I can run my motors by using Marlin.

I want to move the motors without using Marlin, I want to do this directly from AccelStepper.
To this end I have checked the file Marlin\src\pins\ramps\pins_RAMPS.h and got
// Steppers
//
#define X_STEP_PIN                            54
#define X_DIR_PIN                             55
#define X_ENABLE_PIN                          38
#ifndef X_CS_PIN
  #define X_CS_PIN                            53
#endif
Then, I tried to use this and run my x-motor with the following code.
// Include the AccelStepper Library
//#include (AccelStepper.h) //Correct Parenthesis

#define X_STEP_PIN                            54
#define X_DIR_PIN                             55
#define X_ENABLE_PIN                          38

// Define motor interface type
#define MotorInterfaceType 1

// Creates an instance
AccelStepper myStepper(MotorInterfaceType,X_STEP_PIN,X_DIR_PIN);

void setup() {
  // set the maximum speed, acceleration factor,
  // initial speed and the target position
  pinMode(X_ENABLE_PIN,OUTPUT);
  digitalWrite(X_ENABLE_PIN,LOW);

 // myStepper.setCurrent(0);
  
 // myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(2000);
  myStepper.setSpeed(1000);
  myStepper.moveTo(1600);
  
}

void loop() {
  // Change direction once the motor reaches target position
  if (myStepper.distanceToGo() == 0) 
    myStepper.moveTo(-myStepper.currentPosition());

  // Move the motor one step
  myStepper.run();
}
But, I dont see any movement. What do you think can be the problem?

Thank you.
bkarpuz

Edited 1 time(s). Last edit at 05/20/2023 06:49PM by bkarpuz.
Re: Ramps 1.6 and AccelStepper Library
May 21, 2023 02:31PM
After some research, I was able to write a working code, which is given below.
#include 

#define X_STEP_PIN        54
#define X_DIR_PIN         55
#define X_ENABLE_PIN      38

#define Number_of_turns   1
#define Rotation_speed    2000


AccelStepper stepper(AccelStepper:grinning smileyRIVER, X_STEP_PIN, X_DIR_PIN); //Replace grinning smiley with :_D without underscore

void setup()
{
  pinMode(X_ENABLE_PIN,OUTPUT);
  digitalWrite(X_ENABLE_PIN,LOW);

  stepper.setMaxSpeed(4000);
  stepper.moveTo(Number_of_turns*16*200);   
  stepper.setSpeed(Rotation_speed);    
}

void loop()
{ 
  if (stepper.distanceToGo() == 0)
  {
    stepper.moveTo(-(stepper.currentPosition()));
    stepper.setSpeed(Rotation_speed);    
  }
  
  while (stepper.distanceToGo() != 0)
  {
    stepper.runSpeedToPosition();    
  }
  delay(100);
}

Edited 1 time(s). Last edit at 05/21/2023 02:32PM by bkarpuz.
Sorry, only registered users may post in this forum.

Click here to login