stopping a stepper motor with a proxy

good day

I have a stepper motor that i want to stop using a proxy sensor. If its at the proxy its wont start the next cycle but when its on run mode it will run pass the proxy.

#include <Wire.h> 
#include <SoftwareSerial.h> 
#include <Keypad.h>


#define txPin 1                    // lcd serial transmission
#define stepPin 44                // defining step pin connection
#define dirPin 46                // stepper motor direction pin connection
const int ProxyOne = 52;         // sensor for 
const int ProxyTwo = 50;
const int ProxyThree = 40;
int MillisBetweenSteps = 400;
int NumberOfSteps = 160;
int NumberOfStep = 800;
int NumberOfStepss = 4000;
int NumberOfStepsss = 3000;
int ProxyState = 0;         
int ProxyStates = 0;
int ProxyStatess = 0;        
int buttonState = 0; 
int lastButtonState = 0;
int ProxyOneCounter = 0; 
int period = 1000;
unsigned long time_now = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;


char hexaKeys[ROWS][COLS] = {
  {'D', '#', '0', '*'},
  {'C', '9', '8', '7'},
  {'B', '6', '5', '4'},
  {'A', '3', '2', '1'}
};

byte rowPins[ROWS] = {11,10,9,8};
byte colPins[COLS] = {7,6,5,4};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
SoftwareSerial LCD = SoftwareSerial(0, txPin);

void setup(){
  
  
  LCD.begin(9600);
  delay(100);
  pinMode(txPin, OUTPUT);
  pinMode(ProxyOne, INPUT);
  pinMode(ProxyTwo, INPUT);
  pinMode(ProxyThree, INPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, LOW);

  
}

 void loop(){
 time_now = millis();
  LCD.print("stepper motor");
  while(millis() < time_now + period){
       
    }

 clearLCD();

 ProxyStates = digitalRead(ProxyTwo);
 ProxyStatess = digitalRead(ProxyThree);
 
 customKey = customKeypad.getKey();
 
  if (customKey == 'A' && ProxyStates == LOW){
     LCD.print("moving down");
     delay(1000);
     clearLCD();
     digitalWrite(dirPin,HIGH); 
      for(int x = 0; x < NumberOfStep; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(MillisBetweenSteps);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(MillisBetweenSteps);
      } 


  
 }

else if (customKey == 'B'&& ProxyStatess == LOW){

         LCD.print("Moving UP");
         delay(1000);
        clearLCD();
       digitalWrite(dirPin,LOW); 
    
      for(int x = 0; x < NumberOfStep; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(MillisBetweenSteps);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(MillisBetweenSteps);
        
      }
     }


     else if (customKey == 'C' && ProxyStates == LOW){
       
      digitalWrite(dirPin,HIGH);
      for(int x = 0; x <NumberOfStepss; x++) {

        digitalWrite(stepPin,HIGH);
        delayMicroseconds(MillisBetweenSteps);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(MillisBetweenSteps);
      }
      

     }
    
  else if (customKey == 'D' && ProxyStatess == LOW){
       
      digitalWrite(dirPin,LOW);
      for(int x = 0; x <NumberOfStepsss; x++) {

        digitalWrite(stepPin,HIGH);
        delayMicroseconds(MillisBetweenSteps);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(MillisBetweenSteps);
      }
      

     }

    
else if(customKey == '#'){
  
       
       while( ProxyOneCounter < 5)
       {
        LCD.print("run mode");
       
       clearLCD();
       buttonState = digitalRead(ProxyOne);
       
      
    if (buttonState == HIGH  && buttonState != lastButtonState && ProxyStates == LOW ) {
        ProxyOneCounter++;
       digitalWrite(dirPin,HIGH); 
      for(int x = 0; x < NumberOfSteps; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(MillisBetweenSteps);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(MillisBetweenSteps);
      } 

      LCD.print("number of button pushes:  ");
      LCD.print(ProxyOneCounter);
     delay(1000);
      
    }
    lastButtonState = buttonState;
   
 }
  if (ProxyOneCounter == 5){
     LCD.print("waiting for the next cycle");
     delay(1000);
     ProxyOneCounter = 0;
     clearLCD();
   
  }
   
 
}
   

}
        


   void clearLCD(){
  LCD.write(0xFE);   
  LCD.write(0x01);    
}

It is not clear to me at all what your requirements for operation are based on your description or the code. Obviously the code is not working as you intend so I can't rely on the code.

Can you be more specific as to what you expect for the combination of keypad inputs and proxy sensors?

Also, the naming of these variables is obscure and could lead to coding errors. I would suggest renaming them:

int NumberOfSteps = 160;
int NumberOfStep = 800;
int NumberOfStepss = 4000;
int NumberOfStepsss = 3000;
int ProxyState = 0;
int ProxyStates = 0;
int ProxyStatess = 0;

I don't know what you mean by "proxy sensor" but I suspect you need to modify your program to check the state of the sensor between every step.

...R
Stepper Motor Basics
Simple Stepper Code

I assume proxy sensor is proximity sensor.

What is the purpose of this?

  while (millis() < time_now + period) {

  }

I suspect the delays are messing you up. You need to time everything with millis() and check your buttons every time through loop.

Also, since you didn’t indicate how many steps per revolution your steppers are OR how close to the sensors you get with each revolution I could see how easily it could go past the sensor.

Poor choice of variable names:

