Automating Z-axis with Stepper Motor

Hi Guys

I need some help with Writing code to automate the Z-axis on an older machine we have at work.
The machine in question is a JP-S24 (workshop key-way cutting machine).
The machine works with the whole z-axis moving parallel to the x-axis thus making "passes" through/over the material and the z-axis is moved down in small increments until the key-way dept is reached.

I have the basic code sorted (still a noob to coding). I can control the stepper motor with a "up" button and a "down" button for the different rotations.
I have a magnetic pick-up counting the passes the machine makes (z-axis needs to move down one increment every 5 passes until dept is reached)
But am a little lost when it gets to telling the machine exactly what to do.

The process I want the machine / operator to go through should be as follows:
1.) Operator needs to move the axis down/up to the point where it touches the material using the up and down buttons.
2.) The axis needs to be zero'd at this point.
3.) The operator then needs to tell the control how deep the key-way should be (total z-axis travel)
4.) Machine is started and arduino starts counting passes and moves down one increment every X amount of passes.
5.) once set dept is reached the machine should stop and this position should become the new zero position.
6.) Operator then moves the z-axis up, adjusts the Y-axis, touches the material with z-axis, pushes start and then machine moves down to the new zero point in increments every X amount of passes.

I have very basic programming skills, so far I can control the Z-axis UP and DOWN.
I have a counter that counts the passes and every X amount of passes I have a relay activating (want to use the relay as a signal for the stepper motor to move 1 increment and stop and wait for next signal and so on until dept is reached.

I am having trouble with the HMI part and moving down in increments until it reaches the set dept.

Can anyone here point me in the right direction or share some of the code that can help me?

You seem to have a clear description of the steps required - although I don't understand the process described by step 6.

You have not said what "hardware" the operator will interact with. For example is the control system a program running on a PC? or is it intended to be completely on an Arduino?

What have you in mind for the sort of input from and feedback to the operator?

It is hard to know how to give more specific advice. You seem to have some of the programming figured out but I don't really understand in detail where you are stuck. Maybe have a look at Planning and Implementing a Program

If you have a go at writing the code and post that code it may be easier to help.

...R

Thanks for the Reply.

Basically I want the program to run purely off the arduino, I have built a housing for the arduino, LCD display, and enough space for as many buttons as I would need.

For step 6, The key-way being cut calls for the machine to be adjusted on the x-axis (this I have fitted a DRO so operator can adjust accordingly), for this adjustment to happen the z-axis needs to be moved up (with the UP button obviously) so that it clears the material. X is adjusted and then I need the machine to follow the same increment movements so until it reaches the same position (bottom/dept of the key-way aka new zero)

LikeI said I have very limited coding experience so the code I came up with is a bit rough, see code posted below:

 // THIS IS THE DEVOLOPING CODE FOR THE CNC PROJECT

#include <Stepper.h>                                               //Includes the stepper library
#include <LiquidCrystal.h>                                         //Includes the LCD library

//This is a button representing the magnetic pickup that would be used to minitor the movement of the machine 
int  passesButton = ;                                              //The pin that the pushbutton is attached to (will be assigned to a DI on MEGA R3)

//passesButton counting parameters needed for the program to keep track of the current and next state of the button
int buttonPushCounter = 0;                                         //Counter for the number of button presses
int buttonState = 0;                                               //Current state of the button
int lastButtonState = 0;                                           //Previous state of the button

//UP and DOWN buttons to move the Z-axis and get the machine to zero
int upButton = 2;                                                //Sets the up buton to pin 2 on arduino
int RELAY = 3;                                                                                                       //CHANGED downButton to RELAY              //Sets the down buton to pin 2 on arduino
int UP = 0;                                                        //Up Variable
int DOWN = 0;                                                      //Down Variable

//Motor Pins
int motorPin1 = 10;                                                //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
int motorPin2 = 11;                                                //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
int motorPin3 = 12;                                                //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
int motorPin4 = 13;                                                //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil

//Motor Parameters
const int stepsPerRevolution =64;                                  //Here set the stepper motor rotation step how much is a circle
int dim=stepsPerRevolution;                                        //Set the step motor number and pin
int motorSpeed;                                                    //Sets the int motorSpeed   
//int motor_Step;                                                  //Sets the int motorStep

//LCD Parameters and Pin allocation
LiquidCrystal LCD(9,8,7,6,5,4);                                    //Sets the LCD pins up



void setup() {
  Serial.begin(9600);                                              //Begin serial for debugging process  

  pinMode(passesButton, INPUT);                                    //Initialize the button pin as a input:

  LCD.begin(16,2);                                                 //Begins the LCD display and tells the LCD how big it is
  LCD.setCursor(0,0);                                              //Setsthe LCD Cursor
  LCD.print("Set Speed: ");                                        //Prints the quoted text
  LCD.setCursor(0,1);                                              //Setsthe LCD Cursor
  LCD.print("Passes: ");                                           //Prints the quoted text

    
  //pinMode(upButton, INPUT);                                      //Sets up button as an input
  pinMode(RELAY, OUTPUT);                                          //Sets down button as an input
  pinMode(motorPin1, OUTPUT);                                      //Sets motorPin 1 to be an output
  pinMode(motorPin2, OUTPUT);                                      //Sets motorPin 2 to be an output     
  pinMode(motorPin3, OUTPUT);                                      //Sets motorPin 3 to be an output
  pinMode(motorPin4, OUTPUT);                                      //Sets motorPin 4 to be an output
  digitalWrite(RELAY,LOW);
}



void loop() {

//passes Button counter
 buttonState = digitalRead(passesButton);                          //Read the pushbutton input pin
   if (buttonState != lastButtonState) {                           //Compare the buttonState to its previous state
    if (buttonState == HIGH) {                                     //If the state has changed, increment the counter
      buttonPushCounter++;                                         //If the current state is HIGH then the button went from off to on
    }
     delay(50);                                                    //Delay a little bit to avoid bouncing
   }
    lastButtonState = buttonState;                                 //Save the current state as the last state, for next time through the loop

//RELAY to act as switch in order to tell Stepper when to move down 1 increment
if (buttonPushCounter % 6 == 3) {                                  //Turns on the LED every four button pushes by checking the modulo of the button push counter.
  digitalWrite(RELAY, HIGH);                                 
  delay (50); 
  } 
  else {                                                         //The modulo function gives you the remainder of the division of two numbers:
  digitalWrite(RELAY, LOW);                                   
  }
   
  int sensor_Reading = analogRead(A0);                             //Read the sensor value
  int motorSpeed = map(sensor_Reading, 0, 1023, 0, 64);            //Map it to a range according to steps per revolution
  LCD.setCursor(11,0);                                             //Just a calculation test need to modify for distance traveled per revolution and keep track of the revolutions etc.
  LCD.print(float(float(motorSpeed)/float(64)));                   //Values are mixed up, slow is fast and fast is slow
     
 UP = digitalRead(upButton);
 if (UP == HIGH)
  {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motoPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(motorSpeed);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(motorSpeed);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, HIGH);
  delay(motorSpeed);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(motorSpeed);
  } 
   
 DOWN = digitalRead(downButton);
 if (DOWN == HIGH)

  {
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, LOW);
  delay(motorSpeed);
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, LOW);
  delay(motorSpeed);
  digitalWrite(motorPin4, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, HIGH);
  delay(motorSpeed);
  digitalWrite(motorPin4, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin1, HIGH);
  delay(motorSpeed);
  }

   LCD.setCursor(8, 1);
   LCD.print(buttonPushCounter);

}

