1

I have an Arduino connected to a stepper motor with an Easy Driver as follows: enter image description here

I am controlling the position of the stepper motor using the Serial. This is my code:

#include <AccelStepper.h>
AccelStepper stepper(1, 9, 8);
void setup()
{ 
 Serial.begin(9600);
 stepper.setMaxSpeed(10000);
 stepper.setAcceleration(5000);
}
void loop()
{
 char c;
 if(Serial.available()) 
 { 
 int steps = Serial.parseInt();
 Serial.print(steps);
 stepper.moveTo(steps);
 }
 stepper.run();
}

The code works well but I'm facing a problem, when I set a high position value, it starts turning in the opposite direction, like if there was a limit on the steps. Does anyone know why it starts turning to the opposite direction?

I have tried to solve this problem by using:

if (stepper1.distanceToGo() == 0) 
 { 
 stepper1.setCurrentPosition(0);
 } 

so I can set lower values to the serial. But the problem goes back again when the value goes up to the power of 6 (like 1000000).

Any idea?

Thanks!

rebatoma
5593 gold badges12 silver badges22 bronze badges
asked Apr 11, 2016 at 16:59
1
  • 1
    Per a comment from the developer of AccelStepper, the line AccelStepper stepper(1, 9, 8); should be written as AccelStepper stepper(AccelStepper::DRIVER, 9, 8); for clarity Commented Apr 14, 2016 at 20:03

1 Answer 1

0

Ints on vanilla Arduinos are 16 bits, and signed, which means they hold values from -2^15 to +2^15-1, so of course you cannot assign 10^6 to one of them. Use unsigned long (4 bytes), or, better yet, specify the length explicitly by using uint32_t.

answered Apr 14, 2016 at 2:31
2
  • could you post me the reference of this? Commented Apr 14, 2016 at 8:02
  • 1
    This has always been the case for 16-bit processors. Plus, it's in the adruino documentation. Plus, you can verify it for yourself by writing a one-line program. Commented Apr 14, 2016 at 15:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.