2

I want to turn on my motor and count how many total rounds it has until my limit switch is switched on. After that, I want to count rounds counterclockwise until another limit switch is pushed. For this, I'm using AccelStepper and ezButton lib.

Here is my code:

      if ((limitSwitch_XV.getState() == LOW) && (limitSwitch_XE.getState() == LOW) && rightDirectionX) {
        while (1) {
          stepper1.step(-1);
          stageSizeX++;
        }
      }

But here I've got an error which says that I can't use int in step(): 'virtual void AccelStepper::step(long int)' is protected within this context stepper1.step(1);

So how can I count the steps and go step by step to the given direction until I reach the limit switch with AccelStepper?

3
  • Why do you need to control every step yourself? You could also set it to a specific speed, and then in a loop call stepper1.run() and also check for the limit switch. As soon as the limit switch is activated you can stop the motor and read out the position. The difference in position between before and after is the number of steps traveled.
    – chrisl
    Commented Oct 6, 2022 at 15:03
  • can you give me an example for it?
    – Korte Alma
    Commented Oct 6, 2022 at 15:21
  • and also how can I change the direction then?
    – Korte Alma
    Commented Oct 6, 2022 at 15:56

1 Answer 1

0

You get that error, because the step() method is protected in the AccelStepper class. It is not meant for calling by the user. AccelStepper instead wants you to state, what kind of movement you want (moving a specific distance/to a specific point/with a specific speed) and then frequently call the AccelStepper.run() method (or AccelStepper.runSpeed()), which will then execute the steps, when it is time to do so based on your planned movement.


If you want to solve your problem you have basically two ways to do this:

  • With AccelStepper: Set the stepper to run at a specific speed in the correct direction, then in a loop calling stepper1.runSpped() and testing your limitswitch with digitalRead(). So something along the lines of:

      stepper1.setCurrentPosition(0);
      stepper1.setSpeed(20); // Change speed according to your needs, negative values let the motor rotate in the other direction
      while(limitSwitch_XV.getState() == HIGH){ // Assuming, that limit switch is not pressed when HIGH
          stepper1.runSpeed();
      }
      int revolutions = stepper1.currentPosition() / steps_per_revolution;
    
  • or without AccelStepper: Depending on your stepper driver you can use a less sophisticated stepper library (like Arduinos Stepper library) or just control the stepper directly with digitalWrite() calls (to the Dir pin and to the Pulse pin). In that case you can do each step yourself, after each checking for the limit switch with digitalRead(). Something along the lines (I assume a stepper driver with dir pin and pulse pin, not one with multiple phase pins):

      digitalWrite(dir_pin, direction); // provide HIGH or LOW here depending on wanted direction
      int pos = 0;
      while(limitSwitch_XV.getState() == HIGH){
          digitalWrite(pulse_pin, HIGH);
          delayMicroseconds(10);
          digitalWrite(pulse_pin, LOW);
          pos++;
          delayMicroseconds(50); // Change this value according to the wanted speed
      }
      int revolutions = pos / steps_per_revolution;
    

Note, that both code snippets are totally untested and you need to build the surrounding code yourself. They only highlight the principle.

2
  • What is here steps_per_revolution?
    – Korte Alma
    Commented Oct 8, 2022 at 17:42
  • You wanted to know the number of revolutions until you hit the limit switch. For converting traveled steps to number of revolutions you need the number of steps per revolution. That's a value, that is specific to your motor and driver configuration.
    – chrisl
    Commented Oct 8, 2022 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.