0

I have tried hard to make a stepper motor work with a class, but I can't get a property returning its value in a method. I get 0 (zero) instead of 26 (expected) from _stepPin.

I have tried this->_stepPin with no success.

Here is the code. See the comments inside.

 class MKSStepperMotors {

   public:
    int _dirPin;
    int _stepPin;
    int _enablePin;

    MKSStepperMotors(int dirPin, int stepPin, int enablePin) {

      int _dirPin = dirPin;
      int _stepPin = stepPin;
      int _enablePin = enablePin;

      //Habilitamos el motor
      pinMode(_enablePin, OUTPUT);
      digitalWrite(_enablePin, LOW);

      //Cambiamos la direccion y aumentamos la velocidad
      pinMode(_dirPin, OUTPUT);
      digitalWrite(_dirPin, LOW);

      // Le indicamos el pin de señal
      pinMode(_stepPin, OUTPUT);

    }

    void move(int steps, int stepDelay)
    {

      for (int x = 0; x < steps; x++) {
        Serial.println(_stepPin); //  <-- _stepPin is zero and not 26 as it should
        digitalWrite(_stepPin, HIGH); <-- _stepPin is zero and not 26 as it should
        delayMicroseconds(stepDelay);
        digitalWrite(_stepPin, LOW);  <-- _stepPin is zero and not 26 as it should
        delayMicroseconds(stepDelay);
      }
    }
};


 MKSStepperMotors pedo(28, 26, 24);

 void setup() {
  Serial.begin(250000);
}

 void loop() {

      pedo.move(3200,100);

}

2 Answers 2

3

When you put the type there, you are creating new variables. SO in your constructor:

 MKSStepperMotors(int dirPin, int stepPin, int enablePin) {

      int _dirPin = dirPin;
      int _stepPin = stepPin;
      int _enablePin = enablePin;

You start by creating three new variables with the same names as the member variables defined in the class. Throughout the rest of the constructor those get used instead of the ones that are member variables.

You gotta lose the "int" there.

You also have another bug in there. The constructor may run before the init function if one of these objects gets declared at global scope. Things like digitalWite and pinMode aren't going to work yet in that case. A constructor should just have code to initialize variables, it should never touch hardware. The stuff that uses hardware should be in another member function (usually called begin or init) that you can call from setup.

1
  • Did Work !!!!! And I accepted the "init' method suggestion. THANKS
    – FedeKrum
    Commented Nov 12, 2017 at 23:38
2

You have redeclared the class member fields in your constructor:

class MKSStepperMotors {

   public:
    int _dirPin;
    int _stepPin;
    int _enablePin;

    MKSStepperMotors(int dirPin, int stepPin, int enablePin) {

      int _dirPin = dirPin;
      int _stepPin = stepPin;
      int _enablePin = enablePin;
      ...
    }

The int _dirPin = dirPin; declares a local variable with the same name as your class member _dirPin and shadows it, hence your 3 class members never get initialized!

You should change your constructor as follows:

    MKSStepperMotors(int dirPin, int stepPin, int enablePin) {

      _dirPin = dirPin;
      _stepPin = stepPin;
      _enablePin = enablePin;
      ...
    }

Or better, use standard C++ constructor initialization:

    MKSStepperMotors(int dirPin, int stepPin, int enablePin)
    :_dirPin(dirPin),_stepPin(stepPin),_enablePin(enablePin) {
      ...
    }

In this latter example, you don't need to initialize your class members in the constructor body as they are already initialized before the body gets executed (this is idiomatic C++ usage).

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.