Dynamic array in class/constructor

Hi!

I would like to write a library for my stepper driver board using
a bunch of 74HC595 and L293.

I would like to manage (port number, speed, steps to go etc.) all motors inside
a few arrays but I cant get it work to size the array dynamical inside the constructor
of the class.

I would like to initiallize the class giving the channel count (eg. 4 * 4Bit-Pattern
connceted to 2*74HC595) etc. It should be as modular as possible to conncect
as many motors I like using shift registers etc.

multiStepper::multiStepper(int latchPin, int clockPin, int dataPin, int channels)
{
	//74HC595 Init
	_latchPin = latchPin;
	_clockPin = clockPin;
	_dataPin = dataPin;
	_speed = 50;
	_seqCounter = 0;
	
	_channelCount = channels;
	for(int i=0;i<=_channelCount;i++){
		_channels[i] = 0;
	}
	
	//Sequence Init
	_stepSequence[0] = B1000;
	_stepSequence[1] = B0010;
	_stepSequence[2] = B0100;
	_stepSequence[3] = B0001;
};

I init the array _channels inside the header as follows:

class multiStepper
{
	private:
		int _channels[1];
		byte _stepSequence[4];
		int _latchPin;
		int _clockPin;
		int _dataPin;
		int _speed;
		long _previousCycle;
		int _seqCounter;
		int _channelCount;
	public:
		multiStepper(int latchPin, int clockPin, int dataPin, int channels);
		void singleStep(int channel);
		void begin();
		void sendSequence(byte bitPattern);
		void stepAll();
		void refresh();
};

I init the array with one member because an empty array seems to crash the MCU.

Thanks for help! :slight_smile:

asuryan:
I would like to manage (port number, speed, steps to go etc.) all motors inside
a few arrays but I cant get it work to size the array dynamical inside the constructor
of the class.

In what way does your code not work? What error message(s) do you get?

Thanks for your reply!

I do not really get a error message.

I instantiate the class as follows (with 2 channels):

multiStepper myMultiStepper(11, 12, 10, 2);

Inside the header (simplyfied):

class multiStepper
{
	private:
		int _channels[1];
	public:
		multiStepper(int latchPin, int clockPin, int dataPin, int channels);
};

Inside the constructor (simplyfied):

_channelCount = channels;
for(int i=0;i<=_channelCount;i++){
	_channels[i] = 0;
}
  1. When I Init the array without a size: the MCU crashes -> my test sequence does not
    light up (I have LEDs on the shiftregister to see the output)
  2. When I init the array with 1 member in the header I get an unexpected number on
    position 2 (1) in the _channels array.

I tested the content of the array with this function:

void multiStepper::getChannelCount()
{
	for(int i=0;i<=_channelCount-1;i++){
		Serial.print("Channel ");
		Serial.print(i);
		Serial.print(": ");
		Serial.print(_channels[i]);
		Serial.println();
	}
}

and get this result:

Channel 0: 0
Channel 1: 520

I inti the array with 0 so there must be 0 in both positions in the array... but there
is a 520(??) ... So it seems that the dynamic filling of the array does not work. :frowning:

If it is not really clear what I mean I will make a simplified example of my problem! :slight_smile:

		int _channels[1];

So it seems that the dynamic filling of the array does not work.

Dynamically filling an array and dynamic sizing of an array are two completely different issues. The dynamic filling is working, up to the end of your static array.

If you really need dynamic sizing of the array, you will need to learn about malloc(), and all of its pitfalls.

It should be as modular as possible to conncect
as many motors I like using shift registers etc.

Really, there is going to be a practical limit to the number of motors you can power and manage. Set some reasonable upper limit (10?) and statically size the array.