# How and where to put in acceleration/deceleration?

I have gotten the sketch to run the required revaluations back and forth for my surface grinder X axis but can not figure out what and where to put code for accelerate, run x revolutions, and then decelerate then reverse direction continuous.
Any help would be greatly appreciated as I am 75 years old and trying to automate some of my machine shop equipment but the old grey cells are not cooperating as much as I had hoped.
Here is the code so far and it will do everything but accelerate/decelerate at each end of the X travel.

x_axis_on_surface_grinder_1st.ino (1007 Bytes)

Have you hooked this up to the stepper motor yet? I don't have one to test it out and observe what it's doing but just off hand it looks like it should work as is after defining the variables to your particular stepper motor. So how I'm reading this is the program is telling your motor to move forward 36 revolutions then back 36 revolutions.

Maybe I'm missing something, do you have a guide you are using to help with the project? Or a model number of the stepper motor?

I have the program running the stepper just fine but I need it to slow down prior to each end of the travel & accelerate or ramp up the speed so it transitions smoothly on each end.
in other words start slowly and ramp up to speed then ramp down to a 500 mls pause and then reverse and do it over & over.
This is my first attempt to run one of the steppers ever!!
Thanks for the interest in my attempts and ALL help is greatly appreciated.
Bill

I think in order to accomplish the slowing effect you will have to break the region of travel down into different sectors and use your "myStepper.setSpeed(int)" function to different speeds over the travel path.

it might be possible to increment using a loop, so just as a rough idea maybe something like this?

``````void setup(){

for (int i = 0, i < 400, i++){
myStepper.setSpeed(i);
myStepper.step(some distance);
}

// however far you need to travel until de-acceleration occurs

for (i = 400, i > 0, i--){
myStepper.setSpeed(i);
myStepper.step(some distance);
}
}
``````

you may need to use bigger jumps in incremented values to have the effect that you would like but I something like that may work. You may need some conditional statements to let the program which way it wants to go depending on current position of the motor. Please keep in mind the above code probably wont work its just the pseduocode to help get my idea across. Let me know what you think about that idea and I'll help out if you further need it.

Bill, it might be worth your while to check out the "AccelStepper" library.
I've only done one quick test in one direction at a constant speed so far, but it supports acceleration and deceleration, forwards and reverse. You'd need to read the documentation to find out exactly how to do what you want. There are a number of examples that will help. After installation, they'll be under >File >Examples >AccelStepper in your IDE.

The main page:-
AccelStepper main page

The class reference:-
AccelStepper class reference

I hope this makes things a little easier.

here is a little better idea of what I was thinking with the code it is compiling however I have no clue if it works without having the hardware. Oldsteve might be on to something with that other library, it seems to have a lot more funcionality which could possibly make life a lot easier.

``````/*
Stepper Motor Controller
language: Wiring/Arduino

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 2 and 5 of the Arduino.

The motor moves 9 rev per cycle in one direction, then 9 rev in the other.

Created 11 Mar. 2007 by Tom Igoe
Modified 6 April 2016 Bill Dewar

*/

// define the pins that the motor is attached to. You can use
// any digital I/O pins.

#include <Stepper.h>

#define motorSteps 400     // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 2
#define motorPin2 5
int i = 0;

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1, motorPin2);

void setup() {
// set the motor speed at 4000 RPMS:
myStepper.setSpeed(i);

}

void loop() {
// // even values of a should move one way where odd should move other
int a = 0;

// setting acceleration loop

for(i = 0; i < 401; i+=50){
myStepper.setSpeed(i);
if ( a % 2 == 0 ){
myStepper.step(400);
}else{
myStepper.step(-400);
}
}

// middle path full speed
if (a % 2 == 0){
myStepper.step(8000);
}else{
myStepper.step(-8000);
}

// de-acceleration loop
for (i = 400; i > 0; i -= 50){
myStepper.setSpeed(i);
if (a % 2 == 0){
myStepper.step(400);
}else{
myStepper.step(-400);
}
}
// even values of a should move one way where odd should move other
a++;
}
``````

I thank you both very much, you have given me some direction and some good samples of code.
I will work on the ideas you guys have offered, this is new to me on the coding but feel fairly comfortable with the electrical side.
Thanks ZBay and OldSteve for the help, I will check back in when iI get through the sugestions you have given.
Once again thanks
Bill

I forgot to post the equipment I was trying to get codded and I have run into a mental block trying to mesh the accel sketch into what I have got lashed up already?
Here are the data attachments on driver & motor, also switch settings are 1 off, 2 on, 3 on, 4 off, 5 on, 6 on, 7 on, 8 on.
Thanks again
Bill

M542T.pdf (329 KB)

I'll have to look more closely once I get home but wouldn't your output pins 2 and 5 be connected to the pulse + and - pins on the driver board (m542t)? If possible can you snap a picture of what you currently have wired up? It's also possible I'm totally misunderstanding the question.

Bill, can you draw us a diagram of your wiring?
I don't follow how you're using the two "Stepper" library output pins to connect to that particular controller.