Pages: [1]   Go Down
Author Topic: Creating libraries in Arduino sketch folder  (Read 581 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm creating a few classes for a project, which seems so specific that I don't want to keep in the libraries folder. They're mostly working, however, if I want to call any Arduino functions or consts, it will throw errors "is not declared in this scope".

sketch:

Code:
#include "MyClass.h"
void setup(){
}
void loop(){
}
MyClass.h

Code:
class MyClass{
    public:
        MyClass(int inp);
        int myFun();
};
MyClass.cpp

Code:
#include "MyClass.h"
#include <WProgram.h>

MyClass::MyClass(int inp){
    pinMode(13,HIGH);

}
error:

MyClass.cpp: 'HIGH','pinMode' is not declared in this scope.

It wouldn't happen if I put the libraries in libraries folder though. Wondering if there's a way to include arduino functions into sketch folder libraries?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

First upgrade your IDE to 1.0 or above, if done

replace #include <WProgram.h> with
#include <Arduino.h>
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks @pYro_65, just figured it out after getting stuck somewhere else for a couple of hours smiley-grin
I saw someone using the following in his library:
Code:
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
Which may be a even better solution.
But changing the library name is pretty bad for backwards compatibility though
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the contrary, this would detect which Arduino version your library is being compiled in, and uses the appropriate Arduino include file. The name of the arduino library changed ( before version 1.0 it was WProgram.h and since 1.0 it is Arduino.h).

If you're expecting to share your library with others and believe/expect/confirm that your library works with previous Arduino versions (e.g. 0023 ), then put the if/then construct there.  If you're not intending to share it with others and/or you're only using 1.0.x and later, it doesn't really matter--you could just use the Arduino.h...

HTH,
John
Logged

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

Code:
MyClass::MyClass(int inp){
    pinMode(13,HIGH);

}
When will your constructor be called? When will init() be called to set up the hardware?

If you can not answer these questions definitively, you should not be doing hardware-related stuff in your constructor.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting...
The example is just for showing my problem regarding undeclared functions, so it doesn't have real purpose. However I never thought about the potential problem between constructor and hardware. Thank you for the extra knowledge! smiley So what problems can there be?
Logged

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

Quote
So what problems can there be?
Your constructor gets called when the object is instantiated. If that happens at global scope, it will happen before init() is called. You set the pin to be an OUTPUT pin. Then, init() gets called, and it KNOWS that pins are to be INPUT, so it changes the pin to an INPUT pin.

Then, your class tries to turn the LED on and off, and it won't be very bright.
Logged

Pages: [1]   Go Up
Jump to: