Speed control of stepper with easy driver and potentiometer

I want to use this sketch with my Uno and easy driver and a nema 17. It works but the motor just barely moves, but it is working.

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0; // number of steps the motor has taken

void setup() {
// nothing to do inside the setup
}

void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

I have my wiring like this, (easy driver - direction to pin 8) (easy driver - Stepping to pin 9) (easy driver - ground to ground)

Potentiometer to 0. (typical setup)

What changes do I need to make so it works as it should.

This sketch works perfectly,

[code][code]#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);

int pos = 3600;

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

void loop()
{
 if (stepper.distanceToGo() == 0)
 {
   delay(500);
   pos = -pos;
   stepper.moveTo(pos);
 }
 stepper.run();
}

[/code][/code]

But I want to be able to set the speed of the stepper with a potentiometer, so I guess what I really want to do is to marry these two sketches, but I can't seem to do it.

first read how to use code tags item7
then go back and edit you post

http://forum.arduino.cc/index.php/topic,148850.0.html

you have this in your initialization

const int stepsPerRevolution = 200;

then this in void loop ()

  // step 1/100 of a revolution:
  myStepper.step(stepsPerRevolution / 100);]

200/100 is not the same as 1/100

Hi Dave-in-nj,

I tried fixing my posting, but I could only use the code tags once I guess. It wouldn't let me add both codes.

Any way, thanks for your response. I'm not sure as to what you want me to do when it comes to fixing my code.

I think I'm going to post almost the same post but with some changes, under programming questions.

Thanks..........Antonio

You say one of the sketches works properly. If so, a good starting point is to make some small changes to the working program to get it to perform whatever way you want. Keep a copy of the working version and test each change as you make it.

The AccelStepper library is considerably more capable than the Stepper library.

If you are using an Easydriver it is hardly necessary to use any library. See this Simple Stepper Program. Just get your potentiometer to vary the value in the variable millisbetweenSteps

...R
Stepper Motor Basics

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How are you powering your stepper and what is its part/model number and specs?

Tom..... :slight_smile:

AntonioLopez:
Hi Dave-in-nj,

I tried fixing my posting, but I could only use the code tags once I guess. It wouldn't let me add both codes.

Any way, thanks for your response. I'm not sure as to what you want me to do when it comes to fixing my code.

I think I'm going to post almost the same post but with some changes, under programming questions.

Thanks..........Antonio

open your first post.
on the bottom left of your screen is edit. it is hidden, but there.
that will open your post
then hit preview, also on the bottom.
now you will have 2 windows that show your post.

*you may have to hit the edit, then the modify next to the edit button.

only the bottom can be edited, the top shows what is might look like.
highlight your code
on the menu bar for editing, the every left icon is a slash between greater than and less than.
hit that icon and it will add the code tags.
since this thread is not 3 pages long, you can test it here and get used to the way the pages work.
not a huge deal now, but some people get annoyed at long programs being dumped in the regular post.
others hate links, others hate drop box types.... don't worry, you will never please everybody !
but we are having fun and it seems like everything is a learning experience.

Here's what I've done.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 63, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 50);
  }
}

This line was int motorSpeed = map(sensorReading, 0, 1023, 0, 100);

I changed it to int motorSpeed = map(sensorReading, 0, 63, 0, 100);

This line was myStepper.step(stepsPerRevolution / 100);
I changed it to myStepper.step(stepsPerRevolution / 50);

I'm now able to change my speed to where the motor is moving a lot faster than before. So I'm happy with that.

Now, I want to add part of this sketch

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);

int pos = 150000; //amount of distance to travel

void setup()
{  
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(3000);//how fast to accelerate
}

void loop()
{
  if (stepper.distanceToGo() == 0)
  {
    delay(500);
    pos = -pos;
    stepper.moveTo(pos);
  }
  stepper.run();
}

So when the motor moves to this position int pos = 150000; //amount of distance to travel it slows down, stops and heads back in the opposite direction. Which is what the second sketch does. So, I guess I want the best of both sketches, the ability to use the potiometer to set my speed and the ability to set the distance the stepper moves.

I hope this makes sense. Let me know if you have any questions.

Thanks.......Antonio

You can't use the Stepper and AccelStepper libraries in the same program. Because the AccelStepper library is much more capable I suggest your first step should be to modify your first program so it uses the AccelStepper library.

Then it may even be obvious how to incorporate the extra code for the bahaviour you want.

...R