Moving stepper motor to initial postion and diagonal

Hello, I am trying to control 3 stepper motors of a Cartesian robot. I wrote a program to bring the motors to the initial position and then move them in the diagonal. the problem is, it works fine if I run only the initial position part or the diagonal part, but when I run both of them like in the code below, the motors only move indefinitely on the diagonal e never go to the initial position. Do you know why is this happening? Thank you.

//Definition of the motors and end stops pins
#define xsteps         54
#define xdirection     55
#define endstopx       3
#define ysteps         60
#define ydirection     61
#define endstopy       14
#define zsteps         46
#define zdirection     48
#define endstopz       18
#define X_ENABLE_PIN       38
#define Y_ENABLE_PIN       56
#define Z_ENABLE_PIN       62


void setup() {
  //All the pins for the motors movements are considered outputs, the pins for the endstops
  //are inputs_pullups, used to avoid a float input on the switches
  pinMode (xsteps, OUTPUT);
  pinMode (xdirection, OUTPUT);
  pinMode (ysteps, OUTPUT);
  pinMode (ydirection, OUTPUT);
  pinMode (zsteps, OUTPUT);
  pinMode (zdirection, OUTPUT);
  pinMode(X_ENABLE_PIN    , OUTPUT);
  pinMode(Y_ENABLE_PIN    , OUTPUT);
  pinMode(Z_ENABLE_PIN    , OUTPUT);

  pinMode (endstopx, INPUT_PULLUP);
  pinMode (endstopy, INPUT_PULLUP);
  pinMode (endstopz, INPUT_PULLUP);
  
  digitalWrite(X_ENABLE_PIN    , LOW);
  digitalWrite(Y_ENABLE_PIN    , LOW);
  digitalWrite(Z_ENABLE_PIN    , LOW);
}
//function to move the stepper motor on x direction
void stepsx (){
  digitalWrite (xsteps, HIGH);
  delay(1);
  digitalWrite (xsteps, LOW);
}

//function to move the stepper motor on y direction
void stepsy(){
  digitalWrite (ysteps, HIGH);
  delay(1);
  digitalWrite (ysteps, LOW);
}

//function to move the stepper motor on z direction
void stepsz(){
  digitalWrite (zsteps, HIGH);
  delay(1);
  digitalWrite (zsteps, LOW);
}

//function to move the stepper motor on diagonal
void stepdiagonal(){
  digitalWrite (xsteps, HIGH);
  digitalWrite (ysteps, HIGH);
  delay(1);
  digitalWrite (xsteps, LOW);
  digitalWrite (ysteps, LOW);
}
//function to move the stepper motors to the initial position
void initialposition(){
  int endstopReadx, endstopReady, endstopReadz;

  endstopReadx = digitalRead (endstopx);
  endstopReady = digitalRead (endstopy);
  endstopReadz = digitalRead (endstopz);
  
  if (endstopReadx == LOW){
    digitalRead (endstopx);
    digitalWrite (xdirection, HIGH);
    stepsx();
  }
  if (endstopReady == LOW){
    digitalWrite (ydirection, LOW);
    stepsy();
  }
  if (endstopReadz == LOW){
   
    digitalWrite (zdirection, LOW);
    stepsz();
  }
}
void loop() {
  int count

  count = 0;
  initialposition();
  
  for (count - 0; count<=700, count++){
   digitalWrite (xdirection, LOW);
   digitalWrite (ydirection, HIGH);
   stepdiagonal();
   count++;
}

}

It would seem more logical to me to put the call to initialPosition() in setup() - presumably you only need that to happen once.

This style seems illogical to me

void stepsx (){
  digitalWrite (xsteps, HIGH);
  delay(1);
  digitalWrite (xsteps, LOW);
}

If you imagine that function being called repeatedly you have an interval that defines the length of the pulse but you have nothing that represents the interval between steps. Also the pulse width can be much narrower - 10 microsecs should be enough.

If this was my code it would look like this

void stepsx (){
  digitalWrite (xsteps, HIGH);
  delayMicroseconds(10);
  digitalWrite (xsteps, LOW);
  delay(intervalBetweenSteps);
}

