Programming a stepper motor and a Linear Actuator

Hi folks,

I need to get a Stepper motor and a linear actuator to run off the same arduino board as I am building a test rig in work, so far I have got the two devices to work at the same time in roughly the sequence I need but I have some issues that I need to fix, I have never really worked with arduino before and so I am splicing bits of code together and hoping it works! my needs and problems are listed, the code I am using is also attached. Also here is the rundown of what I need the setup to do!

Stepper - Turn 180 clockwise

Piston - Extend fully up and down

Stepper - Turn 180 Anti-Clockwise

Piston - Raise up fully and lower down

then repeat 10,000 times lol

ISSUES

  • the stepper turns a different direction when turned off and back on
  • there are large delays between each cycle
  • I need to be able to adjust the speed of the piston
  • I don't really know what each piece of code does

Parts used:
-Pololou VNH5019 Motor Driver Shield

  • Arduino Uno
  • TB6600 stepper driver
  • Nema 23 Stepper Motor
  • 12v Linear Actuator

Could someone direct me to a place where I can read about what code I need and how to improve this one or couls someone tell me if this is the best way to go about coding this! Let me know if I need to add anything else!

linear_actuator_-_stepper_code.ino (2.16 KB)

{
Serial.begin(115200);
Serial.println("Dual VNH5019 Motor Shield");
md.init();
}

{Why} {is} {this} {code} {in} {curly} {braces} {?}

Why
is
it
piss-poorly
indented
?

   // Sets the two pins as Outputs 
   pinMode(stepPin,OUTPUT);  
   pinMode(dirPin,OUTPUT);

Really? I'd never have figured that out.

   delay(0); // One second delay

Bullsh*t!

for (int i = 0000; i >= -0000; i--)

Minus 0? Just how many times do you think that for loop will iterate?

   // Makes 400 pulses for making two full cycle rotation 
   for(int x = 0; x < 2000; x++) {

Does not.

Your fascination with reading the current level is admirable, but it is seriously getting in the way of getting your system to operate as you expect.

Write out, in English, what you are trying to accomplish. The code does not seem to match what you described.

But, it is hard to follow code that is so poorly indented. Tools + Auto Format would fix that before a delay(0) could finish.

thank
you
for
the
reply

But as I said in the post I am splicing code together, I have never coded before in my life so my apologies for not having a clue!

I'm going off a user manual for the shield im using and im trying to figure this out blind! Since you seem to be so enlightened in this perhaps you could offer some constructive criticism? And once again I didn't write the code I just mashed two sets together because again, I don't know what im doing lol

Please post a link to the datasheets for your stepper motor and your linear actuator.

Please also tell us what stepper motor driver you are using.

Please include your program code in your Post so we can see it without downloading it.

These links may be useful
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

...R

When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

I just mashed two sets together because again, I don't know what im doing

Perhaps you should get a clue, before you hurt yourself.

Write ONE sketch that makes the stepper motor step some number of times in one direction. Run it, and verify that the stepper stepped as far as you expected, and no farther.

Only when that works, should you then make the stepper step the same number of times in the other direction, to verify that the stepper returns to the original position.

Then, write another sketch (do not modify the first one) to extend the actuator. When you can control how far the actuator extends, and the speed that it does so at, then make the actuator retract.

Every time you add code to either sketch, RTFM and understand EXACTLY how to use the new statements, what the new functions do, etc.

Then, when it comes to to put it all together, you'll understand what part of the code makes the stepper dance and what part makes the actuator do the hokey pokey.

And, you won't be a clueless dweeb any more.

Hi PaulS

Thanks for the response!

could you link me to a dictionary or something for the language as im having a hard time understanding what's going on within the IDE! For example what does RTFM mean?

As I said before this is my first time ever coding but I don't think there is any reason to worry about my physical health lol!

Hey Christian,

I recently made a project very similar to yours, albeit with some different components here and there.

Assuming your wiring is 100% correct-

I'd say FIRST, definitely get working with the "Examples>Basics" sketches found in the Arduino IDE under File>Examples. Study them. Rework them. Make them do slightly different things.

While you're doing that, use this link for reference to lines you don't understand:

After you've gone through line-for-line the first 5 or so "Basic" sketches (which will have seemingly little to do with your project at first glance, but ought to provide a few 'ah hah' moments in terms of 'how to setup arduino code') the next step is to learn to add a new library to your project.

Google this:
'How to install a library arduino'

And download/install this library from your Arduino IDE from the Sketch>Include Libraries>'Manage Libraries' tab:

'AccelStepper' (and any other libraries you might be interested in - they'll automatically install when you click on them)

Then look at this page for reference in code involved with that particular library:

https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html

Now, after all this studying, it is still not going to get you coding much on your own. What you need to do for now is go through the examples in the AccelStepper Library and start learning to take lines from one code, and put them in another. If you go through enough examples, you'll start to see it much more clearly.

Now, here's an example (I know, this is probably very different from your code):

// How to use Accelstepper for beginners
// Written by Zach Rockafellow
// May 15th 2019

#include <AccelStepper.h> // We use the #include command to tell the program what libraries we want
AccelStepper linearA(1,8,9); // This gives a name to the motor for the library to use. It can be anything you like - here I chose 'linearA'
                             // note Arduino is 'case-sensitive'.
                             // The numbers in parenthesis refer to your pins
                             // In this particular case, we keep '1', and replace '8' and '9' with the pins that are connected to your motors Left and Right pins (labeled MotorA or MotorB on most chips)

// AccelStepper stepper(1,6,7); // This calls the same library, but shows how you can change the name and pins to match your project.
//   ^ Library    ^name   ^^ Pin numbers.

/*
 * AccelStepper must have this info before a motor will move properly:
 * a setting for the speed               (setMaxSpeed)
 * a setting for the acceleration        (setAcceleration)
 * a position to move to (i.e. 90 steps) (runToNewPosition)
 */
void setup () {

  linearA.setMaxSpeed(200);      // Change the number in parenthesis Higher=faster. Lower=slower (up to around 32000)
  linearA.setAcceleration(2000);  // Increase this to make the motor slow down a bit before it gets to the specified position. Higher = sharper transition lower = smoother transition
  
}

void loop () {
  linearA.runToNewPosition(100); // Higher=Wider Radius, Lower = Smaller radius. If your stepper motor has 200 steps, then 200 = 360 degrees. 100 steps = 180 degrees, 50 steps = 90, etc.
  linearA.runToNewPosition(-100);// The direction will correspond to your MotorA and MotorB pins. Reverse the two numbers to reverse direction (or change the value in this line and the previous from positive to negative, and vice-versa).
}

// Don't add the delay(); command anywhere, as that will make your motor pause between turns
// There are a half a dozen different ways to write this same exact code. Each will function best in slightly different situations

The library in your original code looks very niche-oriented and probably not the best first step for beginners to arduino and using libraries in general. Learn to use the libraries that have a ton of documentation and support, and that will give you the insight necessary to adapt this project.

Hope that helps!

P.S.
RTFM means 'Read the fucking manual'.
I've got a new one.
LTTTN means 'Learn to talk to Newbies'

Christian1997:
For example what does RTFM mean?

Read The Fabulous Manual - in the case of Arduino programming that is in the Resources/Reference link at the top of the page.

...R

Hi ZRguitar and Robin!

Thanks for your help on this! Im making some progress now thanks to you two!

Hopefully one day i wont be a clueless Dweeb Lol!!!