Currently the "passes" push button works as I need it to (Relay turns on when I want it to, will use this as a signal to tell stepper motor when to move 1 increment)
The up and downs buttons work as I need them to.

The problem comes in with the following:
I need to write a loop that tells the motor that every time the relay sends a signal (same as a button) the stepper motor has to turn X amount of steps, and then stop when it reaches a total of Y amount of steps no matter the condition of the relay signal.(this would be the dept of the key-way being cut)
And all of this has to only start once the "zero" button is pressed.

Hi Guys,

Robin 2, I had a quick read through the link you attached, those tips/procedure is awesome advice, I have tried to alter my code accordingly but still needs some work.

Code and steps below:

/*

 THIS IS THE DEVOLOPING CODE FOR THE CNC/AUTOMATION PROJECT
 THIS IS FOR THE KEY-WAY CUTTING MACHINE Z-AXIS.
 STEPS TO FOLLOW SHOULD BE AS FOLLOWS:
 1.) OPPERATOR TO USE UP AND DOWN BUTTON TO GET CUTTING EDGE OF BIT TO TOUCH THE MATERIAL
 2.) AT THIS POINT THE MACHINE Z-AXIS POSITION NEEDS TO BE SET TO BE EQUAL TO ZERO
 3.) LCD HMI PROMT THE OPERATOR TO SELECT A DEPT / TRAVEL FOR THE Z-AXIS
 4.) MACHINE IS STARTED AND PASSES COUNTER STARTS PICKING UP MOVEMENT OF MACHINE (MAGNETIC PICK UP)
 5.) AFTER A FIXED SET AMOUNT OF PASSES THE Z-AXIS MOVES DOWN A SMALL INCREMENT
 6.) THIS PROCESS CONTINUES UNTIL THE SET DEPT OF THE KEY-WAY IS REACHED (NEED TO FIND A WAY OF SHUTTING OFF THE MACHINE)
 7.) THE Z-AXIS THEN RETURNS TO ITS ZERO POINT.
 8.) OPERATOR ADJUSTS THE MACHINE OCCORDING TO THE WIDTH OF THE KEY-WAY (Z-AXIS GOES TO -VE VALUES)
 9.) OPERATOR STARTS THE MACHINE AGAIN AND THE Z-AXIS MOVES DOWN TO THE SAME SET VALUE AS BEFORE IN THE SAME METHOD USED.
10.) SOMETIMES MACHINE GETS ADJUSTED 3 TIMES SO THE PROGRAM NEEDS TO INCLUDE ANY ADJUSTMENTS.

*/

