I'm new to managing an arduino project with more than a few hundred lines of code (praise the atmega328!). I've done a lot of processing coding and I'm used to split my main code into several .pde files. I tried the same way to add additional .pde files and moved my class definitions there but the compiler can't seem to know where the definitions are. I wonder what is the standard way to manage a few files, one of which is the main and say another one or two contain a class.
Can someone post a psudo code like this or refer me to an example? Thanks a lot!
file1.h
// do xxx here
main.pde
// do #define here
// have include (what?)
//setup and loop
every single library in Arduino is implemented as a .h/.c(.cpp) modules. just look at the libraries included in your Arduino installation. (I am using an older version and there they are located under 'hardware'. But AFAIK in the new versions they are moved around)
Thanks. This sounds all good but not what I was looking for, an example of code template. Now I did it as the library does, and my "byte" is not a defined type in my .cpp. What should I be including then?
Thanks. I included WProgram.h and the compiler is ok now. I guess WProgram.h must have included wiring.h directly or indirectly.
Now I have trouble getting global variables to work. I did something with aid from google search (something that rings a bell as I used C before). The compiler went through with no warnings but I'd like my way looked at by you guys so I'm not doing something obviously stupid. It's hard to get in the C groove after not using it for many years.
Here is what I did:
Main.pde #include <LiquidCrystal.h> #include "gates.h" // This has my gates class declaration
LiquidCrystal lcd(blah) // This needs to be accessed by my gates class, not good for encapsulation but I'll change later
void setup()
{
blah
}
void loop()
{
blah
}
gates.h #include <WProgram.h> // to use byte and all those good data types
#defines and class def
gates.cpp #include "gates.h" #include //Why do I need this again?
extern LiquidCrystal lcd; //Correct?
blah
Question 1:
Every file that needs to access my static variable defined in main.pde needs to have "extern LiquidCrystal lcd;", correct?
Question 2:
Since that won't compile without including , every file that needs my lcd variable needs to include ? Right? Is that redundant?
Question 3:
If a certain class or library is needed in one .cpp file, the class header needs to be there. Just including that class in the main.pde is not enough, right? Thanks a lot. (My processing code was much easier on me :D)
Question 1:
Every file that needs to access my static global variable defined in main.pde needs to have "extern LiquidCrystal lcd;", correct?
Correct. A good place for "extern LiquidCrystal lcd;" is in gates.h.
Question 2:
Since that won't compile without including , every file that needs my lcd variable needs to include ? Right?
Right.
Is that redundant?
Put the include in gates.h
Question 3:
If a certain class or library is needed in one .cpp file, the class header needs to be there. Just including that class in the main.pde is not enough, right?
Right. gates.h is a good place for shared class declarations.
Have you put gates.h and gates.cpp in the same directory as the Sketch (pde)? When first dealing with multi-file projects, that would be a good way to start. The two extra files will be available in the IDE as separate tabs.
Thanks Coding badly and MarkT. I created the gates.h and gates.cpp inside of aduino IDE so they're together with main.pde. I feel a lot more clear now! Next step, add more functions and hope they will still fit on an atmega328!