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.
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.