Skip to main content
3 of 5
edited tags
user avatar
user avatar

Arduino Classes , can't get property value

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

I have tried this->_stepPin with no success.

Here is the code. See the comments in it.

 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);

}