Hi all,
So, I am trying to get simple control of 28BYJ-48 stepper motor using the ULN2003 driver. And I'm using this sketch:
#include <AccelStepper.h>
// Motor pin definitions
#define motorPin1 2 // IN1 on the ULN2003 driver 1
#define motorPin2 3 // IN2 on the ULN2003 driver 1
#define motorPin3 4 // IN3 on the ULN2003 driver 1
#define motorPin4 5 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1
(AccelStepper::FULL4WIRE, motorPin1, motorPin3, motorPin2, motorPin4, true);
int pos = 2048; //positions for the motor to go to
void setup()
{
Serial.begin (9600);
stepper1.setMaxSpeed (1000); //work out these values for your stepper via trial
stepper1.setSpeed (500); //Speed work well for the 28BYJ-48 stepper
stepper1.setAcceleration (500); //Value gives quick acceleration to the set speed
stepper1.setCurrentPosition (0); //position of motor when the sketch starts.
stepper1.moveTo (pos); //The first position for the motor to move to
}
void loop()
{
stepper1.run(); // Call run() as often as possible
if (stepper1.distanceToGo() == 0) // If position reached...
{
stepper1.moveTo(-stepper1.currentPosition()); // Go back to the opposite position
}
Serial.println (stepper1.distanceToGo());
}
and when I run it:
1- Runs from "0" position to "2048" (full rotation forward)
2- once it reaches "2048" it calls the loop to go backward. But it starts counting backward (rotating) from "2048" (-2Xfull rotation backward), but I needed it to run one rotation backward not 2 rotations.
So what I'm trying to do is simply get a full rotation forward then full rotation backward.
everything in the setup looks clear and simple but I don't understand what is happening in this section in the loop exactly:
void loop()
{
stepper1.run(); // Call run() as often as possible
if (stepper1.distanceToGo() == 0) // If position reached...
{
stepper1.moveTo(-stepper1.currentPosition()); // Go back to the opposite position
}
Serial.println (stepper1.distanceToGo());
}
I want to understand the functionality of this code (the loop part) so I can move further to do what I want.
I know it might look very basic question to you guys but I need to get my head around this since I'm new to all this.
Appreciated!