Is there a way to bring a stepper to same start position at startup

3,573 views
Skip to first unread message

Aaron Park

unread,
Mar 5, 2014, 3:27:53 PM3/5/14
to accels...@googlegroups.com
I am running an arduino uno with two steppers moving a pan tilt like contraption. My issues is when I power it up the steppers seem to pick arbitrary start points and I have to reset the arduino until it gets into position. The current code Im using is from the accelstepper example page I will list it here:
#include <AccelStepper.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define FULLSTEP 4
#define HALFSTEP 8
// motor pins
#define motorPin1  4     // Blue   - 28BYJ48 pin 1
#define motorPin2  5     // Pink   - 28BYJ48 pin 2
#define motorPin3  6     // Yellow - 28BYJ48 pin 3
#define motorPin4  7     // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
                       
#define motorPin5  8     // Blue   - 28BYJ48 pin 1
#define motorPin6  9     // Pink   - 28BYJ48 pin 2
#define motorPin7  10    // Yellow - 28BYJ48 pin 3
#define motorPin8  11    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
/*-----( Declare objects )-----*/
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
/*-----( Declare Variables )-----*/
//none
void setup()   /****** SETUP: RUNS ONCE ******/
{
 
  stepper1.setMaxSpeed(500.0);
  stepper1.setAcceleration(1000.0);
  stepper1.setSpeed(1000);
  stepper1.moveTo(200);  // 1 revolution
 
 
  stepper2.setMaxSpeed(500.0);
  stepper2.setAcceleration(1000.0);
  stepper2.setSpeed(1000);
  stepper2.moveTo(-250);  // 1 revolution
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  //Change direction at the limits
  if (stepper1.distanceToGo() == 0)
   
    stepper1.moveTo(rand() % 300) ;
   stepper1.setMaxSpeed ( (rand() % 1000) +2);
   stepper1.setAcceleration ((rand() % 1000) +2);
    if (stepper2.distanceToGo() == 0)
   
    stepper2.moveTo(rand() % 1000) ;
    stepper2.setMaxSpeed ( (rand() % 1000) +2);
    stepper2.setAcceleration ((rand() % 1000) +2);
 
  stepper1.run();
  stepper2.run();
}
 
 
 Also does the set values in setup get overridden by the loop random commands? Ill admit Im trial and erroring my way through arduino but I figured if anywhere could answer it may be here. Thanks

Sandy Noble

unread,
Mar 5, 2014, 6:57:27 PM3/5/14
to accels...@googlegroups.com
The start point is arbitrary, yes - there is no internal indexing of rotation, so one step is much the same as the other.

The usual approach is to have some mechanical trigger on the shaft (or on the thing you are moving) and when you turn the machine on, wind the motors back until the trigger hits a switch or a sensor somewhere. That way you know where it is and can work reliably from there. Limit switches.

The easiest way to do it is to turn the motor by hand until it's in the right start position, then turn it on! But that gets old pretty quickly.

Your other question - yes, everything in loop() gets called after the stuff in setup(), so it overrides it. Replaces it, really.

sn


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Sandy Noble

Mike McCauley

unread,
Mar 5, 2014, 6:59:17 PM3/5/14
to accels...@googlegroups.com
Hello,

probably the main problem with your code is you are changing the max speed,
acceleration and target position every time through the loop, making the
changes even more frequent than each step of the steppers.
Yes.

Cheers.


> Ill admit Im trial and erroring my way through arduino but I
> figured if anywhere could answer it may be here. Thanks
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Aaron Park

unread,
Mar 5, 2014, 7:27:24 PM3/5/14
to accels...@googlegroups.com
thank you for the responses. What Im doing is driving a mirror that reflects a stage light so I need the random positions, should I skip the accel and speed aspect?

Mike McCauley

unread,
Mar 5, 2014, 7:31:01 PM3/5/14
to accels...@googlegroups.com
Hi,

I would suggest that you cange the speed and target position only after each
motor has reached its target.

See examples/Random.pde in the AccelStepper distribution

Cheers.

Aaron Park

unread,
Mar 8, 2014, 7:22:56 PM3/8/14
to accels...@googlegroups.com
ok so how would that change my code?
 

demolis...@gmail.com

unread,
Mar 21, 2014, 6:13:26 PM3/21/14
to accels...@googlegroups.com
The title of the post is, "is there a way to bring a stepper to the same start position at startup"

I didn't really look at info in the thread, but to answer the question from the title of the post, I'd suggest looking into the Arduino EEPROM. it allows you to write a value from 0 to 255 into a 'mini hard drive.' the information can be retrieved later after the arduino has been turned off and then back on at a later date. there are 516 'addresses' which you can store information in. check the arduino.cc website under 'refference' or 'examlpes.' my two cents, good luck

Sandy Noble

unread,
Mar 23, 2014, 6:30:12 AM3/23/14
to accels...@googlegroups.com

Always worth reading the thread before adding to it.

Worth noting that eeprom can be 'worn out' in only a couple of thousand write cycles, and so it would be quite easy to irreparably ruin a new arduino following your advice.

Also difficult to see how using eeprom or a different data store to record position would help solve the problem - could you elaborate?

Sn

On 21 Mar 2014 22:13, <demolis...@gmail.com> wrote:
The title of the post is, "is there a way to bring a stepper to the same start position at startup"

I didn't really look at info in the thread, but to answer the question from the title of the post, I'd suggest looking into the Arduino EEPROM. it allows you to write a value from 0 to 255 into a 'mini hard drive.' the information can be retrieved later after the arduino has been turned off and then back on at a later date. there are 516 'addresses' which you can store information in. check the arduino.cc website under 'refference' or 'examlpes.' my two cents, good luck

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Paul Sullivan

unread,
Apr 18, 2014, 5:55:32 PM4/18/14
to accels...@googlegroups.com
You need something to set the start point and read from that. Typically this would be something tied to the shaft like an absolute encoder or you could ham together a simple mechanical trigger.  
Reply all
Reply to author
Forward
0 new messages