and the value of intervalBetweenSteps could be set to define the speed you want the motor to move at.

Alternatively, omit the line delay(intervalBetweenSteps); from the function and includ it in the code that calls the function - something like

for (n = 0; n < numberOfSteps; n++) {
   stepsx();
   delay(intervalBetweenSteps);
}

However all of that use of delay() is probably not appropriate at all as the Arduino is blocked from doing anything else during a delay(). Have a look at the second example in this Simple Stepper Code which uses micros() and millis() for non-blocking timing.

That’s probably enough for now.

…R
Stepper Motor Basics

Hello, thank you for your response. I did what you said about the initial position on setup and it worked, but when I try to use the code for the simple stepper program using the millis() the motor did not move, it got power but didn't do the steps. I tried to change the millisBetweenSteps, and also put a delay after calling the function like you suggested, but the motors didn't move. Do you know why this might be happening? Thank you again

victorfb:
but when I try to use the code for the simple stepper program using the millis() the motor did not move,

Post the program as YOU uploaded it to your Arduino.

Are you CERTAIN that you have the pin references correct for your stepper driver?

...R

Here is the code I used:

//Program to control the cartesian robot, this program will draw a square and a triangle using
//the robot's stepper motors

//Definition of the motors and end stops pins
#define xsteps         54
#define xdirection     55
#define endstopx       3
#define xenable        38
#define ysteps         60
#define ydirection     61
#define endstopy       14
#define yenable        56
#define zsteps         46
#define zdirection     48
#define endstopz       18
#define zenable        62



int count; //variables to count the steps on x, y and z direction
unsigned long instantMillis; 
unsigned long prevMillis = 0;
unsigned long stepsMillis = 25; // time between steps in milliseconds
void setup() {
  //All the pins for the motors movements are considered outputs, the pins for the endstops
  //are inputs_pullups, used to avoid a float input on the switches
  pinMode (xsteps, OUTPUT);
  pinMode (xdirection, OUTPUT);
  pinMode(xenable, OUTPUT);
  pinMode (ysteps, OUTPUT);
  pinMode (ydirection, OUTPUT);
  pinMode(yenable, OUTPUT);
  pinMode (zsteps, OUTPUT);
  pinMode (zdirection, OUTPUT);
  pinMode(zenable, OUTPUT);

  pinMode (endstopx, INPUT_PULLUP);
  pinMode (endstopy, INPUT_PULLUP);
  pinMode (endstopz, INPUT_PULLUP);

  digitalWrite(xenable, LOW);
  digitalWrite(yenable, LOW);
  digitalWrite(zenable, LOW);

  initialposition();

}

//function to move the stepper motor on x direction
void stepsx (){
  if (instantMillis - prevMillis >= stepsMillis){ //the motor will only take a step when 25ms have elapsed
    prevMillis += stepsMillis;
    digitalWrite (xsteps, HIGH);  
    digitalWrite (xsteps, LOW);
  }
}

//function to move the stepper motor on y direction
void stepsy(){
  if (instantMillis - prevMillis >= stepsMillis){
    prevMillis += stepsMillis;
    digitalWrite (ysteps, HIGH);    
    digitalWrite (ysteps, LOW);
  }
}

//function to move the stepper motor on z direction
void stepsz(){
  if (instantMillis - prevMillis >= stepsMillis){
    prevMillis += stepsMillis;
    digitalWrite (zsteps, HIGH);
    digitalWrite (zsteps, LOW);
  }
}

//function to move the end effector in diagonal
void stepdiagonal(){
  digitalWrite (xsteps, HIGH);
  digitalWrite (ysteps, HIGH);
  delay(1);
  digitalWrite (xsteps, LOW);
  digitalWrite (ysteps, LOW);
 }


//function to put the end effector on the initial position
void initialposition(){
  int endstopReadx, endstopReady, endstopReadz;

  endstopReadx = digitalRead (endstopx);
  endstopReady = digitalRead (endstopy);
  endstopReadz = digitalRead (endstopz);
  
  if (endstopReadx == LOW){
    digitalRead (endstopx);
    digitalWrite (xdirection, HIGH);
    stepsx();
  }
  if (endstopReady == LOW){
    digitalWrite (ydirection, LOW);
    stepsy();
  }
  if (endstopReadz == LOW){
   
    digitalWrite (zdirection, LOW);
    stepsz();
  }
}

