How can I set motor moved steps as current position?

Status
Not open for further replies.

jjzai123

Junior Member level 3
Joined
May 12, 2017
Messages
26
Helped
0
Reputation
0
Reaction score
1
Trophy points
1
Activity points
191
I'm trying to code my motor working like this:

1. Let's said the target is 2048
2. I programmed the motor to move 200 steps until it reach 2048

The questions is how to set the current moved steps to current position?

Code:
#include <AccelStepper.h>
    #include <MultiStepper.h>

    #define blue   8
    #define pink   9
    #define yellow 10
    #define orange 11

    AccelStepper stepper(AccelStepper::HALF4WIRE, orange, pink, yellow, blue);

    int target = 2048;
    int current_pos = 0;
    int steps_left;
    
    void setup()
    {
      Serial.begin(9600);
      stepper.setMaxSpeed(1200);
      stepper.setSpeed(200);
      stepper.setAcceleration(110);
 
    }

    void loop()
    {
      target = stepper.targetPosition();
      current_pos = stepper.currentPosition();
      steps_left = stepper.distanceToGo();
      steps_left = target - current_pos;
      if(steps_left != 0)
     {
       //the code here is supposed to be run to 200 steps without blocking and 
         after it done, update the current position till it reach 2048 steps 
       stepper.runToNewPosition(200);
       delay(1000);
   
     }
 }

My questions is like when first the motor moved 200 steps, the current position will set to 200, check if there is still steps left the motor will move again 200 and set the current position to 200+200 something like this.
 

Hi,

you posted a riddle.

we don´t know what hardware you have.
Neither which motor (from the code it looks like a stepper), nor wich motor controller, what interface and what protocol.

So, how can we help you?

Klaus
 

Oops sorry, I have an arduino Uno, a 28byj-48 stepper motor and using ULN2003 to control the stepper motor. The library used is AccelStepper library. Sorry for not mentioning.
 

Hi,

I´m still confused.
the code above is for the arduino uno?

There seems to be a UART ... --> I don´t care about it.

***
There is
* a current_position
* and a traget_position.

To me it seems you never have to say how many steps to go (200).
This is calculated during runtime. Steps_to_go = target_position - current position.
Even the current position is automatically updated.
(At least this is how I see it)

--> just input your target postition.

Klaus
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…