Just finished building a camera slider that uses 3 steppers. Slide, pan and tilt.
It was copied from a youtube vid and I have done some minor changes to the sketch.
It works pretty good but I need to add acceleration and deceleration to its movement. I'm not sure how to do that.
Stepper #1 is the one I need to work on.
If anyone can help, here is the sketch :
#include <AccelStepper.h>
#include <MultiStepper.h>
#define JoyX A0 // Joystick X pin
#define JoyY A1 // Joystick Y pin
#define slider A2 // Slider potentiometer
#define inOutPot A3 // In and Out speed potentiometer
#define JoySwitch 10 // Joystick switch connected
#define InOutSet 12 // Set Button
#define limitSwitch 11
#define inLED 8
#define outLED 9
// Define the stepper motors and the pins the will use
AccelStepper stepper1(1, 7, 6); // (Type:driver, STEP, DIR)
AccelStepper stepper2(1, 5, 4);
AccelStepper stepper3(1, 3, 2);
MultiStepper StepperControl; // Create instance of MultiStepper
long gotoposition[3]; // An array to store the In or Out position for each stepper motor
int JoyXPos = 0;
int JoyYPos = 0;
int sliderPos = 0;
int currentSpeed = 100;
int inOutSpeed = 100;
int XInPoint = 0;
int YInPoint = 0;
int ZInPoint = 0;
int XOutPoint = 0;
int YOutPoint = 0;
int ZOutPoint = 0;
int InandOut = 0;
void setup() {
// Set initial seed values for the steppers
stepper1.setMaxSpeed(3000);
stepper1.setSpeed(200);
stepper2.setMaxSpeed(3000);
stepper2.setSpeed(200);
stepper3.setMaxSpeed(3000);
stepper3.setSpeed(200);
pinMode(JoySwitch, INPUT_PULLUP);
pinMode(InOutSet, INPUT_PULLUP);
pinMode(limitSwitch, INPUT_PULLUP);
pinMode(inLED, OUTPUT);
pinMode(outLED, OUTPUT);
// Create instances for MultiStepper - Adding the 3 steppers to the StepperControl instance for multi control
StepperControl.addStepper(stepper1);
StepperControl.addStepper(stepper2);
StepperControl.addStepper(stepper3);
// Move the slider to the initial position - homing
while (digitalRead(limitSwitch) != 0) {
stepper1.setSpeed(1000);
stepper1.runSpeed();
stepper1.setCurrentPosition(0); // When limit switch pressed set position to 0 steps
}
delay(20);
// Move 200 steps back from the limit switch
while (stepper1.currentPosition() != -200) {
stepper1.setSpeed(-1000);
stepper1.run();
}
}
void loop() {
// Limiting the movement - Do nothing if limit switch pressed or distance traveled in other direction greater then 80cm
while (digitalRead(limitSwitch) == 0 || stepper1.currentPosition() < -64800) {}
// If Joystick pressed increase the Pan and Tilt speeds
if (digitalRead(JoySwitch) == 0) {
currentSpeed = currentSpeed + 200;
delay(200);
}
// If Set button is pressed - toggle between the switch cases
if (digitalRead(InOutSet) == 0) {
delay(1000);
// If we hold set button pressed longer than 1 second, reset the in and out positions
if (digitalRead(InOutSet) == 0) {
InandOut = 4;
}
switch (InandOut) {
case 0: // Set IN position
InandOut = 1;
XInPoint = stepper1.currentPosition(); // Set the IN position for steppers 1
YInPoint = stepper2.currentPosition(); // Set the IN position for steppers 2
ZInPoint = stepper3.currentPosition(); // Set the IN position for steppers 3
digitalWrite(inLED, HIGH); // Light up inLed
break;
case 1: // Set OUT position
InandOut = 2;
XOutPoint = stepper1.currentPosition(); // Set the OUT Points for both steppers
YOutPoint = stepper2.currentPosition();
ZOutPoint = stepper3.currentPosition();
digitalWrite(outLED, HIGH);
break;
case 2: // Move to IN position / go to case 3
InandOut = 3;
inOutSpeed = analogRead(inOutPot)*3; // Auto speed potentiometer
// Place the IN position into the Array
gotoposition[0] = XInPoint;
gotoposition[1] = YInPoint;
gotoposition[2] = ZInPoint;
stepper1.setMaxSpeed(inOutSpeed);
stepper2.setMaxSpeed(inOutSpeed);
stepper3.setMaxSpeed(inOutSpeed);
StepperControl.moveTo(gotoposition); // Calculates the required speed for all motors
StepperControl.runSpeedToPosition(); // Blocks until all steppers are in position
delay(200);
break;
case 3: // Move to OUT position / go back to case 2
InandOut = 2;
inOutSpeed = analogRead(inOutPot)*3;
// Place the OUT position into the Array
gotoposition[0] = XOutPoint;
gotoposition[1] = YOutPoint;
gotoposition[2] = ZOutPoint;
stepper1.setMaxSpeed(inOutSpeed);
stepper2.setMaxSpeed(inOutSpeed);
stepper3.setMaxSpeed(inOutSpeed);
StepperControl.moveTo(gotoposition); // Calculates the required speed for all motors
StepperControl.runSpeedToPosition(); // Blocks until all are in position
delay(200);
break;
case 4: // If Set button is held longer than 1 second go back to case 0
InandOut = 0;
digitalWrite(inLED, LOW);
digitalWrite(outLED, LOW);
delay(1000);
break;
}
}
// Joystick X - Pan movement
JoyXPos = analogRead(JoyX);
// if Joystick is moved left, move stepper 2 or pan to left
if (JoyXPos > 600) {
stepper2.setSpeed(currentSpeed);
}
// if Joystick is moved right, move stepper 2 or pan to right
else if (JoyXPos < 400) {
stepper2.setSpeed(-currentSpeed);
}
// if Joystick stays in middle, no movement
else {
stepper2.setSpeed(0);
}
//Joystick Y - Tilt movement
JoyYPos = analogRead(JoyY);
if (JoyYPos > 600) {
stepper3.setSpeed(currentSpeed);
}
else if (JoyYPos < 400) {
stepper3.setSpeed(-currentSpeed);
}
else {
stepper3.setSpeed(0);
}
// Slider potentiometer
sliderPos = analogRead(slider);
// If potentiometer is turned left, move slider left
if (sliderPos > 600) {
sliderPos = map(sliderPos, 600, 1024, 0, 3000);
stepper1.setSpeed(sliderPos); // Increase speed as turning
}
// If potentiometer is turned right, move slider right
else if (sliderPos < 400 ) {
sliderPos = map(sliderPos, 400, 0, 0, 3000);
stepper1.setSpeed(-sliderPos); // Increase speed as turning
}
// If potentiometer in middle, no movement
else {
stepper1.setSpeed(0);
}
// Execute the above commands - run the stepper motors
stepper1.runSpeed();
stepper2.runSpeed();
stepper3.runSpeed();
}
Welcome to the forum. Please use code tags to enclose your code. It's the </> button on the text editor. Please edit your post.
If you are lucky someone might write your code for you. But how things usually go down is that you tell us what your program does and how it differs from your expectation.
I don't know what you mean by "add acceleration and deceleration". If the thing moves and stops it is both accelerating and decelerating. You need to give us more information about what you are trying to achieve.
It appears that the MiltiStepper class does not support acceleration/decleration. It is designed to get every motor to its final position at the same time.
blh64:
It appears that the MiltiStepper class does not support acceleration/decleration. It is designed to get every motor to its final position at the same time.
Yes, you are right about multistepper.
I believe accelstepper is the one that controls acceleration/deceleration and its library is included in the sketch, but I don't know how to use it.
I want stepper #1 to ramp up to speed then at the end if its travel, ramp down to stop.
Metallor:
I don't know what you mean by "add acceleration and deceleration". If the thing moves and stops it is both accelerating and decelerating. You need to give us more information about what you are trying to achieve.
Thanks for the welcome.
Consider when you drive your car. When you press the throttle, it doesn't go instantly from 0-100kmh. It takes time (maybe 10secs) to reach the speed and is therefore accelerating.
My stepper #1 needs a little time to reach its terminal speed else it will skip steps.
Stretcher:
Um well, this is arduino.cc insn't it ? A forum with people who program arduinos ?
Yes. That doesn't mean that we write code for you. It means we help you understand why the code YOU write (or find) doesn't do what you expect it to do.
You only need Multistepper if it is essential for two or more motors to move different numbers of steps in exactly the same time - as for example with a CNC milling machine or a 3D printer.
As others have said Multistepper does not support acceleration.
If you need co-ordinated movement AND acceleration it is not very difficult to do that with your own code.
PaulS:
Yes. That doesn't mean that we write code for you. It means we help you understand why the code YOU write (or find) doesn't do what you expect it to do.
That is, we'll help if you don't cop an attitude.
Nothing wrong with my attitude mate....I'm just here because I need help.
I'm a beginner at writing to arduino so as yet I don't understand much of it.
Look at the "bounce" example under Files->Examples->AccelStepper. It sets the acceleration and max speed in the setup() and then moves clockwise/counter-clockwise. It will ramp up then down and then switch direction
Thanks for the reply.
Ok I copied a line (setAcceleraion) from that example and added to the portion of the sketch below.
When I tested it, it didn't seem to do anything.
case 2: // Move to IN position / go to case 3
InandOut = 3;
inOutSpeed = analogRead(inOutPot)*2; // Auto speed potentiometer
// Place the IN position into the Array
gotoposition[0] = XInPoint;
gotoposition[1] = YInPoint;
gotoposition[2] = ZInPoint;
stepper1.setMaxSpeed(inOutSpeed);
stepper2.setMaxSpeed(inOutSpeed);
stepper3.setMaxSpeed(inOutSpeed);
stepper1.setAcceleration(20);
StepperControl.moveTo(gotoposition); // Calculates the required speed for all motors
StepperControl.runSpeedToPosition(); // Blocks until all steppers are in position
delay(200);
break;
Do you have another simple program that can make the motors move - so that we can eliminate wiring and power problems?
...R
The complete program is in my first post.
I don't have any other program as this is my first project using arduino, and no need to worry about the wiring as it works perfectly with the program originally written for it......Besides, I'm an electronics tech.
Stretcher:
The complete program is in my first post.
I want to see the complete program that includes the changes you referred to in Reply #13.
And please DO NOT make any more changes to the code in your Original Post - just add the latest program in your next Reply. That way way we an compare the versions if necessary.
Ok I see what you mean. Well the changes I made to the original program are only a few numbers that control speed and distance because my slider is longer and I needed it to run faster, but faster created the skipping, and that's why it needs acceleration. Anyway this is the current program including that acceleration addition :
#include <AccelStepper.h>
#include <MultiStepper.h>
#define JoyX A0 // Joystick X pin
#define JoyY A1 // Joystick Y pin
#define slider A2 // Slider potentiometer
#define inOutPot A3 // In and Out speed potentiometer
#define JoySwitch 10 // Joystick switch connected
#define InOutSet 12 // Set Button
#define limitSwitch 11
#define inLED 8
#define outLED 9
// Define the stepper motors and the pins the will use
AccelStepper stepper1(1, 7, 6); // (Type:driver, STEP, DIR)
AccelStepper stepper2(1, 5, 4);
AccelStepper stepper3(1, 3, 2);
MultiStepper StepperControl; // Create instance of MultiStepper
long gotoposition[3]; // An array to store the In or Out position for each stepper motor
int JoyXPos = 0;
int JoyYPos = 0;
int sliderPos = 0;
int currentSpeed = 100;
int inOutSpeed = 100;
int XInPoint = 0;
int YInPoint = 0;
int ZInPoint = 0;
int XOutPoint = 0;
int YOutPoint = 0;
int ZOutPoint = 0;
int InandOut = 0;
void setup() {
// Set initial seed values for the steppers
stepper1.setMaxSpeed(3000);
stepper1.setSpeed(200);
stepper2.setMaxSpeed(3000);
stepper2.setSpeed(200);
stepper3.setMaxSpeed(3000);
stepper3.setSpeed(200);
pinMode(JoySwitch, INPUT_PULLUP);
pinMode(InOutSet, INPUT_PULLUP);
pinMode(limitSwitch, INPUT_PULLUP);
pinMode(inLED, OUTPUT);
pinMode(outLED, OUTPUT);
// Create instances for MultiStepper - Adding the 3 steppers to the StepperControl instance for multi control
StepperControl.addStepper(stepper1);
StepperControl.addStepper(stepper2);
StepperControl.addStepper(stepper3);
// Move the slider to the initial position - homing
while (digitalRead(limitSwitch) != 0) {
stepper1.setSpeed(1000);
stepper1.runSpeed();
stepper1.setCurrentPosition(0); // When limit switch pressed set position to 0 steps
}
delay(20);
// Move 200 steps back from the limit switch
while (stepper1.currentPosition() != -200) {
stepper1.setSpeed(-100);
stepper1.run();
}
}
void loop() {
// Limiting the movement - Do nothing if limit switch pressed or distance traveled in other direction greater then 80cm
while (digitalRead(limitSwitch) == 0 || stepper1.currentPosition() < -20600) {}
// If Joystick pressed increase the Pan and Tilt speeds
if (digitalRead(JoySwitch) == 0) {
currentSpeed = currentSpeed + 100;
delay(200);
}
// If Set button is pressed - toggle between the switch cases
if (digitalRead(InOutSet) == 0) {
delay(1000);
// If we hold set button pressed longer than 1 second, reset the in and out positions
if (digitalRead(InOutSet) == 0) {
InandOut = 4;
}
switch (InandOut) {
case 0: // Set IN position
InandOut = 1;
XInPoint = stepper1.currentPosition(); // Set the IN position for steppers 1
YInPoint = stepper2.currentPosition(); // Set the IN position for steppers 2
ZInPoint = stepper3.currentPosition(); // Set the IN position for steppers 3
digitalWrite(inLED, HIGH); // Light up inLed
break;
case 1: // Set OUT position
InandOut = 2;
XOutPoint = stepper1.currentPosition(); // Set the OUT Points for both steppers
YOutPoint = stepper2.currentPosition();
ZOutPoint = stepper3.currentPosition();
digitalWrite(outLED, HIGH);
break;
case 2: // Move to IN position / go to case 3
InandOut = 3;
inOutSpeed = analogRead(inOutPot)*2; // Auto speed potentiometer
// Place the IN position into the Array
gotoposition[0] = XInPoint;
gotoposition[1] = YInPoint;
gotoposition[2] = ZInPoint;
stepper1.setMaxSpeed(inOutSpeed);
stepper2.setMaxSpeed(inOutSpeed);
stepper3.setMaxSpeed(inOutSpeed);
stepper1.setAcceleration(20);
StepperControl.moveTo(gotoposition); // Calculates the required speed for all motors
StepperControl.runSpeedToPosition(); // Blocks until all steppers are in position
delay(200);
break;
case 3: // Move to OUT position / go back to case 2
InandOut = 2;
inOutSpeed = analogRead(inOutPot)*2;
// Place the OUT position into the Array
gotoposition[0] = XOutPoint;
gotoposition[1] = YOutPoint;
gotoposition[2] = ZOutPoint;
stepper1.setMaxSpeed(inOutSpeed);
stepper2.setMaxSpeed(inOutSpeed);
stepper3.setMaxSpeed(inOutSpeed);
StepperControl.moveTo(gotoposition); // Calculates the required speed for all motors
StepperControl.runSpeedToPosition(); // Blocks until all are in position
delay(200);
break;
case 4: // If Set button is held longer than 1 second go back to case 0
InandOut = 0;
digitalWrite(inLED, LOW);
digitalWrite(outLED, LOW);
delay(1000);
break;
}
}
// Joystick X - Pan movement
JoyXPos = analogRead(JoyX);
// if Joystick is moved left, move stepper 2 or pan to left
if (JoyXPos > 600) {
stepper2.setSpeed(currentSpeed);
}
// if Joystick is moved right, move stepper 2 or pan to right
else if (JoyXPos < 400) {
stepper2.setSpeed(-currentSpeed);
}
// if Joystick stays in middle, no movement
else {
stepper2.setSpeed(0);
}
//Joystick Y - Tilt movement
JoyYPos = analogRead(JoyY);
if (JoyYPos > 600) {
stepper3.setSpeed(currentSpeed);
}
else if (JoyYPos < 400) {
stepper3.setSpeed(-currentSpeed);
}
else {
stepper3.setSpeed(0);
}
// Slider potentiometer
sliderPos = analogRead(slider);
// If potentiometer is turned left, move slider left
if (sliderPos > 600) {
sliderPos = map(sliderPos, 600, 1024, 0, 3000);
stepper1.setSpeed(sliderPos); // Increase speed as turning
}
// If potentiometer is turned right, move slider right
else if (sliderPos < 400 ) {
sliderPos = map(sliderPos, 400, 0, 0, 3000);
stepper1.setSpeed(-sliderPos); // Increase speed as turning
}
// If potentiometer in middle, no movement
else {
stepper1.setSpeed(0);
}
// Execute the above commands - run the stepper motors
stepper1.runSpeed();
stepper2.runSpeed();
stepper3.runSpeed();
}
Robin2:
When you said (in Reply #13)do you mean that there was no change in the behaviour?
OR
do you mean that the motors do not move but they did move before the change in the code?
I don't understand why (in Reply #13) you saidwhen you have already been told more than once that MultiStepper does not do acceleration.
...R
1/ When I added the line ( stepper1.setAcceleration(20); ) , it did not alter the behavior.
2/ I realize MultiStepper does not do acceleration but I said I believe accelstepper does do acceleration and it is called in the very first line of the sketch. So what's that got to do with multistepper ? ... can't the sketch be changed to do what I need ?
Please have a look here and you'll see what the original sketch does :