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