void loop() {
  for (count = 0; count<=700; count++){
    digitalWrite( xdirection, LOW);
    stepsx();
  }
}

I am sure the pins are correct because I can move the motors using the delay, as I shown on the first code I posted.

Thank you

I just realized that on this code I didn’t assigned instantMillis to millis() - instantMillis = millis() - on the Loop. But on the code that I ran I did that. this is the correct code that I used:

//Program to control the cartesian robot, this program will draw a square and a triangle using
//the robot's stepper motors

//Definition of the motors and end stops pins
#define xsteps         54
#define xdirection     55
#define endstopx       3
#define xenable        38
#define ysteps         60
#define ydirection     61
#define endstopy       14
#define yenable        56
#define zsteps         46
#define zdirection     48
#define endstopz       18
#define zenable        62



int count; //variables to count the steps on x, y and z direction
unsigned long instantMillis; 
unsigned long prevMillis = 0;
unsigned long stepsMillis = 25; // time between steps in milliseconds
void setup() {
  //All the pins for the motors movements are considered outputs, the pins for the endstops
  //are inputs_pullups, used to avoid a float input on the switches
  pinMode (xsteps, OUTPUT);
  pinMode (xdirection, OUTPUT);
  pinMode(xenable, OUTPUT);
  pinMode (ysteps, OUTPUT);
  pinMode (ydirection, OUTPUT);
  pinMode(yenable, OUTPUT);
  pinMode (zsteps, OUTPUT);
  pinMode (zdirection, OUTPUT);
  pinMode(zenable, OUTPUT);

  pinMode (endstopx, INPUT_PULLUP);
  pinMode (endstopy, INPUT_PULLUP);
  pinMode (endstopz, INPUT_PULLUP);

  digitalWrite(xenable, LOW);
  digitalWrite(yenable, LOW);
  digitalWrite(zenable, LOW);

  initialposition();

}

//function to move the stepper motor on x direction
void stepsx (){
  if (instantMillis - prevMillis >= stepsMillis){ //the motor will only take a step when 25ms have elapsed
    prevMillis += stepsMillis;
    digitalWrite (xsteps, HIGH);  
    digitalWrite (xsteps, LOW);
  }
}

//function to move the stepper motor on y direction
void stepsy(){
  if (instantMillis - prevMillis >= stepsMillis){
    prevMillis += stepsMillis;
    digitalWrite (ysteps, HIGH);    
    digitalWrite (ysteps, LOW);
  }
}

//function to move the stepper motor on z direction
void stepsz(){
  if (instantMillis - prevMillis >= stepsMillis){
    prevMillis += stepsMillis;
    digitalWrite (zsteps, HIGH);
    digitalWrite (zsteps, HIGH);
  }
}

//function to move the end effector in diagonal
void stepdiagonal(){
  digitalWrite (xsteps, HIGH);
  digitalWrite (ysteps, HIGH);
  delay(1);
  digitalWrite (xsteps, LOW);
  digitalWrite (ysteps, LOW);
 }


//function to put the end effector on the initial position
void initialposition(){
  int endstopReadx, endstopReady, endstopReadz;

  endstopReadx = digitalRead (endstopx);
  endstopReady = digitalRead (endstopy);
  endstopReadz = digitalRead (endstopz);
  
  if (endstopReadx == LOW){
    digitalRead (endstopx);
    digitalWrite (xdirection, HIGH);
    stepsx();
  }
  if (endstopReady == LOW){
    digitalWrite (ydirection, LOW);
    stepsy();
  }
  if (endstopReadz == LOW){
   
    digitalWrite (zdirection, LOW);
    stepsz();
  }
}

void loop() {
    instantMillis = millis();
    for (count = 0; count<=700; count++){
    digitalWrite( xdirection, LOW);
    stepsx();
  }
}

In Reply #3 I was asking you to post the version of my program that YOU have uploaded to your Arduino and which, you say, did not work for you.

When something does not work start the diagnosis with the simplest possible code.

…R