Im writing a library that will simulate a continuous servo with two pwm pins and a regular motor. However, when i compile my code i get get this:
Users/Jim/Documents/Arduino/libraries/CustomServo/CustomServo.cpp: In constructor 'CustomServo::CustomServo(int, int)':
/Users/Jim/Documents/Arduino/libraries/CustomServo/CustomServo.cpp:12: error: 'setMode' was not declared in this scope
this is my constructor:
CustomServo::CustomServo(int a, int b)
{
pinHigh = a;
pinLow = b;
Then, might I respectfully suggest that library development NOT be your first project?
If you are to persist in this endeavor, look at some other libraries. There are some things you need to consider.
The IDE creates a main() function, and calls init(), setup() and loop(). Somewhere in that process, it also calls any constructors that the sketch calls for.
Does your constructor get called before init() or after it? If you don't know, then you should not be writing code that relies on it having been called.
If your constructor is called first, and the mode of some pins is set, and then init() is called, and the mode all set to INPUT (the default), your code will not behave as expected.
You'll notice that a lot of libraries provide a begin() method, like Serial.begin(), EthernetClient.begin(), EthernetServer.begin(), LiquidCrystal.begin(), etc. The reason is that the constructor simply records what pins you want to use. The begin() method is where the calls to pinMode() go.
Since the begin() method is typically called in setup(), though it can be called in loop(), the begin() method(s) call(s) to pinMode() are guaranteed to be made AFTER the init() function has run, and therefore the effects are persistent.
Then, might I respectfully suggest that library development NOT be your first project?
If you are to persist in this endeavor, look at some other libraries.
Excellent advice. Only start on libraries once you've got everything you need working nicely in a sketch, and you've got a clear understanding of the system.
PaulS:
The IDE creates a main() function, and calls init(), setup() and loop(). Somewhere in that process, it also calls any constructors that the sketch calls for.
Does your constructor get called before init() or after it?
In fact, constructors for objects defined statically (the only rational way to work in Arduino) are called BEFORE main() and ergo BEFORE init(), during the static initialization phase.
So yes, unless you want to compete with init(), steer clear of making Arduino system calls in a constructor.
Is there a time when a non-static objects constructor is called before init? I noticed that the LiquidCrystal library is doing exactly this, calling pinMode for rs/rw/enable pins in it's constructor.
extent:
Is there a time when a non-static objects constructor is called before init?
Non-static objects are constructed, well, whenever the object is constructed!
Consider:
void setup(void)
{
int i = 5;
while (i--)
{
MyObject my_instance(i);
my_instance.do_something();
}
}
In this case, my_instance is constructed 5 times, each time through the while loop, and destroyed at the end of it.
extent:
I noticed that the LiquidCrystal library is doing exactly this, calling pinMode for rs/rw/enable pins in it's constructor.
pinMode() may be safe, it may not. I haven't studied init() well enough to know either way. The fat that init() does lots of system setup leads me to steer clear of it precisely because I don't want to sort through every nuance of init() to determine what's safe or what's not.
The fat that init() does lots of system setup leads me to steer clear of it precisely because I don't want to sort through every nuance of init() to determine what's safe or what's not.
Exactly. Plus, you don't want to have to do that with every new version of the IDE.