So while there are many drivers for various motor shields I noticed that there wasn't one for just the L298 chip itself or the various single chip modules such as this https://www.sparkfun.com/products/9670
I know it's not really.necessary to have a library for this usage but I wrote it so that my students can get started on the meaty bits of robotics nice and quickly after learning the basics of the arduino IDE.
Let me.know what you think. Feel free to submit issues if you see them.
pinMode() assumes that the hardware is ready. When your constructor is run, that is NOT a valid assumption. Your class needs a begin() method where the hardware diddling happens.
Perhaps I'm missing something, but BRAKE and RELEASE seem to do the same thing.
The class should have three arrays of pin numbers:
private:
int aPins[2];
int bPins[2];
int dirPins[2];
In the implementation of the constructor, set the 0th element of each array, or the 1th element of each array, to aPin, bPin, and dirPin, respectively, based on the value in motorNum. camelCase is better than flatcase for names.
I think I have it working I would really appreciate it if you cast an eye over my new branch with these changes.
When I create an instance of your class, like so:
DCMotor motorOne(114, 2, 3, 9);
Where is your constructor going to store the pin numbers?
[ rant ]I REALLY hate variable names that have IN in the name being set to OUTPUT. It does not make sense. That the pin is connected to an INput to the motor driver is NOT a reason to use IN in the name. [ /rant ]
Well as it is a driver for a dual H bridge they should.only define two motors.
Well, as its your library, you should check for sane input before using it.
Out of interest though how do I make a list/array "expandanle" so that it will grow to whatever is needed?
You shouldn't. You could use malloc(), realloc(), etc. to make dynamic arrays. But, you really shouldn't. There is no chance that the number of motors that one instance of your class, being based on hardware, can handle will change while the Arduino is running.
PaulS:
Well, as its your library, you should check for sane input before using it.
OK I thought so,
I am struggling with this though. I have included an if statement:
if (num == 1 || num == 2) {
//do stuff
}
else {
//bad input
}
however I am sure sure what to put in my else block.
Googling to how to throw an error just gets me lots of people asking about the errors they have!
Thanks for all the help you have given me, really appreciate it. I can see on this forum you spend a lot of time helping people like me and it looks quite frustrating ins some instances!
At least point me in a relevant direction. I can't find anything to have it work.
You are trying to get the preprocessor to do something that it is not designed to do. It can not tell what value is stored in variables in the code. That approach will never work.
All that you can do is use any valid data that is supplied, and quietly ignore any bad data.