CustomStepper - A library for stepper motors

ChristopheDV:
I did some tests with the Custom Stepper Library and it worked.
At startup my motor turns 90 degrees to left and then 90 degrees to the right, which is correct according to the code.

What I want, is when I push button1 the motor turns 90 degrees to the left and when I press button2 , 90 degrees to the right.

This is the working code (example)

#include <CustomStepper.h>

CustomStepper stepper(8, 9, 10, 11);
boolean rotatedeg = false;

void setup()
{
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
}

void loop()
{
  if (stepper.isDone() && rotatedeg == false)
  {
    stepper.setDirection(CW);
    stepper.rotateDegrees(360);
    rotatedeg = true;
  }
  stepper.run();
}




Here's my changed code which is not working (with the implementation of the 1 button)



#include <CustomStepper.h>
CustomStepper stepper(8, 9, 10, 11);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;

const int buttonPin = 4;   
int buttonState = 0;

void setup()
{
  pinMode(buttonPin, INPUT);
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
}
 
void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    stepper.setDirection(CW);
    stepper.rotateDegrees(360);
    rotatedeg = true;     
    stepper.run();
  }
}




Is there somebody facing the same problem, or can somebody help me with this?
Thank you

Did you get your code to work, would be interested in having a look if possible.

Otto

Hi
there is already a similar question put to you but I cant find your response.
My question is how can your code be used with a stepper driver which requires just 2 pins - 1) direction & 2) pulse.
Your response would be much appreciated as I have almost zero arduino experience & have just begun on that long road of learning.............
Many thanks
Mike

mikemorr56:
My question is how can your code be used with a stepper driver which requires just 2 pins - 1) direction & 2) pulse.

The simple code in this Demo will work with your stepper driver without needing any library.

...R

Hi,
I am making laser scanning device for which I need precise small coordinate , for this I need to drive stepper motor very slow. Can anybody tell me how to have RPM less than 1 RPM?

jehanzaib:
Hi,
I am making laser scanning device for which I need precise small coordinate , for this I need to drive stepper motor very slow. Can anybody tell me how to have RPM less than 1 RPM?

Did you look at the code I linked to in the previous Reply #22

You can modify it to do 1 revolution per month if you want to.

...R

I have a problem interfacing my motor driver to the motor to arduino. We know that 4 motor pins are to assigned in the arduino, but how if my 4 motor pins are connected to the driver. I can't assign pin numbers to the arduino since it is connected to the driver.

Rbtimbre:
I have a problem interfacing my motor driver to the motor to arduino. We know that 4 motor pins are to assigned in the arduino, but how if my 4 motor pins are connected to the driver. I can't assign pin numbers to the arduino since it is connected to the driver.

You will have to provide a link to the datasheet for your motor driver before you can get an answer.

...R

Can someone please tell me that can the CustomStepper library can be used with the Adafruit motorshield whose link is given below:

Rbtimbre:
I have a problem interfacing my motor driver to the motor to arduino. We know that 4 motor pins are to assigned in the arduino, but how if my 4 motor pins are connected to the driver. I can't assign pin numbers to the arduino since it is connected to the driver.

You have to assign the pins on Arduino what you are using to drive the driver. Its the drivers job then handle the motor.

What I do not understand is that step() calls step(). Why ?

Hi,

I have managed to get the stepper motor to work with a push button, here is my code, if it's of any help.

#include <CustomStepper.h>

CustomStepper stepper(4, 5, 6, 7);

int       buttonPin = 8,          // start button
          buttonState = 0,        // for holding state of button
          stateMachine = 0,       // to transition between different states fo    
          start = 0;              // to start the state machine

void setup() {
  pinMode(buttonPin, INPUT);
  
  stepper.setRPM(15);
  stepper.setSPR(4075.7728395);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  
  if(buttonState == HIGH)
  {
    start = 1;
  }
  
  while(start == 1)
  {
    if (stepper.isDone() && stateMachine == 0)      // state one, 180deg clockwise
    {
      stepper.setDirection(CW);
      stepper.rotateDegrees(180.0);
      stateMachine = 1;
    }
    if (stepper.isDone() && stateMachine == 1)      // state two, 90deg counter clockwise
    {
      stepper.setDirection(CCW);
      stepper.rotateDegrees(90.0);
      stateMachine = 2;
    }
    if (stepper.isDone() && stateMachine == 2)      //state three, 360deg clockwise
    {
      stepper.setDirection(CW);
      stepper.rotateDegrees(360.0);
      stateMachine = 3;
    }
    if (stepper.isDone() && stateMachine == 3)      // state four, 90deg counter clockwise to finish
    {                                               // in the position were we left off.
      stepper.setDirection(CCW);
      stepper.rotateDegrees(90.0);
      stateMachine = 4;
    }
    if(stepper.isDone() && stateMachine == 4)      // and finaly, state five (reset state variables)
    {
      stateMachine = 0;
      start = 0;
    }
  
    stepper.run();
  }
}

