Constructing member obj passing pins

Hi there!

I know this is a basic C++ question but after many different tries I don’t know what to try any more.

Here is the problem:

I created a class that instances an ArduinoDCMotor (using an Arduino Motor Shield, but this is irrelevant for the code).

I created a class that is called ThreadLegs, which has two member vars called ArduinoDCMotor _lf and _rt, representing the two legs of my toy.

But the silly thing is, I am having a hard time constructing those member vars from the ThreadLegs constructor that takes the pins.

Please note that if I create the ArduinoDCMotors first and then pass them to the appropriate constructor of ThreadLegs, it works. (I want to avoid this because this is hurting encapsulation principles).

Ok, onto the code:

The arduino sketch:

#include <ThreadLegs.h>

#define M_RT_SPEED_PIN 3
#define M_RT_DIR_PIN 12
#define M_RT_BRAKE_PIN 9
#define M_RT_I_PIN A0

#define M_LF_SPEED_PIN 11
#define M_LF_DIR_PIN 13
#define M_LF_BRAKE_PIN 8
#define M_LF_I_PIN A1

//Build the legs providing the pins directly
ThreadLegs legs(M_LF_DIR_PIN, M_LF_SPEED_PIN, M_LF_BRAKE_PIN, M_LF_I_PIN,
		M_RT_DIR_PIN, M_RT_SPEED_PIN, M_RT_BRAKE_PIN, M_RT_I_PIN);
/*
//Or build the legs from pre existing motors, currently working
//default constructor creates a motor with pins set to the default for A motor in the Arduino Motor Shield, which is wired to the right motor
ArduinoDCMotor rtLeg; 
ArduinoDCMotor lfLeg(M_LF_DIR_PIN, M_LF_SPEED_PIN, M_LF_BRAKE_PIN, M_LF_I_PIN);
ThreadLegs legs(lfLeg, rtLeg);
*/

void setup() {
  delay(200);

  //Open Serial communication
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
  delay(200);
  Serial.flush();
  delay(200);

  Serial.println(F("Successfuly began serial communication."));
  Serial.println();  
  delay(200);

  Serial.println();
  Serial.println(F("   - Initiating ThreadLegs class tests - "));
  Serial.println();
  Serial.println();
  Serial.println(F("   --- Configure pins of the legs ---"));
  Serial.println();
  Serial.println(F("legs.setupPinMode();"));
  
  legs.setupPinMode();
  
  //Verify  
  Serial.println();
  Serial.println();
  Serial.println(F("   --- Print pins for verification ---"));
  Serial.println();
  legs.printPins(); 
}

void loop() {
  while(1) {}
}

I am including the *.cpp and *.h files in the thread, but this is the constructor that I can’t implement correctly. (I commented out my initial try).

ThreadLegs::ThreadLegs(
  byte lfMotor_dir_pin, byte lfMotor_speed_pin, byte lfMotor_brake_pin, byte lfMotor_i_pin,
  byte rtMotor_dir_pin, byte rtMotor_speed_pin, byte rtMotor_brake_pin, byte rtMotor_i_pin) {

  //tried this but it didn't work
  //  ArduinoDCMotor _lf(lfMotor_dir_pin, lfMotor_speed_pin, lfMotor_brake_pin, lfMotor_i_pin);
  //  ArduinoDCMotor _rt(rtMotor_dir_pin, rtMotor_speed_pin, rtMotor_brake_pin, rtMotor_i_pin);

  //this is also not working
  ArduinoDCMotor _lftemp(lfMotor_dir_pin, lfMotor_speed_pin, lfMotor_brake_pin, lfMotor_i_pin);
  ArduinoDCMotor _rttemp(rtMotor_dir_pin, rtMotor_speed_pin, rtMotor_brake_pin, rtMotor_i_pin);

  _lf = _lftemp;
  _rt = _rttemp;
}

And these are the two constructors of the ArduinoDCMotor:

ArduinoDCMotor::ArduinoDCMotor():
  _speed_pin(3), 
  _dir_pin(12), 
  _brake_pin(9), 
  _i_pin(A0), 
  _dir(HIGH), 
  _speed(0) {}


ArduinoDCMotor::ArduinoDCMotor(byte dir_pin, byte speed_pin, byte brake_pin, byte i_pin):
  _dir(HIGH), 
  _speed(0),
  _dir_pin(dir_pin),
  _speed_pin(speed_pin),
  _brake_pin(brake_pin),
  _i_pin(i_pin) {}

And the ArduinoDCMotor member vars:

 private:
  byte _speed_pin,
    _dir_pin,
    _brake_pin,
    _i_pin;

  byte _dir,
     _speed;

and just to be clear, this is the declaration of these member vars in the ThreadLegs class:

 private:
  ArduinoDCMotor _lf, _rt; //The two motors the leg is made of

Many thanks for the help!!!

ArduinoDCMotor.cpp (3.33 KB)

ThreadLegs.h (9.7 KB)

ThreadLegs.cpp (11.7 KB)

ArduinoDCMotor.h (2.42 KB)

If you want to use anything but the default constructor for member variables, you have to use initializer list in your class constructor, i.e.

ThreadLegs::ThreadLegs(
  byte lfMotor_dir_pin, byte lfMotor_speed_pin, byte lfMotor_brake_pin, byte lfMotor_i_pin,
  byte rtMotor_dir_pin, byte rtMotor_speed_pin, byte rtMotor_brake_pin, byte rtMotor_i_pin)
  :_lf(lfMotor_dir_pin, lfMotor_speed_pin, lfMotor_brake_pin, lfMotor_i_pin)
  ,_rt(rtMotor_dir_pin, rtMotor_speed_pin, rtMotor_brake_pin, rtMotor_i_pin)
{
}

Thank you so much!