Loop 1 Rotation with Acceleration and Deceleration

Hello All!

I'm getting back into Arduino programming and I'd like some help with a seemingly simple code. I have stepper motor and driver where I just want to loop one rotation with acceleration and deceleration. My code is shown below.

The stepper motor spins but changing the speed and acceleration values in the code does nothing. Any insight would be helpful. Thanks!

#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

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

void setup()
{  
stepper.setMaxSpeed(100);
stepper.setAcceleration(100);
}
void loop()
{    
// Set the target position:
stepper.moveTo(400);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
delay(1000);

}

Here is a picture of my schematic and wiring:


image

What speed do You want? Does the stepper reach the target position?
Both acceleration and speed are set to 100. The delay(1000) would make the stepper reach top speed during the first loop.

Hi Railroader! Thanks for replying.

I'm just changing speed (and acceleration) randomly and nothing changes. I have my stepper set to 400 steps/rev. It just rotates and rotates without any accel or decel.

Okey. I don't use AccelStepper but I recommend You to read information accompanying the lib. What exactly do the lib functions do. Are You using them correctly? I've seen members sometimes use the wrong function.
I'm sure some more knowing helper steps in.

Your posted code, wiring diagram and written description do not agree.

Your posted code uses pins 2 and 3 for step and dir. The diagram shows pins 8 and 9 for step and dir. The orange wire seems to go to pin 0, the hardware serial RX pin.

The posted code will run the motor to position 400 and stop. The motor will not continue running. The moveTo() function will move to an absolute position.

Here is an example code that will run the motor one turn on each button push at the speed set by a potentiometer.

// rotate stepper 1 turn with push of button at speed set by potentiometer.
// by groundFungud AKA charlie goulding.

// change step, dir, button and pot pins to match your set up
// tested with an Uno with CNC shield V3, 
// hence the step, dir and enable pin numbers
// stepper set to 4X microstepping

#include <AccelStepper.h>

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;  // for CNC shield
const byte buttonPin = 9;
const byte potPin = A0;

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

bool lastButtonState = HIGH;

void setup()
{
   Serial.begin(115200);
   pinMode(enablePin, OUTPUT);
   pinMode(buttonPin, INPUT_PULLUP);
   digitalWrite(enablePin, LOW);
   // Change these to suit your stepper if you want
   stepper.setMaxSpeed(100);
   stepper.setAcceleration(500);
}

void loop()
{      
   int potValue = analogRead(potPin); // read pot
   potValue = map(potValue, 0, 1023, 50, 4000);  
   stepper.setMaxSpeed(potValue); // change the speed by pot setting.
   stepper.setAcceleration(potValue/2);
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      bool buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            Serial.println(potValue);
            stepper.move(800);  // move 800 steps, RELATIVE to current position
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
   stepper.run();
}

Hi groundFungus. Thanks so much for the reply. You are correct. I just switched the pins and reflected that in the code. I'll try 8 and 9. I pulled the orange wire from the arduino and just left it there. It is connected to Enable on the driver that is not used.

Thanks for the code! I can maybe implement this as a next project. What I am working on now is continuously rotating shaft that has acceleration and deceleration. Can you assist with code for that?

Is this what you are trying to do? You realize 100 steps per second is only 15 RPM?

#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

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

void setup()
{  
stepper.setMaxSpeed(100);
stepper.setAcceleration(100);
}
void loop()
{    
// Set the target position:
stepper.moveTo(400);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
delay(1000)
stepper.moveTo(0);
stepper.runToPosition();
delay(1000);
}

Hi @JCA34F! Thank you very much for the reply. I uploaded the code and my mechanism (see below) rotates. However changing the values for acceleration and max speed do nothing. I don't understand why. Could it be the steps/rev setting on my driver? I can try varying that setup to see if anything changes.

image

I commented out the whole loop section just to test cause and effect and the motor still rotates?!?! What am I missing?

#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

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

void setup()
{  
stepper.setMaxSpeed(10000);
stepper.setAcceleration(1000);
}

void loop()
{    
// Set the target position:
//stepper.moveTo(800);
// Run to target position with set speed and acceleration/deceleration:
//stepper.runToPosition();
//delay(1000);
//stepper.moveTo(0);
//stepper.runToPosition();
//delay(1000);
}

This should not be possible. Did You successfully download the code to the controller?

Try and declare dirPin and stepPin as OUTPUT in setup.

 pinMode(dirPin, OUTPUT); //Dir
 pinMode(stepPin, OUTPUT); //Step

Hi @Railroader Thanks for the suggestion. I tried declaring the pins, but still the same outcome: no change in speed or acceleration. I'm baffled.

What the hack... Reply #13 again. This is not possible...
I don't use AccelStepper and don't know it. I've made my own stepper code. What does "#define motorInterfaceType 1" really do?
As I surely said before, use the magnifier and read info text, advices, examples to find out what the different function really do.

Try and comment out the last AccelStepper line in Setup. Any change?
If not, comment out the second line? Any change? It just has to be....

10,000 steps per second (1500 RPM) is impossible with AccelStepper and a 16Mhz Arduino, about 4000 is max. Try 1000 to 4000 SPS (150 to 600 RPM) and acceleration up to 600.
Steps per second = RPM * steps per revolution / 60.

#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

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

void setup()
{  
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
}
void loop()
{    
// Set the target position:
stepper.moveTo(400);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
while(1); // press reset button to repeat
}

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