This code will turn the stepper motor 180deg clockwise, 90deg counter clockwise, 360deg clockwise and lastly 90deg counter clockwise to finish up were we started from. The board I used is the Uno, this code should work with the Mega 2560.

Let me know if it works for you?

Hello,

Is it possibble to use serial monitor with this library ?

As somebody else before, as soon as I use a serial.println instruction, the skecth stop working.

Regards

Thanks for sharing a lot. Great library for easy start with stepper motors.

Can i move a stepper motor within 90 deg using this library? Fixed 90 degress i mean.

I agree that none of this works as soon as you try using a serial connect. Is there any way around this? I'd like to be able to show the stepper motor angle on the serial monitor and then type in a new angle. So simple but can't be done with this library. Or can it?

Is there bug?
void CustomStepper::run()
{
if (this->timer <= micros() && micros() < this->timer + this->time)
this->step();
}

Did I understand this right? when you call run() it will check that if time is more than timer and step. But it also check that time is not more than timer + step delay. So if run is called slower that delay it will get stuck for ever? Also when micros get reseted (each 70 minutes) this will stuck for ever?

I think
if (this->timer <= micros() || this->timer > micros() + this->time)
this->step();
Will do better job.
First part will do step each time when timer expires. Second part will do tick if micros do overflow and at same time run is too late.
It is not perfect neither because it will do one step too early at each 70minutes when micros overflow. But at least it does not get stuck.

Looks that also this contains bug:

else if (this->mode == CONTINUOUS)
{
this->stepsGone = 0;
this->step();
}

So if motor is continous mode and direction is stopped then it goes to infinite recursion.

Actually looks that this whole logic with CONTINUOUS mode is so rotten that I must ask that did you test it at all?

Fixed step method below. I only check use scenarios what I use. At this point I have found 3 different bug from that library so I assume that this is still broken but at least it works now for me :slight_smile:

void CustomStepper::step()
{

if (this->stepsToGo - this->stepsGone > 0 || this->mode == CONTINUOUS)
{
if (this->direction != STOP)
{
digitalWrite(this->pin1, (this->steps[this->nextStep] & B1000));
digitalWrite(this->pin2, (this->steps[this->nextStep] & B0100));
digitalWrite(this->pin3, (this->steps[this->nextStep] & B0010));
digitalWrite(this->pin4, (this->steps[this->nextStep] & B0001));
this->setTimer();
if (this->direction == CW)
this->nextStep == this->noSteps-1 ? this->nextStep = 0 : this->nextStep++;
else if (this->direction == CCW)
this->nextStep == 0 ? this->nextStep = this->noSteps-1 : this->nextStep--;

if (this->mode != CONTINUOUS)
this->stepsGone++;
}
else
this->disable();
}
else if (this->mode == ROTATIONS)
{
this->stepsGone = 0;
this->stepsToGo = this->spr + this->stepCorrection + 0.5; //Steps per rotation + the step correction + 0.5 to force round
this->stepCorrection = this->spr + this->stepCorrection - this->stepsToGo; //adjust the step correction for the next steps
if (++this->rotationsGone >= this->rotationsToGo)
this->disable();
else
this->step();
}
else if (this->mode == STEPS)
this->disable();
}

Hello

I am currently hacking BYJ48 motors so that they work as Biploar steppers
Will your library work with these too ?

Thanks

Hello,

I have been using this code too, and it works well.
Just one issue when the required rotations or degrees is completed the code seems to return all the i/o to low state and releases the motor allowing it to freewheel, esp if it was going quick to start with.

How do I get the class to hold the motor at zero speed, I did try direction = stop, and speed = 0, neither worked, is it something to do with this continuous mode I just saw further up.

Ta.