Pages: [1]   Go Down
Author Topic: Dynamic array in class/constructor  (Read 351 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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:
Code:
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! smiley
« Last Edit: August 17, 2011, 05:40:59 am by asuryan » Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply!

I do not really get a error message.

I instantiate the class as follows (with 2 channels):
Code:
multiStepper myMultiStepper(11, 12, 10, 2);

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

Inside the constructor (simplyfied):
Code:
_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:

Code:
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:
Code:
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. smiley-sad

If it is not really clear what I mean I will make a simplified example of my problem! smiley
« Last Edit: August 17, 2011, 06:21:09 am by asuryan » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46009
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int _channels[1];
Quote
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.

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: