Im currently trying to write a programm which lets a stepper motor do a full rotation after the press of a button. My problem is that the stepper only does this one time and won't do it again after another press. The problem i just described happens when i use the "stepper.moveTo" command. When i use the "stepper.move" command the stepper never stops rotating after the press of the button. I would like to know whats wrong with both things altough im probably going to use and prefer the "stepper.move" command for my needs.
#include <Button.h>
#include <AccelStepper.h>
AccelStepper stepper;
Button button1(A0); // Connect your button between pin 2 and GND
int State = 0;
void setup() {
button1.begin();
stepper.setMaxSpeed(200);
stepper.setAcceleration(200);
while (!Serial) { }; // for Leos
Serial.begin(9600);
}
void loop() {
if (button1.pressed()) {
State = 1;
Serial.print(State);
}
while(State == 1) {
stepper.moveTo(2048);
stepper.run();
if(stepper.distanceToGo() == 0) {
State = 0;
Serial.print(State);
}
}
}
Are you copying patterns from example code for this library?
I ask because many button libraries need a frequent service call that takes a peek at the button and updates its internal state; later you use other methods from the library to find out about up or down or just got pressed and so forth.
Omitting that wou,d mean bad or no functioning on the button.
What exact library is that? Post a link or say, there are a many libraries, all a bit different even if they are more or less similar.
Thanks for the link. That may be a library I did not ever tear apart.
Glancing at the code it seems the authors have distributed the work that is sometimes done in a top of the loop update method so users don't needa worry about it.
I kind of figured out a way around that problem. I reseted the steppers current position with "stepper.setCurrentPosition = 0" right after it reached its position of 2048. Works for my project.