OOP, Stepper motors and, AccelStepper library

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

How many steps per revolution are your motors? That is the defining factor in getting a complete revolution.

I would also encourage you to change your 'get_parameters' function to 'set_parameters'. Getting usually implies you are returning values to the main program. Setting implies setting values within the class.

Thanks, blh64.
I use 28BYJ-48 stepper motor and ULN2003 driver.

based on available online tutorials this stepper motor needs 2048 steps per revolution.
Another observation here is that if I set it to rotate 5 times it works perfectly but when I go to bigger numbers it just doesn't do it. it gets 10 and it rotates 6 times. it gets 5 and it rotates 5 times!

I use 28BYJ-48 stepper motor and ULN2003 driver.

based on available online tutorials this stepper motor needs 2048 steps per revolution.

I've seen many of those with 4096 steps per revolution (and other values)... worth checking

why do you feel it's needed to create a new class that just embeds an AccelStepper instance ? that's not what OOP is about

the compiler when seeing mathematical operations such as 2048*10 will use a signed int to do the math. that means you can't go above 32,767 if you write it this way. 20480 would still fit but careful there - use 2048L to tell the compiler you want to do the maths with long instead of int

This may be the problem:

      stepper_num_x->moveTo(2 * CW0_CCW1 * movingTo);

Why the "2 *"??? Could that be because your steps per revolution is 4096 instead of 2048 and you put in a 'correction factor' instead of changing your understanding?

Since all three value are declared (or implicitly) 'int' you overflow into negative numbers and get the value 0xA000, (a.k.a. -24576). At 4096 steps per revolution that would be 6 turns. just as you were seeing. Did you not notice that the stepper was going in the opposite direction from what you intended?

johnwasser:
This may be the problem:

      stepper_num_x->moveTo(2 * CW0_CCW1 * movingTo);

Why the "2 *"??? Could that be because your steps per revolution is 4096 instead of 2048 and you put in a 'correction factor' instead of changing your understanding?

Since all three value are declared (or implicitly) 'int' you overflow into negative numbers and get the value 0xA000, (a.k.a. -24576). At 4096 steps per revolution that would be 6 turns. just as you were seeing. Did you not notice that the stepper was going in the opposite direction from what you intended?

Thanks. I will try it.

I'll second the thing about set_parameters. In OO programming getters and setters read and write
the object's slots respectively (C calls slots "member variables" or some such). You write the
method names as operations upon the object, from the POV of a client of that class.