AccelStepper Asistance

Good morning everyone!

I'm having problems controlling a stepper motor using accelstepper, I don't have a lot of experience coding, so I think this is the right place to ask.

The deal is that I have a stepper that I want to move when a button is pressed, the motor will move an specific number of steps, then, it will move in the oposite direction until another button is pressed.

Ill attach the code that I'm using, if needed, I can upload a video of the motor running.

#include "AccelStepper.h" 

AccelStepper stepper (1, 9 , 8);  

#define home_switch 4 
#define sensor 7
long initial_homing=-1; 
int x;



void setup() {

   
   pinMode(home_switch, INPUT_PULLUP);
   
   delay(5);  




  while (digitalRead(home_switch)) {    
    stepper.moveTo(initial_homing);  
    initial_homing++;  
    stepper.run(); 
    delay(5);
}

  stepper.setMaxSpeed(250000.0);      
  stepper.setAcceleration(250000.0);  


  stepper.setCurrentPosition(0); 
  stepper.setMaxSpeed(100.0);      
  stepper.setAcceleration(100.0); 
  initial_homing=1;

  while (!digitalRead(home_switch)) { 
    stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
    delay(5);
  }
  
  stepper.setCurrentPosition(0);
  x == 0;
  stepper.setMaxSpeed(600000.0);      
  stepper.setAcceleration(100000.0);  

}

void loop()
{
  if(digitalRead(sensor)==HIGH){
    x=1;
  }
  
  if (x == 1){
  stepper.moveTo(200);
  while (stepper.currentPosition() != 200) 
    stepper.run();
  stepper.stop(); 
  stepper.runToPosition(); 

 
  while (digitalRead(home_switch))
  stepper.moveTo(-200);
    stepper.run();
  stepper.stop(); 
  x=0;
  }

     if (digitalRead(home_switch){
    stepper.setCurrentPosition(0);
  }


}

The setup part is a homing secuence which works well, the loop part is where im having problems.

Thanks in advance for the help!

You forgot to mention the symptom. What is it doing or not doing that doesn't meet your expectation. That's a pretty critical piece of information if you want to get help fixing something don't you think?

  x == 0;

Compare x to 0. Do nothing if it is. Do nothing if it isn't.

Why bother?

  if(digitalRead(sensor)==HIGH)

I've got to say that's a dumb name for the pin number variable. Did you get out your electronic parts catalog, open the sensor section, and just pick a random sensor? I seriously doubt that you did. So, use a name that reflects that the variable holds a pin number AND the kind of sensor attached to the pin.

  stepper.moveTo(200);
  while (stepper.currentPosition() != 200)
    stepper.run();

You could use stepper.runToPosition() to do the blocking for you, if there is some reason to do nothing until the stepper has stepped 200 times.

    stepper.run();
  stepper.stop();
  stepper.runToPosition();

It's rather pointless to use runToPosition() AFTER the stepper has gotten to the commanded position.

  while (digitalRead(home_switch))
  stepper.moveTo(-200);
    stepper.run();
  stepper.stop();
  x=0;

Which of these statements do you think is the body of the while statement? Why not use curly braces to make it clear?

Delta:

You are right, I forgot to put the symptom. When I activate the button, the stepper will move to the desired position and then go backwards to another position. The deal is that I need that when the stepper go backwards, it stops when home_switch is activated.

Paul:

I appreciate your observations, ill correct the code and test it. As a favor, could you give me an example or maybe a tip to archive my goal?

Thank you all!

Try this. Notice there is not a single WHILE.

void loop()
{
  if(digitalRead(sensor)==HIGH and x == 0){
    x=1;
  }

  if (x == 1){
    stepper.moveTo(200);
    x = 2;
  }
  if (x == 2 and stepper.currentPosition == 0) {
    x = 3;
  }
  
  if (x == 3){
    stepper.moveTo(-200);
    x = 4;
  }
  if (x == 4 and stepper.currentPosition == 0) {
    x = 0;
  }

  stepper.run();
}

...R

That program did work Robin!

Now, what if It, instead of moving -200 steps, i wanted to move until the home_switch is activated?

I'm sorry if it takes me to much time to answer, I love in an area where internet access is not reliable.

manchap23:
That program did work Robin!

Miracles do happen - I am more surprised than you are!

Now, what if It, instead of moving -200 steps, i wanted to move until the home_switch is activated?.

You already have code that can do that in your setup() function. Break that piece of code into its own function and then you can call it from setup() and from anywhere else.

HOWEVER ... you should really only need to home the motor once and after that you should always be able to get back to the home position by keeping count of the steps taken so that, at any time, you can calculate how many steps are needed to get back to zero.

...R

Yeah well, the problem is that I'm using the stepper to move some pieces. The weight of the pieces make the stepper miss some steps, that's why I need that the stepper always stops when the home_switch is activated.