Can i set a different home position from 0 at home?

I would like to know if I can define as a home position 300 and not zero
I use accel steper library in my code and if i set as curent position 300 in homming mode it not work corectly
I will also attach the code I use (piece of code)
if anyone knows anything about this please help.
thanks

void home() {
  if(state == 3){
   
   lcd.begin();  // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("START HOMMING!");
  lcd.setCursor(1,1);
  lcd.print("");
  lcd.setCursor(4,2);
  lcd.print("W A I T");
   
   stepper.setMaxSpeed(600.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
digitalWrite(sol, HIGH);

 while (digitalRead(Home_Switch)) {  // Make the Stepper move CCW until the switch is activated 
   digitalWrite(RedLed, HIGH); 
    
     
    stepper.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepper.run();  // Start moving the stepper
    digitalWrite(RedLed, HIGH);
    delay(5);
}

  stepper.setCurrentPosition(0);  // Set the current position as zero for now
  stepper.setMaxSpeed(600.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing=1;

  while (!digitalRead(Home_Switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
     digitalWrite(RedLed, LOW);
     digitalWrite(GreenLed, HIGH);
    
  } 
   delay(10);
   digitalWrite(sol, LOW); 
  stepper.setCurrentPosition(300);  //<==========Home position at end of homing     
  currentposition="300";
  state=0;
// Draw starting screen on  LCD
  lcd.begin();                      // initialize the lcd  
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("HOMMING COMPLETE");
  lcd.setCursor(3,1);
  lcd.print("cur-pos:  ");
   lcd.setCursor(12 , 1);
  lcd.print(currentposition);
   lcd.setCursor(3,2);
  lcd.print("PLEASE WAIT:");
  lcd.clear();

   //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1200);  // Not to fast or you will have missed steps
  stepper.setAcceleration(150);  //  Same here

  
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("ENTER DISTANCE");
  lcd.setCursor(2,1);
  lcd.print( "MM==>");
  lcd.setCursor(0,2);
   lcd.print("------------------");
  lcd.setCursor(0,3);
  lcd.print("cur-pos");
  lcd.setCursor(8,3);
  lcd.print(currentposition);
    
  }
x=1;
 }

Try it

Mark

You can pretend that ANY value means home. I can't, for the life of me, imagine why you would want to do that, but it is your code and your grief.

Thank's for reply

I do not want to pretend. I want to be the real position because it must be true of the 300 position
If we pretend it is 300 when I press on the keypad 300 does not stay the stepper in the Home (300) position but it goes +300
when I type 0 goes a litle back

PROFILCA:
If we pretend it is 300 when I press on the keypad 300 does not stay the stepper in the Home (300) position but it goes +300
when I type 0 goes a litle back

To make sense of that we need to see the complete program.

What's the difference between {A} setting HOME as 300 and getting the motor to move to 0 and {B} setting HOME as 0 and getting the motor to move to -300 ?

...R

Home is just some arbitrary value - usually 0 so that moving away from home by some amount results in an easy to compute new position.

If you want to call 300 home, do that. But, then correct the calculation of the new absolute position that +300 represents, or -200.

All that you say is right and reasonable but I have a difficulty due to the construction of the project.
I want to make a meter for an aluminum saw and the moving part can not get close to the disk at point 0.
The request is to have a lower position of 300 and begin to count from 300
To ensure do not move under 300 I have the code(part of code)

void movestepper(int z) {
// Move the stepper
if(keyfullnumber > 3950 || keyfullnumber <300){ //set the maximum and minimum distance
stepper.stop();
}

But the problem is that after the zeroing (at 300 μm) on the first movement does not remove these 300 from the requested length.
If I enter a distance of 400 or something else goes to 700 (400 + 300) while it should be (300 + 100) = 400
I do not know if it is clear what exactly is the problem I have with this construction.
I made some modification to the code can you tell me if it is right?
(stepper.runToNewPosition(300-300):wink:

Thank you all for your help

void home() {
  if(state == 3){
   
   lcd.begin();  // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("START HOMMING!");
  lcd.setCursor(1,1);
  lcd.print("");
  lcd.setCursor(4,2);
  lcd.print("W A I T");
   
   stepper.setMaxSpeed(600.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(100.0);  // Set Acceleration of Stepper
digitalWrite(sol, HIGH);

 while (digitalRead(Home_Switch)) {  // Make the Stepper move CCW until the switch is activated 
   digitalWrite(RedLed, HIGH); 
    
     
    stepper.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepper.run();  // Start moving the stepper
    digitalWrite(RedLed, HIGH);
    delay(5);
}

  stepper.setCurrentPosition(0);  // Set the current position as zero for now
  stepper.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(50.0);  // Set Acceleration of Stepper
  initial_homing=1;

  while (!digitalRead(Home_Switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
     digitalWrite(RedLed, LOW);
     digitalWrite(GreenLed, HIGH);
    
  } 
   delay(10);
   digitalWrite(sol, LOW); 
  stepper.setCurrentPosition(0);
  currentposition="300";
  state=0;
// Draw starting screen on  LCD
  lcd.begin();                      // initialize the lcd  
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("HOMMING COMPLETE");
  lcd.setCursor(3,1);
  lcd.print("cur-pos:  ");
   lcd.setCursor(12 , 1);
  lcd.print(currentposition);
   lcd.setCursor(3,2);
  lcd.print("PLEASE WAIT:");
  lcd.clear();

   //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(600);  // Not to fast or you will have missed steps
  stepper.setAcceleration(100);  //  Same here

  
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("ENTER DISTANCE");
  lcd.setCursor(2,1);
  lcd.print( "MM==>");
  lcd.setCursor(0,2);
   lcd.print("------------------");
  lcd.setCursor(0,3);
  lcd.print("cur-pos");
  lcd.setCursor(8,3);
  lcd.print(currentposition);
    
  }
  state=1;                                
 if (state==1){
   stepper.runToNewPosition(300-300);   //<====== is this right?
  currentposition = ("300");
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("ENTER DISTANCE");
  lcd.setCursor(2,1);
  lcd.print( "MM");
  lcd.setCursor(0,2);
  lcd.print("cur-pos");
  lcd.setCursor(8,2);
  lcd.print(currentposition);
  lcd.clear();       
  }
  x=1;
 }

Is this a correct description of the problem ...

  • There is a limit switch to define the HOME position
  • But the lowest acceptable working position is 300 steps above that position.

If that is correct then I think you should set your HOME position at 0 (zero), then move the motor 300 steps and after that make sure that your step count never falls below 300.

...R

Yes that is the problem

That’s what I tried at the end of the home process and worked on
I do not know if this is the right way, but it works and does what I want
stops at 300 and gets a value of 300 and not 0

 while (!digitalRead(Home_Switch)) { // Make the Stepper move CW until the switch is deactivated
   stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
     digitalWrite(RedLed, LOW);
     digitalWrite(GreenLed, HIGH);
   
  } 
   delay(10);
   digitalWrite(sol, LOW); 
 stepper.setCurrentPosition(600);  <=====from 0 to 600
 currentposition="300";

However, thank you all for your help was valuable.
If you have any observation I am happy to see
Excuse me if I’m mistaken in writing but translating through google translation

It seems to me you are trying to change the NUMBER associated with the HOME position. As I understand the problem then that is the wrong thing to do.

Do the homing and leave the number of the home position at 0. And subsequently make sure that none of your moves goes to a position below 300.

I can't help feeling that you are over-thinking the problem.

If I'm not making sense then draw a simple diagram to illustrate the problem. See this Simple Image Upload Guide

...R

Okay, I’ll try to do what you say by modifying some mechanical parts if I can make it.
However, this false way to the code worked but I do not know if it will be stable.
In any case thank you very much all your help is always precious to me.
Thanks to all.

It is perfectly reasonable to mount a home switch at physical position 300. And you should be able to keep referring to that position as 300 in all of your high-level code.

Be sure though, to always perform the homing function from one side of home. The “width” of the switch is probably significant and you should always try to pick up the same “edge” of the switch.

  stepper.setCurrentPosition(300);  //<==========Home position at end of homing     

currentposition=“300”;

Good, but why is currentposition a string? Strings are for letters and words, not numbers.

Use the stepper.currentPosition() to find out where the stepper is.

string is for lcd

   stepper.setCurrentPosition(600);
 currentposition="300";
  state=0;
// Draw starting screen on  LCD
  lcd.begin();                      // initialize the lcd  
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("HOMMING COMPLETE");
  lcd.setCursor(3,1);
  lcd.print("cur-pos:  ");
   lcd.setCursor(12 , 1);
  lcd.print(currentposition);   //<===============
   lcd.setCursor(3,2);
  lcd.print("PLEASE WAIT:");
  lcd.clear();

MorganS:
It is perfectly reasonable to mount a home switch at physical position 300. And you should be able to keep referring to that position as 300 in all of your high-level code.

If you can mount the switch at the right place I don't see the point of calling it 300 rather than zero :slight_smile:

I have been assuming that the OP's problem is that he cannot mount the switch at the ideal position. But I may be wrong.

...R

That’s right.
The moving part can not go closer than 300 to the 0 point.
I place limit switcjh for home to 300 cm away from zero point and i want after zeroin the machine to start counting from 300cm and plus.
And then make sure that none of the moves go to a position below 300.(the working area is from 300 to 3950)

void movestepper(int z) { 
  //  Move the stepper
 if(keyfullnumber > 3950 || keyfullnumber <300){    //set the maximum and minimum distance 
  stepper.stop();

to approach point 0, I need to convert the machine, which is quite difficult.
That’s what I’m trying to get through the code

So we have established why the 300. Is there still a problem?

In this way he works but I have not checked if he is stable.
I will see how he works.