Homing position sketch

Hi,

I having an issue with the sketch below, it works fine the first time but won't work again afterwards (unless you press reset). Basically what should happen is that when the switch D10 is pressed the sketch should rewind the motor until it hits a limit switch (D7) and then move it forward to its home position. But unfortunately after doing it the first time it justs sits there.

I have looked at D10 on the serial monitor and it is constantly reading 1's until D10 is pressed and then you get a single zero and no further zero's no matter how many times D10 is pressed.

Any help would be appreciated

Steve

#include <AccelStepper.h>

AccelStepper stepper1(1, 5, 4);
const int ActivationButton = 10;
const int limitButton = 7;
byte LBval;
byte buttonState;


void setup() {
  Serial.begin(9600);
  pinMode(ActivationButton, INPUT_PULLUP);
  pinMode(limitButton, INPUT_PULLUP);
}

void loop () {

buttonState = digitalRead(ActivationButton);
  Serial.println(buttonState);
if (buttonState == LOW)
  {
    stepper1.setMaxSpeed(800);
    stepper1.setAcceleration(2500);
    
    do
    {
      stepper1.setSpeed(800);
      stepper1.runSpeed();
      LBval = digitalRead(limitButton);
    } while (LBval == HIGH);
    do
    {
      stepper1.moveTo(-1);
      stepper1.run();
    } while (LBval == LOW);

  }
}
      stepper1.setSpeed(800);
      stepper1.runSpeed();
      LBval = digitalRead(limitButton);

Slamming into the limit switch that fast does not seem like a good idea. There is no reason to set the speed over and over.

    do
    {
      stepper1.moveTo(-1);
      stepper1.run();
    } while (LBval == LOW);

In this block, you don't seem to think that it is important to read the state of the pin. Why not?

[EDIT - same answer as Paul who was faster posting :slight_smile: ]

would help to update LBval in the second do / while loop

    LBval = digitalRead(limitButton);

Thanks will give it a try,

PaulS

The motor speed will be adjusted later once the sketch is complete. The reason for not reading the pin in the block below is that once the motor has reversed back to the limit switches it then moves forward away from them. I will add some more code later to establish this as position Zero.

    do
    {
      stepper1.moveTo(-1);
      stepper1.run();
    } while (LBval == LOW);

J-M-L

Just added the line as you suggested and it worked :slight_smile:

Thanks

J-M-L:
[EDIT - same answer as Paul who was faster posting :slight_smile: ]

would help to update LBval in the second do / while loop

    LBval = digitalRead(limitButton);

Thank you all three contributors for this post.
I am "borrowing" some of this code to help me add a homing sequence to my stepper driver setup, using AccelStepper.
I too found that I needed to add: LBval = digitalRead(limitButton); to the second do/while loop.

However, having done so, the motor no longer drives back off of the button.
It simply hits the switch at D7 and stops..
My code now reads:

void loop()

{ 
  Switch = digitalRead(SwitchPin);
  buttonState = digitalRead(ActivationButton);
  if (buttonState == LOW)
  {
    stepper.setMaxSpeed(1000);
    stepper.setAcceleration(500);
    
    do
    {
      stepper.setSpeed(200);
      stepper.runSpeed();
      LBval = digitalRead(limitButton);
    } while (LBval == HIGH);
    do
    {
      stepper.moveTo(-1);
      stepper.run();
      LBval = digitalRead(limitButton);
    } while (LBval == LOW);

  }
}

I am hoping to drive the motor back off of the switch until the switch is released.
Thank you in advance.

I am hoping to drive the motor back off of the switch until the switch is released.

So, you know that you have hit the limit switch. You know that you have been stepping one way to do that. You know that you then need to step the other way some fixed number of steps. What, exactly, is the problem?

You do NOT need a do/while loop to make the stepper step some fixed number of times in the other direction.

Your variable names leave a lot to be desired. You didn't just attach a switch to the Arduino for the hell of it. The switch is there for a reason. The name of the variable that holds the number of the pin that the switch is attached to should reflect that reason. The name of the variable that holds the state of the pin should reflect the reason.

You apparently want to press one switch to home the motor, which presses another one when it is home. I say apparently, because I would want to home the motor when the go-home switch BECOMES pressed, not when it IS pressed.

I would want the motor to press the am-home switch, whose state I would poll while taking one step at a time.

The BECOMES vs. IS is important because you want to make the GoHome() action happen ONCE, not over and over.

I think you could also read between the lines, and see that you should have a function that does what is needed to cause the motor to go home. That function should be called by loop(). The code should NOT be in loop().

I'm not sure why you need acceleration and speed control when homing the motor. I'm not sure why you think you need to call runSpeed() over and over.

I'm not convinced that you know what moveTo() does.

I'm not convinced that you understand relative moves vs. absolute moves, what you are moving relative to.

Specifically, where do you tell the AccelStepper class that the instance of the motor IS home?