#include <Stepper.h>
#include <LiquidCrystal.h>


//Digital IO's:
int SlowUpButton = 22;
int SlowDownButton = 23;
int MediumUpButton = 2;
int MediumDownButton = 3; 
int FastUpButton = 24;
int FastDownButton = 25; 
int passesButton = 4;
int zeroButton = 5; 
int RELAY = 6; 
int RELAYinputSignal = 7;   

int motorDIRECTION = 10; 
int motorPULSE = 11; 


//passesButton counting parameters needed for the program to keep track of the current and next state of the button
int buttonPushCounter = 0;  
int buttonState = 0; 
int lastButtonState = 0; 

//passesButton counting parameters needed for the program to keep track of the current and next state of the button
int buttonStateZ = 0;
int lastButtonStateZ = 0; 

//Counting the motor steps
int waveStepCount = 4;
int currentStep = 0;
int stepCount = waveStepCount;
int currentStepInSequence = currentStep % stepCount;

//UP and DOWN variables
int UP = 0;
int DOWN = 0; 

//Motor Parameters
const int stepsPerRevolution = 200; 
//int dim = stepsPerRevolution; 
int motorSpeedFast = 120; 
int motorSpeedMedium = 1200; 
int motorSpeedSlow = 2200;
//int motorStep; 
Stepper myStepper(stepsPerRevolution, 10,12,11,13);
const int incrementalSteps = 300;

void setup() {

 Serial.begin(9600);
 
 pinMode(SlowUpButton, INPUT); 
 pinMode(SlowDownButton, INPUT);
 pinMode(MediumUpButton, INPUT); 
 pinMode(MediumDownButton, INPUT); 
 pinMode(FastUpButton, INPUT);
 pinMode(FastDownButton, INPUT);
 pinMode(passesButton, INPUT);
 pinMode(zeroButton,INPUT);
 pinMode(RELAY, OUTPUT);
 
 pinMode(RELAYinputSignal,INPUT); 

 pinMode(motorDIRECTION, OUTPUT); 
 pinMode(motorPULSE, OUTPUT);
 digitalWrite(motorDIRECTION, LOW);
 digitalWrite(motorPULSE, LOW);
 
 myStepper.setSpeed(500);

 
}


void loop() {
 FastAxisUpMovement ();
 FastAxisDownMovement ();
 MediumAxisUpMovement ();
 MediumAxisDownMovement ();
 SlowAxisUpMovement ();
 SlowAxisDownMovement ();
 passesCounter();
 passesActivateRelay();
// zAxisPositionDisplay();
// serialPassesDisplay();
 zeroPosition();
//  incrementalMovementPerXPasses();
 
}


