Arduino mega, ramps 1.4 and stepper coding

Hi there, I have some code (below) that is basically to test that all motors/extruder/heated bed work on a 3d printer set-up.
I’m wanting to manipulate the code so that I can control 3 stepper motors at different speeds independently possibly using the mystepper.setSpeed function of the stepper library?

Im not sure how though, can someone help me understand how as this is for a college project that I volunteered for but I’m really struggling! :frowning:

#define X_STEP_PIN 54
#define X_DIR_PIN 55
#define X_ENABLE_PIN 38
#define X_MIN_PIN 3
#define X_MAX_PIN 2

#define Y_STEP_PIN 60
#define Y_DIR_PIN 61
#define Y_ENABLE_PIN 56
#define Y_MIN_PIN 14
#define Y_MAX_PIN 15

#define Z_STEP_PIN 46
#define Z_DIR_PIN 48
#define Z_ENABLE_PIN 62
#define Z_MIN_PIN 18
#define Z_MAX_PIN 19

#define E_STEP_PIN 26
#define E_DIR_PIN 28
#define E_ENABLE_PIN 24

#define Q_STEP_PIN 36
#define Q_DIR_PIN 34
#define Q_ENABLE_PIN 30

#define SDPOWER -1
#define SDSS 53
#define LED_PIN 13

#define FAN_PIN 9

#define PS_ON_PIN 12
#define KILL_PIN -1

#define HEATER_0_PIN 10
#define HEATER_1_PIN 8
#define TEMP_0_PIN 13 // ANALOG NUMBERING
#define TEMP_1_PIN 14 // ANALOG NUMBERING

void setup() {
pinMode(FAN_PIN , OUTPUT);
pinMode(HEATER_0_PIN , OUTPUT);
pinMode(HEATER_1_PIN , OUTPUT);
pinMode(LED_PIN , OUTPUT);

pinMode(X_STEP_PIN , OUTPUT);
pinMode(X_DIR_PIN , OUTPUT);
pinMode(X_ENABLE_PIN , OUTPUT);

pinMode(Y_STEP_PIN , OUTPUT);
pinMode(Y_DIR_PIN , OUTPUT);
pinMode(Y_ENABLE_PIN , OUTPUT);

pinMode(Z_STEP_PIN , OUTPUT);
pinMode(Z_DIR_PIN , OUTPUT);
pinMode(Z_ENABLE_PIN , OUTPUT);

pinMode(E_STEP_PIN , OUTPUT);
pinMode(E_DIR_PIN , OUTPUT);
pinMode(E_ENABLE_PIN , OUTPUT);

pinMode(Q_STEP_PIN , OUTPUT);
pinMode(Q_DIR_PIN , OUTPUT);
pinMode(Q_ENABLE_PIN , OUTPUT);

digitalWrite(X_ENABLE_PIN , LOW);
digitalWrite(Y_ENABLE_PIN , LOW);
digitalWrite(Z_ENABLE_PIN , LOW);
digitalWrite(E_ENABLE_PIN , LOW);
digitalWrite(Q_ENABLE_PIN , LOW);
}

void loop () {

if (millis() %1000 <500)
digitalWrite(LED_PIN, HIGH);
else
digitalWrite(LED_PIN, LOW);

if (millis() %1000 <300) {
digitalWrite(HEATER_0_PIN, HIGH);
digitalWrite(HEATER_1_PIN, LOW);
digitalWrite(FAN_PIN, LOW);
} else if (millis() %1000 <600) {
digitalWrite(HEATER_0_PIN, LOW);
digitalWrite(HEATER_1_PIN, HIGH);
digitalWrite(FAN_PIN, LOW);
} else {
digitalWrite(HEATER_0_PIN, LOW);
digitalWrite(HEATER_1_PIN, LOW);
digitalWrite(FAN_PIN, HIGH);
}

if (millis() %10000 <5000) {
digitalWrite(X_DIR_PIN , HIGH);
digitalWrite(Y_DIR_PIN , HIGH);
digitalWrite(Z_DIR_PIN , HIGH);
digitalWrite(E_DIR_PIN , HIGH);
digitalWrite(Q_DIR_PIN , HIGH);
}
else {
digitalWrite(X_DIR_PIN , LOW);
digitalWrite(Y_DIR_PIN , LOW);
digitalWrite(Z_DIR_PIN , LOW);
digitalWrite(E_DIR_PIN , LOW);
digitalWrite(Q_DIR_PIN , LOW);
}

digitalWrite(X_STEP_PIN , HIGH);
digitalWrite(Y_STEP_PIN , HIGH);
digitalWrite(Z_STEP_PIN , HIGH);
digitalWrite(E_STEP_PIN , HIGH);
digitalWrite(Q_STEP_PIN , HIGH);
delay(1);

digitalWrite(X_STEP_PIN , LOW);
digitalWrite(Y_STEP_PIN , LOW);
digitalWrite(Z_STEP_PIN , LOW);
digitalWrite(E_STEP_PIN , LOW);
digitalWrite(Q_STEP_PIN , LOW);
}

