Go Down

Topic: trouble with "Arduino.h" (Read 1 time) previous topic - next topic

Jim2012

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;

   setMode(pinHigh, OUTPUT);
   setMode(pinLow, OUTPUT);
}

Jantje

Jim
are you sure you do not want to use pinMode ?
Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Jim2012

I should just put noob in my name haha thanks. i did the same thing with another function, writeAnalog haha. Just got my Arduino last night

PaulS

Quote
Just got my Arduino last night

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.

maniacbug


Quote
Just got my Arduino last night

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.


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.

extent

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.

maniacbug


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:
Code: [Select]

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.


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.

PaulS

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

Go Up