Simple class stops working on reset

I’m attempting to make an object-oriented robotic arm. I’m trying to control the stepper through the class Joint. I already had some problems making the sketch compile and execute because I’m pretty new to C++ and I don’t really know a lot about classes, pointers, etc. Eventually, I found that this code works:

#include <Stepper.h>

class Joint
{
  private:
    //int NextStep;
    //Stepper JointStepper;
    //void Step;

  public:
    //int StepDelay;
    //void Update;
    Stepper *TestStepper;
    *Joint(int StepsPerRevolution, int DirPin, int StepPin)
    {
      TestStepper = new Stepper(StepsPerRevolution, DirPin, StepPin);
    }
};

Joint a(200, 3, 4);

void setup() 
{ 
  
}

void loop() {
  a.TestStepper->step(1);
  delay(2);
}

It works fine, the only problem is that when I reset (from the onboard button) the Arduino, it crashes and I can’t get it to start again not even by unplugging and plugging it back. To make it work I have to first unplug it, upload a new code (down here) a few times until I can get it back to work and then upload the code with the class.

#include <Stepper.h>
Stepper stepper(200, 3, 4);

void setup(){
  
}

void loop()
{
  stepper.step(1);
  delay(2);
}

I think I’ve figured out it has something to do with the class because the other code works perfectly even after multiples reset.

I’m using an Arduino Due and an A4988 Stepper Driver. I’m programming the Arduino through the Native USB and powering the motor with a 9v battery.

If each Joint has one Stepper, you want to use inheritance…

#include <Stepper.h>

class Joint : public Stepper{
  public:
    Joint(int StepsPerRevolution, int DirPin, int StepPin) : Stepper(StepsPerRevolution,DirPin, StepPin) {}
  private:
};

Joint a(200, 3, 4);

void setup()
{

}

void loop() {
  a.step(1);
  delay(2);
}

AchilleM:
I'm using an Arduino Due and an A4988 Stepper Driver. I'm programming the Arduino through the Native USB and powering the motor with a 9v battery.

What sort of battery?
If its a smoke detector type 9V battery then you won't get much energy out of it.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Including how you are powering your project.
Thanks.. Tom... :slight_smile:

BulldogLowell:
If each Joint has one Stepper, you want to use inheritance…

#include <Stepper.h>

class Joint : public Stepper{
  public:
    Joint(int StepsPerRevolution, int DirPin, int StepPin) : Stepper(StepsPerRevolution,DirPin, StepPin) {}
  private:
};

Joint a(200, 3, 4);

void setup()
{

}

void loop() {
  a.step(1);
  delay(2);
}

This code solves the reset problem. Thank you very much.
In the future tough I will have to add a class with multiple steppers, how should I handle that?

AchilleM:
This code solves the reset problem. Thank you very much.
In the future tough I will have to add a class with multiple steppers, how should I handle that?

there are a couple of different ways to do it… would you want to have a fixed number of steppers per Joint or variable (i.e one, two or even three)?

here is an example using exactly three:

#include <Stepper.h>

Stepper knuckles[] = {  // distal, middle, proximal
  {200, 3, 4},
  {200, 5, 6},
  {200, 7, 8},
};

class Finger {
  public:
    Finger(Stepper* servoMotors){
      Stepper* knuckle = servoMotors;
      distal = knuckle;
      middle = ++knuckle;
      proximal = ++knuckle;
    }
    void openDistal(int steps) {
      distal->step(steps);
    }
    void closeDistal(int steps) {
      openDistal(0 - steps);
    }
  private:
    const Stepper* distal;
    const Stepper* middle;
    const Stepper* proximal;
};

Finger indexFinger(knuckles);

void setup() {
  
}

void loop() {
  indexFinger.openDistal(20);
  delay(1000);
  indexFinger.closeDistal(20);
  delay(1000);
}