Oriented object probleme with Arduino

Hi

I need help about a problem. I buiding a librairy to define a step motor. this librairy will manage the acceleration, deceleration, speed and more. This librairy is in C++.

So it’s working very fine when I use only one motor but If I define 2 different motor it’s working not properly. So the first motor will run normaly but the seconde will start and went the first motor stop, the seconde one stop also. So it’s two diffrent object and it’s suppose to work independently.

My objective is to drvie many stepmotor at different speed independently.

in my google folder I put a picture and a video.

this is the link:https://drive.google.com/folderview?id=0BxqwDkxB95CrOGNDRjNCemdNSms&usp=sharing

and this is my code

Arduino Code:

#include <StepMotor.h>

#include "Arduino.h"

int OutputPin[4] = {2, 3, 4, 5};

StepMotor MotA;
StepMotor MotB;

void setup() {

  Serial.begin(9600); // set serial speed

  int i;
  for (i = 0; i < sizeof(OutputPin); i = i + 1) {
    pinMode(OutputPin[i], OUTPUT); 
    digitalWrite(OutputPin[i], HIGH); 
    }

  MotA.setPul(4); // Pin pulse
  MotA.setDir(5); // Pin direction
  MotA.setAC(150); // Acceleration
  MotA.setDC(150); // deceleration
  MotA.setVM(300); // Max speed
  MotA.setPulseToGo(6000); // number of pulse to go

  MotB.setPul(2); // Pin pulse
  MotB.setDir(3); // Pin direction
  MotB.setAC(4000); // Acceleration
  MotB.setDC(4000); // deceleration
  MotB.setVM(2000); // Max speed
  MotB.setPulseToGo(5000); // number of pulse to go

}

void loop() {
  
  MotA.control();
  //MotB.control(); 
  
}

StepMotor.h file:

#ifndef STEPMOTOR_H
#define STEPMOTOR_H

#include "Arduino.h"


class StepMotor
{
    public:
        StepMotor();
        virtual ~StepMotor();
        void setAC(double AC);
        double getAC();
        void setDC(double DC);
        double getDC();
        void setVM(double VM);
        double getVM();
        void setPul(int Pul);
        int getPul();
        void setDir(int Dir);
        int getDir();
        void control();
        long StartTime();
        void setPulseToGo(int PulseToGo);
        void setCase(int Case);
        int getCurrentPulse();



    protected:
    private:

        // Variable d'initialisation
        double _AC;
        double _DC;
        double _VM;
        int _Pul; // Pulse
        int _Dir; // Direction
        double _PulsTime; // time in mirco sec

        // Variable de fonctionnement
        float _Timepulse;
        long _StartTime;
        int _PulseToGo;
        int _CurrentPulse; // Direction
        int _P1;
        int _P2;
        int _P3;
        double _T1;
        double _T2;
        double _T3;
        float _VitInst;
        float _VitPres;
        int _CASE;
        int _start;
        bool _stop;
        bool _setpath;

};

#endif // STEPMOTOR_H

and the StepMotor.cpp

#include "StepMotor.h"
#include <math.h>


StepMotor::StepMotor()
{

    _PulsTime = 100; // duré du pulse en micro sec
    _CASE = 1;
    _start = 0;

}

StepMotor::~StepMotor()
{
    //dtor
}

void StepMotor::setAC(double AC)
{
    _AC = AC;
}

double StepMotor::getAC()
{
    return _AC;
}

void StepMotor::setDC(double DC)
{
    _DC = DC;
}

double StepMotor::getDC()
{
    return _DC;
}

void StepMotor::setVM(double VM)
{
    _VM = VM;
}

double StepMotor::getVM()
{
    return _VM;
}

void StepMotor::setPul(int Pul)
{
    _Pul = Pul;
}

int StepMotor::getPul()
{
    return _Pul;
}

void StepMotor::setDir(int Dir)
{
    _Dir = Dir;
}

int StepMotor::getDir()
{
    return _Dir;
}

void StepMotor::setPulseToGo(int PulseToGo)
{
    _PulseToGo = PulseToGo;
}

void StepMotor::setCase(int Case)
{
    _CASE  = Case;
}
int StepMotor::getCurrentPulse()
{
    return _CurrentPulse;
}

long StepMotor::StartTime()
{
    return _StartTime;
}

void StepMotor::control()
{

switch (_CASE) {
    // Phase d'initialisation
    case 1:
        _StartTime = micros();
        _T1 = _VM / _AC;
        _P1 = round(_T1*_VM/2);
        _T3 = _VM / _DC;
        _P3 = round(_T3*_VM/2);
        _T2 = (_PulseToGo - _P1 - _P3)/_VM;
        _P2 = _T2 * _VM;
        _CurrentPulse = 0;
        _Timepulse = 0;
        _VitInst = 0;
        _VitPres = 0;
        _CASE = 2;

        Serial.println("Inialisation terminé");
        Serial.println("_P1: "+String(_P1));
        Serial.println("_P2: "+String(_P2));
        Serial.println("_P3: "+String(_P3));
        Serial.println("_PulseToGo: "+String(_PulseToGo));
        Serial.println("TempsTotal: "+String(_T1+_T2+_T3));

        // Code
    break;
    // Phase d'accélération
    case 2:
        if(_CurrentPulse <= _P1)
        {
            _CurrentPulse = _CurrentPulse + 1;
            _VitInst = sqrt(_VitPres*_VitPres+2*_AC*1);
            _Timepulse = _Timepulse + (2/(_VitPres+_VitInst));
            _VitPres = _VitInst;
            _CASE = 6;


        }
        else if(_CurrentPulse > _P1 and _CurrentPulse <=_P2 + _P1)
        {
            _CurrentPulse = _CurrentPulse + 1;
            _Timepulse =_Timepulse + (1/_VM);
            _CASE = 6;


        }
        else if( _CurrentPulse > _P2 + _P1 and _CurrentPulse < _PulseToGo)
        {
            _CurrentPulse = _CurrentPulse + 1;
            _VitInst = sqrt(_VitPres*_VitPres-2*_DC*1);
            _Timepulse = _Timepulse + (2/(_VitPres+_VitInst));
            _VitPres = _VitInst;
            _CASE = 6;


        }
        else if(_CurrentPulse >= _PulseToGo)
        {

             Serial.println("Routine terminé");
             Serial.println("Délais total: " + String(micros() - _StartTime));
             Serial.println("Current pulse: " + String(_CurrentPulse));

        }

    break;

    // Phase Top speed
    case 3:

        // Code
    break;

    // Phase décélération
    case 4:
        // Code
    break;

    // Phase reset
    case 5:

    break;
    case 6:
        if ((micros() - _StartTime) > (_Timepulse*1000000))
        {
            //Serial.println("Pulse: "+ String(_CurrentPulse) +"Micro - _StartTime: "+ String(micros()- _StartTime)+">= Time: "+String(_Timepulse*1000000));
            digitalWrite(_Pul, LOW);
        }
        if (micros() - (_StartTime + (_Timepulse*1000000)) > _PulsTime)
        {
            digitalWrite(_Pul, HIGH);
            _CASE = 2;
            //Serial.println("close: ");
        }
    break;
    // Phase reset
    default:
        // Code
    break;
}

}
  else if( _CurrentPulse > _P2 + _P1 and _CurrentPulse < _PulseToGo)

Did you maybe mean to write:

  else if( _CurrentPulse > _P2 + _P1 && _CurrentPulse < _PulseToGo)

See the C++ Standard, paragraph 2.5:

In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling. The set of alternative tokens is defined in Table 1.

Amongst other things, && and and are equivalent.

AccelStepper ?

...R