You have chosen a very strange way to write code to test your motors.

I suggest you write a short program that works the way you want with a single motor. Then add another motor, etc.

If you are trying to drive a 3D printer you will need closely coordinated movements between the motors - basically step by step control for each of them. The stepper and AccelStepper libraries are not designed for that.

If all you want to do is prove that the motors work then any simple piece of code should do - see for exanple the second piece of this simple stepper code - the version that uses millis(). It would be easy to extend it to 3 motors - but that is not really necessary just to test the motors.

You may also be interested in stepper motor basics

...R

I didn’t write this, its from the reprap website.
Basically its an experiment set up that the code is for. I’ve attached a simple schematic to help understand but basically I have 1 stepper motor that drives the cylinder, 1 stepper to move the extruder so the extruded polymer doesn’t overlap and then another stepper to feed the filament to the extruder.
And I need a sketch that accelerates the cylinder, and accelerates the extruder head to prevent overlap and has a constant speed for the filament feed. They don’t need to be closely co-ordinated as long as there isn’t any overlap.

Trouble is, I’m struggling with writing code, and the only thing that I can get to work is the code I posted, which I can alter the speed slightly by changing the delay but other than that I don’t really know.

I am willing to pay to get a sketch to do what I want as arduino is something I’m struggling with and this speedbump is stopping me getting data on the polymer for a project.

If you want someone to write code for a fee (not me) you should ask in the Gigs and Collaborations section.

If you want to write the code yourself you will need to produce a much more detailed description of the requirement . I wonder if you are even using the word "accelerate" correctly.

...R

I wrote this code for it, it verifies fine, but when I upload, nothing happens, the motors don’t move at all. I’ve defined each stepper based on the pins that are on the test code (the original code in the post) and that code works, so the pins must be correct, what is wrong with this then?

#include <AccelStepper.h>

//AccelStepper Xaxis(1, 54, 55); // pin 54 = step, pin 55 = direction
//AccelStepper Yaxis(1, 60, 61); // pin 60 = step, pin 61 = direction
//AccelStepper Zaxis(1, 46, 48); // pin 46 = step, pin 48 = direction

AccelStepper Xaxis(1, 54, 55); // pin 54 = step, pin 55 = direction
AccelStepper Yaxis(1, 60, 61); // pin 60 = step, pin 61 = direction
AccelStepper Zaxis(1, 46, 48); // pin 46 = step, pin 48 = direction

void setup() {
Xaxis.setMaxSpeed(400);
Yaxis.setMaxSpeed(400);
Zaxis.setMaxSpeed(400);
Xaxis.setSpeed(45);
Yaxis.setSpeed(25);
Zaxis.setSpeed(80);
}

void loop() {
Xaxis.runSpeed();
Yaxis.runSpeed();
Zaxis.runSpeed();
}

Please post your code properly using code tags so it is easy to read and easy to copy to an editor - like this

#include <AccelStepper.h>

//AccelStepper Xaxis(1, 54, 55); // pin 54 = step, pin 55 = direction
//AccelStepper Yaxis(1, 60, 61); // pin 60 = step, pin 61 = direction
//AccelStepper Zaxis(1, 46, 48); // pin 46 = step, pin 48 = direction

AccelStepper Xaxis(1, 54, 55); // pin 54 = step, pin 55 = direction
AccelStepper Yaxis(1, 60, 61); // pin 60 = step, pin 61 = direction
AccelStepper Zaxis(1, 46, 48); // pin 46 = step, pin 48 = direction

void setup() {
  Xaxis.setMaxSpeed(400);
  Yaxis.setMaxSpeed(400);
  Zaxis.setMaxSpeed(400);
  Xaxis.setSpeed(45);
  Yaxis.setSpeed(25);
  Zaxis.setSpeed(80);
}

void loop() { 
   Xaxis.runSpeed();
   Yaxis.runSpeed();
   Zaxis.runSpeed();
}

I think that code should work - so there must be a wiring problem

Why not make life easier for yourself and start with a single motor like I suggested in Reply #1

Make a pencil drawing of how everything is connected and post a copy of the drawing. (Obviously easier if you just have 1 motor)

…R