void passesCounter() {
 //passes Button counter
 buttonState = digitalRead(passesButton);
 if (buttonState != lastButtonState) {
   if (buttonState == HIGH) {
     buttonPushCounter++; 
   }

   delay(30);     
 }
 lastButtonState = buttonState;     
}


void passesActivateRelay() {

 if (buttonPushCounter % 6 == 3) {    
   digitalWrite(RELAY, HIGH);
 }
 else {     //The modulo function gives you the remainder of the division of two numbers:
   digitalWrite(RELAY, LOW);
 }
}

void SlowAxisUpMovement () {
// int sensorReading = analogRead(A0);
// int motorSpeed = map(sensorReading, 0, 1023, 200, 1);
 UP = digitalRead(SlowUpButton);     
 if (UP == HIGH)
 {
   digitalWrite(motorDIRECTION, HIGH);
   digitalWrite(motorPULSE, HIGH);
   delayMicroseconds(motorSpeedSlow);
   digitalWrite(motorPULSE, LOW);
   delayMicroseconds(motorSpeedSlow);
   ++currentStep;
 }
}


void SlowAxisDownMovement () {
//  int sensorReading = analogRead(A0);     
//  int motorSpeed = map(sensorReading, 0, 1023, 200, 1);    

 DOWN = digitalRead(SlowDownButton);    
 if (DOWN == HIGH)
 {
   digitalWrite(motorDIRECTION, LOW);
   digitalWrite(motorPULSE, HIGH);
   delayMicroseconds(motorSpeedSlow);
   digitalWrite(motorPULSE, LOW);
   delayMicroseconds(motorSpeedSlow);
   --currentStep;
 }
}
void MediumAxisUpMovement () {
// int sensorReading = analogRead(A0);     
// int motorSpeed = map(sensorReading, 0, 1023, 200, 1);   

 UP = digitalRead(MediumUpButton);     
 if (UP == HIGH)
 {
   digitalWrite(motorDIRECTION, HIGH);
   digitalWrite(motorPULSE, HIGH);
   delayMicroseconds(motorSpeedMedium);
   digitalWrite(motorPULSE, LOW);
   delayMicroseconds(motorSpeedMedium);
   ++currentStep;
 }
}


void MediumAxisDownMovement () {
//  int sensorReading = analogRead(A0);     
//  int motorSpeed = map(sensorReading, 0, 1023, 200, 1);    

 DOWN = digitalRead(MediumDownButton);     
 if (DOWN == HIGH)
 {
   digitalWrite(motorDIRECTION, LOW);
   digitalWrite(motorPULSE, HIGH);
   delayMicroseconds(motorSpeedMedium);
   digitalWrite(motorPULSE, LOW);
   delayMicroseconds(motorSpeedMedium);
   --currentStep;
 }
}
void FastAxisUpMovement () {
// int sensorReading = analogRead(A0);   
// int motorSpeed = map(sensorReading, 0, 1023, 200, 1);     

 UP = digitalRead(FastUpButton);   
 if (UP == HIGH)
 {
   digitalWrite(motorDIRECTION, HIGH);
   digitalWrite(motorPULSE, HIGH);
   delayMicroseconds(motorSpeedFast);
   digitalWrite(motorPULSE, LOW);
   delayMicroseconds(motorSpeedFast);
   ++currentStep;
 }
}


void FastAxisDownMovement () {
//  int sensorReading = analogRead(A0);     
//  int motorSpeed = map(sensorReading, 0, 1023, 200, 1);     

 DOWN = digitalRead(FastDownButton);     
 if (DOWN == HIGH)
 {
   digitalWrite(motorDIRECTION, LOW);
   digitalWrite(motorPULSE, HIGH);
   delayMicroseconds(motorSpeedFast);
   digitalWrite(motorPULSE, LOW);
   delayMicroseconds(motorSpeedFast);
   --currentStep;
 }
}


void serialPassesDisplay() {
 Serial.print("Number of Passes: ");
 Serial.println(buttonPushCounter);
}

