Go Down

Topic: 28BYJ-48 stepper motor control Sketch (Read 75 times) previous topic - next topic

Ataur_Rahman

Nov 10, 2019, 11:03 pm Last Edit: Nov 11, 2019, 12:16 am by Ataur_Rahman
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:

Code: [Select]


#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:



Code: [Select]

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!

JCA34F

Answer is in part of code you didn't post.

sayHovis

#2
Nov 11, 2019, 07:32 am Last Edit: Nov 11, 2019, 10:47 am by sayHovis
I think you need to use move() not moveTo().

moveTo() does exactly what it says- it moves to a position- so if you're at +2000 and tell it to move to -2000, it will go back 2000 to 0 then go another 2000 back to -2000.

move() otoh is a relative movement, so from +2000 it will move back by 2000 to 0 .

AccelStepper reference says:

Quote
moveTo(): Set the target position.
move(): Set the target position relative to the current position

Go Up