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
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:
Change the type of stepper from a pointer to a value (loose the *).
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:
Change the type of stepper from a pointer to a value (loose the *).
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.
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?