composition with the stepper library

The purpose of this my class is to add methods to the stepper class (without modifying the class).
PROBLEM: when calling step(10); it doesn't 'step'
The think there is a problem with the pointer (i'm terrible with pointers). Would anyone know what i do wrong?
Thanks for your time!
Header:

#ifndef STEPPERMOD
#define STEPPERMOD
#include <Stepper.h>
class StepperMod
{
private:
  int currentSteps = 0;
  Stepper *stepper;
  int angleRatio = 0; //amount of steps per degree
public:
  StepperMod(int stepsPerRevolution,int pin1,int pin2, int pin3, int pin4);
  void step(int steps);
  void setSpeed(int speed);
  int getSteps();
  void reset();
  void calibrate();
  int getAngle();
  void angle(int angle);
};
#endif

source:

#include "Arduino.h"
#include "StepperMod.h"
#include <Stepper.h>

StepperMod::StepperMod(int stepsPerRevolution,int pin1,int pin2, int pin3, int pin4)
{
  Stepper ref(stepsPerRevolution,pin1,pin2,pin3,pin4);
  stepper = &ref;
  Serial.begin(9600);
  Serial.println("initialised stepper");
}
void StepperMod::setSpeed(int speed)
{
  stepper -> setSpeed(speed);
  Serial.println("Change speed stepper");
}
void StepperMod::step(int steps)
{
  currentSteps +=steps;
  stepper -> step(steps);
}

& some other less important stuff

the problem was that it should be: Stepper * ref = new Stepper()

bieboebap:
the problem was that it should be: Stepper * ref = new Stepper()

Don't use the new keyword if you can avoid it. Dynamic memory allocation in general is a bad idea in embedded systems such as the arduino. Especially, because you are not cleaning up the memory. For every call to new there MUST be a call to delete, otherwise you will have created a memory leak.

Refactor it in the following way:

  1. Change the type of stepper from a pointer to a value (loose the *).
  2. Change your constructor to use an initializer list
// Don't do this.
StepperMod::StepperMod(int stepsPerRevolution,int pin1,int pin2, int pin3, int pin4)
{
  Stepper ref(stepsPerRevolution,pin1,pin2,pin3,pin4);
  stepper = &ref;
}

// But this instead
StepperMod::StepperMod(int stepsPerRevolution,int pin1,int pin2, int pin3, int pin4)
   : stepper(stepsPerRevolution, pin1, pin2, pin3, pin4)
{
     Serial.begin(9600);
     // Other code
}

Thanks, didn't know that, i use Dynamic memory allocation a lot in my application, is there something there a better alternative than Denamic memory allocation?

LightuC:
Don't use the new keyword if you can avoid it. Dynamic memory allocation in general is a bad idea in embedded systems such as the arduino. Especially, because you are not cleaning up the memory. For every call to new there MUST be a call to delete, otherwise you will have created a memory leak.

Refactor it in the following way:

  1. Change the type of stepper from a pointer to a value (loose the *).
  2. Change your constructor to use an initializer list
// Don't do this.

StepperMod::StepperMod(int stepsPerRevolution,int pin1,int pin2, int pin3, int pin4)
{
  Stepper ref(stepsPerRevolution,pin1,pin2,pin3,pin4);
  stepper = &ref;
}

// But this instead
StepperMod::StepperMod(int stepsPerRevolution,int pin1,int pin2, int pin3, int pin4)
  : stepper(stepsPerRevolution, pin1, pin2, pin3, pin4)
{
    Serial.begin(9600);
    // Other code
}

Meh, I think the prevailing dogma against using dynamic allocation is a little overdone. Although, I do admit to doing all my projects with processors having more generous resource allocation than an Arduino Uno.

Regarding OP's question, I'd consider using inheritance from the Stepper class.

The standard Stepper library is a pretty poor starting point. Have you looked at the much more capable AccelStepper library?

...R

Robin2:
The standard Stepper library is a pretty poor starting point. Have you looked at the much more capable AccelStepper library?

Good input, I haven't done much with steppers.

So, I'd start with inheritance from AccelStepper. This new class after all IS (a new type of) Stepper. Makes more sense than it HAS as Stepper.

gfvalvo:
Regarding OP's question, I'd consider using inheritance from the Stepper class.

Thanks, changed it to inheritance. Works great now.
I do however us a lot of dynamic memory allocation (database), rather large: 1800 integers. Could it be harmful?

What processor? How many bytes per integer on that machine? How much RAM does it have?

BTW, if you use int8_t, int16_t, int32_t, uint32_t, etc, you'll always know how many bytes a given variable takes.

gfvalvo:
Good input, I haven't done much with steppers.

So, I'd start with inheritance from AccelStepper.

I wonder if the AccelStepper library is sufficiently competent not to need the OP's proposed extension ?

...R