int NumberOfStep = 800;
int NumberOfSteps = 160;
int NumberOfStepss = 4000;
int NumberOfStepsss = 3000;
int ProxyState = 0;
int ProxyStates = 0;
int ProxyStatess = 0;

You have many copies of the step loop that all do the same thing except for the number of steps. That loop should be in a function like this:

void move(unsigned steps)
{
  for (unsigned x = 0; x < steps; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(MillisBetweenSteps);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(MillisBetweenSteps);
  }
}

None of the step loops was checking the proximity sensors between steps so they are going to move the full amount regardless of triggering a proximity sensor. If you want a step loop to stop when a proximity sensor is triggered you will have to check the input before every step:

void move(unsigned steps, byte sensorPin)
{
  for (unsigned x = 0; x < steps; x++)
  {
    if (digitalRead(sensorPin) == HIGH)
      return; // Sensor triggered: Don't take another step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(MillisBetweenSteps);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(MillisBetweenSteps);
  }
}

It looks like you have only one axis. You might want to have separate moveUp() and moveDown() functions. Then the direction pin setting can be moved to the function and the function would know what sensor to check:

void moveUp(unsigned steps)
{
  digitalWrite(dirPin, LOW);  // UP
  for (unsigned x = 0; x < steps; x++)
  {
    if (digitalRead(TopProxSensorPin) == HIGH)
      return;  // Reached the top: Don't take another step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(MillisBetweenSteps);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(MillisBetweenSteps);
  }
}

void moveDown(unsigned steps)
{
  digitalWrite(dirPin, HIGH);  // DOWN
  for (unsigned x = 0; x < steps; x++)
  {
    if (digitalRead(BottomProxSensorPin) == HIGH)
      return;  // Reached the bottom: Don't take another step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(MillisBetweenSteps);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(MillisBetweenSteps);
  }
}

This is how I would re-write your sketch. Maybe it will help.

#include <Wire.h>
#include <SoftwareSerial.h>
#include <Keypad.h>


const byte txPin = 1;           // lcd serial transmission
const byte stepPin = 44;        // defining step pin connection
const byte dirPin = 46;         // stepper motor direction pin connection
const byte Button = 52;         // sensor for
const byte BottomProxSensorPin = 50;
const byte TopProxSensorPin = 40;

const unsigned MicroSBetweenSteps = 400;

const int Step160 = 160;
const int Step800 = 800;
const int Step3k = 3000;
const int Step4k = 4000;

int buttonState = 0;
int lastButtonState = 0;
int ButtonCounter = 0;
int period = 1000;
unsigned long time_now = 0;

// Keypad declaration
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] =
{
  {'D', '#', '0', '*'},
  {'C', '9', '8', '7'},
  {'B', '6', '5', '4'},
  {'A', '3', '2', '1'}
};
byte rowPins[ROWS] = {11, 10, 9, 8};
byte colPins[COLS] = {7, 6, 5, 4};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

SoftwareSerial LCD = SoftwareSerial(0, txPin);

void setup()
{
  LCD.begin(9600);
  delay(100);
  pinMode(txPin, OUTPUT);
  pinMode(Button, INPUT);
  pinMode(BottomProxSensorPin, INPUT);
  pinMode(TopProxSensorPin, INPUT);

  digitalWrite(stepPin, LOW);
  pinMode(stepPin, OUTPUT);

  digitalWrite(dirPin, HIGH);
  pinMode(dirPin, OUTPUT);
}

void loop()
{
  LCD.print("stepper motor");
  delay(period);
  clearLCD();

  switch (customKeypad.getKey())
  {
    case 'A':
      LCD.print("Moving Down");
      delay(1000);
      clearLCD();
      moveDown(Step800);
      break;

    case 'B':
      LCD.print("Moving UP");
      delay(1000);
      clearLCD();
      moveUp(Step800);
      break;

    case 'C':
      moveDown(Step4k);
      break;

    case 'D':
      moveUp(Step3k);
      break;

    case '#':
      while (ButtonCounter < 5)
      {
        LCD.print("run mode");
        clearLCD();

        buttonState = digitalRead(Button);
        if (buttonState != lastButtonState)
        {
          // Button just changed state
          lastButtonState = buttonState;

          if (buttonState == HIGH)
          {
            // Button just went HIGH
            ButtonCounter++;

            moveDown(Step160);

            LCD.print("number of button pushes:  ");
            LCD.print(ButtonCounter);

            delay(1000);
          }
        }
      } // end while
      LCD.print("waiting for the next cycle");
      delay(1000);
      ButtonCounter = 0;
      clearLCD();
      break;
  } // end switch
} // end loop()

void moveUp(unsigned steps)
{
  digitalWrite(dirPin, LOW);  // UP
  for (unsigned x = 0; x < steps; x++)
  {
    if (digitalRead(TopProxSensorPin) == HIGH)
      return;  // Reached the top: Don't take another step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(MicroSBetweenSteps);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(MicroSBetweenSteps);
  }
}

void moveDown(unsigned steps)
{
  digitalWrite(dirPin, HIGH);  // DOWN
  for (unsigned x = 0; x < steps; x++)
  {
    if (digitalRead(BottomProxSensorPin) == HIGH)
      return;  // Reached the bottom: Don't take another step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(MicroSBetweenSteps);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(MicroSBetweenSteps);
  }
}

void clearLCD()
{
  LCD.write(0xFE);
  LCD.write(0x01);
}

thanks everyone, its working perfectly now.