Pages: [1]   Go Down
Author Topic: trouble with "Arduino.h"  (Read 1577 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Jim
are you sure you do not want to use pinMode ?
Best regards
Jantje
Logged

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 -

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

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

Seattle, WA
Offline Offline
God Member
*****
Karma: 8
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


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

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

Seattle, WA
Offline Offline
God Member
*****
Karma: 8
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


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

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

Pages: [1]   Go Up
Jump to: