joy stick control of stepper motor

Hi
This is my first post so go gentle on me.
I am controlling a bipolar stepper motor with analogue joystick with the following code. I have tried to program end stops, so far without success. Can this be done ?
Once I have mastered this simple control, I want to add accelerometers to this for stabilization.
Any help would be most appreciated.

//declare pins
int potPin = 1;
int Step_X = 13;
int Dir_X = 12;
int Enable_X = 8;

//declare values
int Speed_X = 0; //step speed (delay between steps)
int val= 0;
int j = 0;

void setup() {
pinMode(Step_X, OUTPUT);
pinMode(Dir_X, OUTPUT);
pinMode(Enable_X, OUTPUT);

//Serial.begin(9600); // note that serial comm can be used to debug
// but it will slow down the code and slow down the stepper motor alot
//(and be confusing to me)
}

void loop() {

val = analogRead(potPin); // read the value from the sensor

j = val - 517; // 517 is center positions - how far from center?
j = abs(j); //absolute value
Speed_X = 70000/j; //This math inverts the value and scales as needed
//(value found through trial and error)
// The delay between steps will determine the speed of the motor
// So, delay up = speed down

if (val >= 520){
digitalWrite(Enable_X,LOW); // enable
digitalWrite(Dir_X, HIGH); // Set direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);

}
if (val <= 510) {
digitalWrite(Enable_X,LOW);// enable
digitalWrite(Dir_X, LOW); // Other direction
digitalWrite(Step_X,HIGH);
delayMicroseconds(2);
digitalWrite(Step_X,LOW);
delayMicroseconds(Speed_X);
}

if (val <=520 && val >= 510) {
digitalWrite(Enable_X,HIGH); // disable the stepper motor if the joystic is in the center
}
}

I have tried to program end stops, so far without success. Can this be done ?

Yes, it can be done.

Hardware or software?

For hardware endstops you would connect sensors to Arduino inputs and check those inputs before each step to see if you had reached an end stop.

For software endstops you have to keep track of your current position and compare that position to the programmed end stop positions.

Thanks for the reply, I'd prefer to do it via software. Just don't know enough about feedback yet.

In software the only feedback needed is the initial determination of the starting position when powered up - stepper motors (used properly) are open-loop position control, you just keep track of how many net steps.

A single switch is enough to set the home position, if its at the end of travel. You step the motor in that direction until the switch activates and then call that position zero (or whatever). Thereafter all is dead-reckoning.

A single switch is enough to set the home position, if its at the end of travel. You step the motor in that direction until the switch activates and then call that position zero (or whatever).

If the stepper motor has end hard stops, then one can just drive the stepper motor several steps past its full sweep step number and consider the motor to be at the hard stop position.