-2

I created a class to manage commands thru serial port and i am using a stepper motor but when i am trying to use step function is not working. I declare an object in arduino file and i used pointer of stepper object to wotk inside class. testing it, setSpeed method works fine but when i try to use step method, i got segmentation fault on compiling action. Stepper motor library is working ok, i already did some test and motor works fine but when i tried to use it in a class with pointers is not working.

main file

    #include "ClassTest.h"

    ClassTest test;

    Stepper myStepper1 = Stepper(200, 8, 9, 10, 11);

    void setup() {
        test.SetupMotor(&myStepper1);  
    }
    void loop() {
        test.MoveMotor('Motor1',200);  
    }

ClassTest.h

#include "Arduino.h"
#include "Stepper.h"

class ClassTest
{
  public:
    ClassTest();
    void SetupMotor(Stepper* step);
    void MoveMotor(String ,int );
  private:
    Stepper* _myStepper1;
};

ClassTest.cpp

void ClassTest::SetupMotor(Stepper* step)
{
    _myStepper1=step;
        _myStepper1->setSpeed(200);
}

void ClassTest::MoveMotor(String motor,int stepCount)
{
        // i am getting an issue on compiling time about segmentation fault
    _myStepper1->step(200);

}

I tried many things but not sure why is not working yet, any help??? Thanks!!

Matiasg1982
  • 399
  • 3
  • 5

1 Answers1

0

I'm unable to reproduce your error. The following code compiles in Arduino IDE:

#include "Arduino.h"
#include "Stepper.h"

class ClassTest
{
  public:
    ClassTest() = default;
    void SetupMotor(Stepper* step);
    void MoveMotor(String, int );
  private:
    Stepper* _myStepper1;
};

void ClassTest::SetupMotor(Stepper* step)
{
  _myStepper1 = step;
  _myStepper1->setSpeed(200);
}

void ClassTest::MoveMotor(String motor, int stepCount)
{
  _myStepper1->step(200);

}

ClassTest test;

Stepper myStepper1 = Stepper(200, 8, 9, 10, 11);

void setup() {
  test.SetupMotor(&myStepper1);
}
void loop() {
  test.MoveMotor("Motor1", 200);
}

Skrino
  • 520
  • 2
  • 12
  • correct, issue was on Serial port, i was writing and reading from it and i was getting a segmentation fault after a few tests using this code, i guess overflow of serial port, not sure but it is working now. Thanks! – Matiasg1982 Aug 05 '19 at 10:03