void zAxisPositionDisplay(){
 Serial.print("Z-Axis Position: ");
 Serial.print(float(float(currentStep)/float(512)));        
 Serial.print(" mm ");
}

void zeroPosition() {
buttonStateZ = digitalRead(zeroButton);     
 if (buttonStateZ != lastButtonStateZ) {    
   if (buttonStateZ == HIGH) {     
     currentStep = 0;    
   }

   delay(30);   
 }
 lastButtonStateZ = buttonStateZ;    
}


void incrementalMovementPerXPasses() {    
 



void returnToDeptAfterReadjust() {
}

I have decided to use 6 buttons to control the speed the axis moves up and down just to make it easier for the operator to understand.

I am still struggling with the following though:
When I press more than one up/down button the servo reacts strange, what would the best way be of stopping this from happening.
And the biggest worry of the project, I want the servo to turn X-amount of steps everytime the counter reaches the set counts (I have a relay kicking in exactly when I want it to) At the moment the motor turns the required steps but keeps on turning as long as the counter is on the set count. I need the motor/relay to energise and go off (only once).
Then I need the motor to follow the set steps until it reaches Y-amount of steps and not go any further, basically ignoring the counter.

Can you guys help with the code ?

You seem to have a huge program in Replies #2 and #3 - but you have used Quotes rather than [code] [/code] tags which would make it easy to copy to a text editor. Your code is too long for me to study quickly without copying to a text editor.

If the program is too long to include in a single Post then please add the .ino file as an attachment.

...R

Robin2:
You seem to have a huge program in Replies #2 and #3 - but you have used Quotes rather than [code] [/code] tags which would make it easy to copy to a text editor. Your code is too long for me to study quickly without copying to a text editor.

If the program is too long to include in a single Post then please add the .ino file as an attachment.

...R

Hi Robin 2.

Thanks for reply I have edited the posts accordingly, your help will be much appreciated

JacquesNel:
When I press more than one up/down button the servo reacts strange, what would the best way be of stopping this from happening.

I presume you mean "stepper"

You need to explain "strange".

If this was my project I would have a readButtons() function that reads and saves the position of all the buttons. Then the various functions could use the saved values.

And the biggest worry of the project, I want the servo to turn X-amount of steps everytime the counter reaches the set counts (I have a relay kicking in exactly when I want it to) At the moment the motor turns the required steps but keeps on turning as long as the counter is on the set count. I need the motor/relay to energise and go off (only once).
Then I need the motor to follow the set steps until it reaches Y-amount of steps and not go any further, basically ignoring the counter.

I can't make sense of that. And I don't see any code where a motor does something depending on a counter.

It makes things much easier if you mention the functions in which the behaviour you refer to takes place (or should take place).

...R

The way I understand your description, you want to use the manual movement buttons to bring the cutting head to the workpiece, note that position and set a keyway depth and then make three cutting passes at a time before lowering the cutting head. This to repeat until you have cut to the appropriate depth.

Then the cutter must move up so you can move the workpiece so another identical cutting sequence can be made to widen the keyway.

I'm puzzled as to why all your up & down functions control the stepper directly - you have declared myStepper but don't use it.

Is the program intended to control X-axis movement of the cutting head?

What does the relay do? It sounds as though it's used as an indication that it's time to lower the cutter. Why does this need physical hardware?

A picture of the machine might help us understand it better.

wildbill:
The way I understand your description, you want to use the manual movement buttons to bring the cutting head to the workpiece, note that position and set a keyway depth and then make three cutting passes at a time before lowering the cutting head. This to repeat until you have cut to the appropriate depth.

Then the cutter must move up so you can move the workpiece so another identical cutting sequence can be made to widen the keyway.

I'm puzzled as to why all your up & down functions control the stepper directly - you have declared myStepper but don't use it.

Is the program intended to control X-axis movement of the cutting head?

What does the relay do? It sounds as though it's used as an indication that it's time to lower the cutter. Why does this need physical hardware?

A picture of the machine might help us understand it better.

You are correct in the operating procedure of the machine.

The myStepper was part of the testing, Im very new to coding kinda forgot it in the code (my bad).

I am using he relay purely as an indication to physically see the operation/process of the code.
In the code you will see a passesButton that I am using purely for testing, this will be replaced with a magnetic pick up that picks up the movement of the machine.

I will quickly take some pictures of the machine and upload it to this topic maybe even post a video of the machine on youtube and leave the link here so you guys can have a better understanding of how the machine works.

I will do all the above mentioned once I get home tonight.

Okay I have quickly posted two videos I have of the machine that I took a while ago when I started the project

Here are the links to the Videos:

You will see in the Video the moving part of the machine has an adjusting handle on the top of the "Z-axis", this gets adjusted manually every 3 passes the machine makes, which allows for human error and imo is a waste of the operators time.

in the code I have a passes button that activates a relay every x-passes, (relay used for testing purposes so that I can see the magic happen.

one turn on the stepper motor = 1mm in my case. I need the stepper motor to move/turn the equivalent of 0,2mm for every 3 passes the machine makes (reason for passes is the speed can be adjusted thus it cant be a time delay)

up to dept/total movement of 7mm then the axis must stay there no matter the counting of the passes? Does this make sense?

Robin2:
I presume you mean "stepper"

You need to explain "strange".

If this was my project I would have a readButtons() function that reads and saves the position of all the buttons. Then the various functions could use the saved values.

I can't make sense of that. And I don't see any code where a motor does something depending on a counter.

It makes things much easier if you mention the functions in which the behaviour you refer to takes place (or should take place).

...R

Apologies I meant Stepper motor

Strange behaviour in this case is when I press the up and down button the stepper motor stops, Is there a way of coding it so that when I press a button the arduino should ignore the other buttons?

I am very new to coding not to sure about the readButtons() function, how would you code the arduino to acchieve this is there a code I can look up or research?

The counter activates the relay, (purely for testing purposes) I need the relay to activate the motor and move x-amount of steps untill it reaches the set amount of steps then the stepper must stop at that point no matter the relay and/or passes counter?

JacquesNel:
I am very new to coding not to sure about the readButtons() function, how would you code the arduino to acchieve this is there a code I can look up or research?

There is nothing complicated about this. At the moment you have the digitalRead()s for the various buttons scattered through your program. Just move them all into one function.

And that will probably facilitate dealing with your other issue

when I press a button the arduino should ignore the other buttons?

because it will be easy to write your code to give priority to any button you like when you have all the button states together.

I think the movement after every N counts can be achieved with a variable that keeps track of the state of the system - perhaps call it moveDown and when the counter reaches 3 (or whatever) set it to true. Then have a function that checks that variable and resets it when finished - something like

void moveDownwards() {
   if (moveDown == true) {
     // code to make the move happen
     moveDown = false;
   }
}

Apologies if my variable and function names are inappropriate, but I hope they will give the idea.

...R

I have tried the code for this project but still no luck in getting the axis to move a certain distance after X-passes. And the biggest concern is stopping the program/sequence after the axis has reached the desired dept.

Any advice guys?

Please see the links to the YouTube Videos I posted to see what type of machine I am dealing with for a clear understanding of what I want to achieve.

JacquesNel:
I have tried the code for this project but still no luck in getting the axis to move a certain distance after X-passes.

You need to post the latest version of your code.

...R

I am still trying to neaten up the code so that others can understand the code.. will try and post it by tomorrow but there is very little to no change from the last code I posted

This sounds like a pretty simple state machine. The best tutorial I know online for state machines is State Machine from one of the forum members here.

It seems like you have the "inner" state machine working - it counts passes and steps the motor down. Then you need to add an "outer" one which reads the buttons and keeps track of the total depth. I always like to draw the states on paper because describing them in words takes too long. You will end up with a lot more states than you think.

Draw each state as a bubble and give it a name. Within each state, it will be doing some specific thing with the outputs. (All-outputs-off is a specific thing.) Then draw arrows out of each state with the reason for why it left that state. "Emergency stop button pushed" is going to be one arrow going out of almost every single state. "User spent more than 50 seconds entering number" might be a reason for leaving the "enter number" state and going back to the "waiting to start" state.

Many states will have the same outputs but they are different because you entered that state from a different direction. "Emergency stop" and "Waiting for start" might do the same thing except you need the transition "emergency stop switch reset" to go from stop to wait.