Stepper motor homing question using AccelStepper.h

Hello Forum,
I have been working my way though a stepper motor homing program.
The simple way to think about the project would be the stepper motor is driving the hour hand of a clock.
so first, on power up i want to home the motor during the void setup() onto a switch, then reverse off the switch and get to the 12o’clock position. (by using an offset amount of steps from the switch). at this point i would consider the system to be ready to enter the main loop.

and i have got it to work!!.. not really :frowning:
however by messing around i have found that if i put my reverse of the switch code into the main void loop() then it works , and i don’t understand why i cant call my 'void bounce() at the bottom the void setup()

I would be very grateful if anyone could point out my stupidity… my feeling is the ’ do while’ isnt working for this but, ive been going round in circles.

(I have been using the mega for 4 weeks and got alot of items working, but i am finding the AccelStepper library quite difficult to get my head round or find out how all the functions should be used.)

I hope this post + code works out!.. its my first one :o

Code included for the working state

Thanks for your time

#include<AccelStepper.h>
AccelStepper stepper (AccelStepper::DRIVER,50,48);

int limitswitch = 40; // input pin of limit
int enPin = 52; // enable pin on drive
int state = 0;

void setup()
{
  stepper.setMaxSpeed(4500);
  stepper.setAcceleration(400);
  stepper.setSpeed (800);
    pinMode(50,OUTPUT);   //Step
    pinMode(48,OUTPUT);   //direction
    pinMode(40,INPUT);    //Limit Switch
    pinMode(52,OUTPUT);   //Drive enable
      digitalWrite(50,LOW);
      digitalWrite(48,LOW);
      digitalWrite(52,HIGH); //enable drive

 // stepper.setCurrentPosition(0);
  
  stepperHome();  // stepper homing loop
  resetPos();     // set position to 0 loop
  stepper.stop(); // been told to call this between movements ?
 // bounce();     // this wont work
}

void loop()             //main loop
          {
          Serial.begin(9600);
          stepper.moveTo(-8000); 
          //stepper.setSpeed(-1500);
          stepper.run(); 
          }

void stepperHome()      // find home
          {
          while(digitalRead(limitswitch) == LOW)
          {
          stepper.move(100);
          stepper.setSpeed(3500);
          stepper.run();
          }
          }

void resetPos()         //Reset position
          {
          while(digitalRead(limitswitch) == HIGH)
          {
          stepper.setCurrentPosition(0);
          }
          }

//void bounce()           // bounce off or reverse of limit switch
                    //{
                    //while (state == 0)
                    //{
                    //if (stepper.currentPosition() != -4000) {
                    //stepper.moveTo(-4000); 
                    //stepper.runSpeedToPosition();
                    //stepper.setSpeed(1500);
                    //stepper.run(); 
                    //}
                    //}
                    //}
  while (state == 0)

How does state ever become not-zero so that it can get out of this loop?

hi MorganS,

I have been trying out a few different approaches but without success. i have included my latest failed attempt below.

I am wondering if the fuction (stepper.distanceToGo() is working as i expect it to,
As i am pulling the 'state' upto 1 once the homing switch goes high...
i was hoping the while == 1 would let the 'Void bouNce' run...

i was hoping the (stepper.distanceToGo() would get to 0 position enabling me to jump out by reseting the 'state' to 0.

thank you for the reply!

void resetPos()         //Reset position
          {
          while(digitalRead(limitswitch) == HIGH)
          {
          stepper.setCurrentPosition(0);
          state = 1;
          Serial.println(state);
          }
          }

void bouNce()           // bounce off or reverse of limit switch
                    {
                    while (state == 1) 
                    stepper.moveTo(-400); 
                    stepper.run();
                    Serial.println(state);
                    if (stepper.distanceToGo() == 0){
                    state = 0;
                    }
                    }

The easiest way to get to a limit switch is just to move one step at a time and check the switch between moves.

I think I would use the function runToNewPosition() and increment the value of position by one for every call to the function.

...R

Thank you Robin2!

With continuous playing about last night i got it working, i believe the real issue is, i have is lack of understanding of how the AccelStepper.h works.. :slight_smile: ... i am going to explore your proposal for using the runToNewPosition() function. and see what i can learn from that.

appreciate the feedback !