Creating an stepper object within a class

Is the finger a stepper (Inheritance)
or
Has the finger a stepper (composition)

The finger might have even more steppers one day, a touch sensor, a temperature sensor, a bending sensor...
So I believe the stepper is just one part of the finger.

I would keep the composition.

#include <AccelStepper.h>

#define MAX_SPEED 500

// Class for each finger
class Finger {
  protected:
    // Pins setup
    const int stepPin;  // do you really need them? at least make them const
    const int dirPin;

  public:
    // Constructor for each finger object
    Finger(int stepPin, int dirPin) :
      stepPin{stepPin},
      dirPin{dirPin},
      stepper(stepPin, dirPin)
    {
      stepper.setMaxSpeed(MAX_SPEED);  // are you sure this is a good idea in the constructor?
    }
    AccelStepper stepper;
};

Finger Thumb(2, 3);

void setup() {
}

void loop() {
  //Example of how I want to use the stepper finger class
  Thumb.stepper.moveTo(90);
}
1 Like