After searching for a quite long while over the internet, I have no choice but to try asking someone if they can explain me this apparently strange situation.
I'm doing some tests using some servo motors, trying to move them almost together using millis()
and Servo
object.
It is, of course, working either for single Servos and multiple servos.
The case, more detailed, is about this:
- Move one, two or more servo motors together.
- Force them to take ALMOST the same time to accomplish any action.
- Have a dynamic number of servos.
About the first two points I had no problems to solve them, I was easily able to make a function and then make a library to accomplish such a job.
However, since I'm NOT that used to C++ and I was wondering if it actually was possible to create an array of Servo objects, like you can actually do, on arduino, with strings:
String stringArray[] = {"string1", "string2", "and so on, I love it."};
So, after wondering a while, I've tried this:
Servo servObject;
Servo servObject_2;
Servo servObject_3;
Servo servos[] = {servObject, servObject_2, servObject_3};
And you know what's cool? my function is actually working correctly by forcing the amount of elements of the servos array.
What I mean by that is that if I force my function to know that it is going to have 3 elements into the array it will, of course, work; However, for some reason, if I use sizeof(servos);
the value returned, surprisingly, is 2
....
Moreover, if I only push two servo objects instead of three into the servo array, its sizeof
returns 6
.
By googling, I've found out a couple of discussions about the fact that, for some reasons, in some cases, sizeof returns an incorrect value (if I'm not wrong it was the correct value -1), but in my case it apparently is not following any logic at all.
The board I'm using is an Arduino nano w/ ATMEGA 328.
Any idea of why it is not returning the correct value? Am I actually going totally wrong because I shouldn't make an array of servo objects?
ps: I didn't post the whole code because it would've been useless, since the sizeof is the first function called in the loop()
Regards,
briosheje
sizeof
construct available for any array object. Therefore, any array (ex. int elements[] = {2,3,1,3,9};) outputs its correct value (in the previous case 5) and I'm getting the value directly from the Serial port. In any case, tonight, I will upload the whole code, I'm not and home and can't really paste it from my Nexus 5 :P – briosheje May 7 '14 at 7:11sizeof
at the start of my loop, it still outputs the same value as in the function where I'm using it. Also, is there any alternative to check if an array element exists? If it's possible, then I could make my ownsizeof
prototype. – briosheje May 7 '14 at 13:12#define countof(a) (sizeof(a)/sizeof(a[0]))
to find the # of elements in an array, but since it depends onsizeof()
it's won't be much help to you. – JRobert May 7 '14 at 19:30