I am going to use object-oriented programming in Arduino to rotate two stepper motors.
here are my codes.
// Include the AccelStepper Library
#include <AccelStepper.h>
class STEPER_Motor
{
int motorPins[4];
int max_speed;
int accelrate;
int initSpeed;
int movingTo;
int CW0_CCW1;
int FULLSTEP;
AccelStepper *stepper_num_x = NULL;
public:
// constructor
STEPER_Motor(const int mot_Pins[4], int f_FULLSTEP)
{
motorPins[0] = mot_Pins[0];
motorPins[1] = mot_Pins[2];
motorPins[2] = mot_Pins[1];
motorPins[3] = mot_Pins[3];
FULLSTEP = f_FULLSTEP;
stepper_num_x = new AccelStepper(FULLSTEP, motorPins[0], motorPins[1], motorPins[2], motorPins[3]);
}
// method
void get_parameters(int m_max_speed, int a_accelrate, int i_initSpeed, int c_CW0_CCW1)
{
max_speed = m_max_speed;
accelrate = a_accelrate;
initSpeed = i_initSpeed;
// movingTo = m_movingTo;
CW0_CCW1 = c_CW0_CCW1;
}
// method
void Run_it(int movingTo)
{
stepper_num_x->setMaxSpeed(max_speed);
stepper_num_x->setAcceleration(accelrate);
stepper_num_x->setSpeed(initSpeed);
stepper_num_x->moveTo(CW0_CCW1*movingTo);
stepper_num_x->run();
}
// method
void run_to_posi(int movingTo)
{
stepper_num_x->setMaxSpeed(max_speed);
stepper_num_x->setAcceleration(accelrate);
stepper_num_x->setSpeed(initSpeed);
stepper_num_x->moveTo(2*CW0_CCW1*movingTo);
stepper_num_x->runToPosition();
}
};
// introduce the stepper motors
const int pins1[] = {8,9,10,11};
STEPER_Motor Steppr_1(pins1, 8);
const int pins2[] = {4,5,6,7};
STEPER_Motor Steppr_2(pins2, 8);
void program_run_to_posi(){
Steppr_1.run_to_posi(2048*10); // here i am going to rotate it 10 times but it just rotates 6 times?!
Steppr_2.run_to_posi(2048*10);
};
void initite_all_parameters()
{
Steppr_1.get_parameters(1000, 1000, 1000, 1);
Steppr_2.get_parameters(1000, 1000, 1000, 1);
}
void setup()
{
initite_all_parameters();
program_run_to_posi();
}
void loop()
{
// Steppr_1.Run_it(1000);
// Steppr_2.Run_it(1000);
}
I am going to rotate two stepper motors 10 rotations.
I see it rotates around 6 times!
Could you please let me know what is the problem with these codes?
Thanks