Protocol for avoiding pointers

That would avoid having the data stored twice in RAM for the duration of the code. The class’s lifetime is the whole code so you could instantiate directly the motors as you had done for the transient motorData array and then just call a begin() or init() method in setup. (As you did in the new code).

PS: you could use an initializer list in your constructor it’s « cleaner » and you could then move what’s constant to constant type.

MotorClass(int _stepPin, int _dirPin, int _direction, int _rpm)
  : stepPin(_stepPin), dirPin(_dirPin), direction(_direction), rpm(_rpm), speed(10000), run(false) 
  {}

(An initializer list is cleaner because it directly initializes members as the object is being constructed, instead of default-constructing them first and then assigning new values inside the constructor body. This avoids redundant operations, ensures const and reference members can be initialized, improves clarity by showing initialization in one place, and can slightly improve performance since no temporary default state is created before assignment.)

1 Like