Rotating the stepper motor

Hello,
I just started learning programming
I want the stepper motor to rotate 400 steps 3 times at 30 second intervals. (It should stop if my potValue reaches 1023 while doing this) After delay(30000) I wrote "stepper.moveTo(400); " again but it didn't work. Is it possible to do that?
Thanks for your help.


#include <AccelStepper.h>
#define dirPin 8
#define stepPin 9
#define motorInterfaceType 1
const byte potPin = A0;
int potValue=0;

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);


void setup() 
{
  stepper.setMaxSpeed(1000); // Set the maximum speed in steps per second
  stepper.setAcceleration(200);// Set the acceleration
  Serial.begin(9600); //Set the serial communication speed
}

void loop() 
{
 potValue = analogRead(potPin);//at the beginning of each cyle, check the value of the potentiometer

  if(potValue !=1023)
  {
  stepper.moveTo(400);// Set the target position                                   
  stepper.runToPosition();// Run to target position with set speed and acceleration/deceleration
  Serial.print("Potentiometer at ");
  Serial.println(potValue);
  delay(30000);
  
  }
  else
  {
    stepper.stop();
  }
  
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial

This is absolute positioning. That means you command the stepper to go to the position 400 steps past the starting position. If you do the same after 3 sec, the stepper is already there and will not move again.

stepper.move(400); 

will do a relative move starting at the actual position.

Thanks for your help.

How can I stop the stepper motor when the potValue reaches the maximum value?

With your blocking design, you can only stop the stepper after the complete 400 steps and the 3 sec delay (when the potentiometer is at maximum).
If you want to be able to stop the stepper at any time, you need a non-blocking approach.
So what do you want to do?

I want my stepper motor to stop if the potValue reaches 1023 while the stepper motor has not completed 400 steps (waiting 30 seconds after 400 steps and taking 400 steps again).

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