Why is my code not working

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);
      }
    }
}

Once the "moveto" is done, the stepper shaft is where it should be, so that won't repeat.

You just need to think this through a bit, after studying the documentation and examples.

Please click Tools->auto Format and then re-post your code.

#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.move(2048);
    stepper.run();

    if (stepper.distanceToGo() == 0) {
      State = 0;
      Serial.print(State);
    }
  }
}

You have an idea why the stepper wont stop moving with the "stepper.move" command?

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.

a7

Sure, check the docs at AccelStepper: AccelStepper Class Reference

Solved the problem, Thanks.

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.

So not your problem. :expressionless:

a7

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.