Splitting code into different classes and files?

Hello,

I'm new to the Arduino Way in particular, and also C in general, but have done a fair bit of other programming before. My usual way is to split my code into various modules, classes and/or files, depending on the language etc.

I don't know if I'm falling foul of the C syntax for these things, or limitations of the Arduino build process, but I'm stumped at how to split my code into different classes without creating actual libraries.

For starters, I assume I have to have a directory to match the pde file name

C:\Projects\ProjectA\ProjectA.pde

Can I create a bunch of something.h and something.cpp files in that directory and include them? Or in a subdir?

Currently I'm fudging it by creating a subfolder "libraries" and putting them in there, again in a subfolder, and telling the Arduino IDE that my actual root is the current project.

**C:\Projects\ProjectA**ProjectA.pde **C:\Projects\ProjectA\libraries\LibA**LibA.h **C:\Projects\ProjectA\libraries\LibA**LibA.cpp **C:\Projects\ProjectA\libraries\LibB**LibB.h **C:\Projects\ProjectA\libraries\LibB**LibB.cpp

And then in my ProjectA.pde I can do this:

#include "LibA.h"
#include "LibB.h"

LibA something();
LibB somethingElse()

(My full real example is available here)

Cylindric:

Can I create a bunch of something.h and something.cpp files in that directory

Yes. That’s the ticket for “normal” Arduino operation. All .h and .cpp files that are in the same directory as the main sketch .pde file will appear as “tabs” in the Arduino IDE, and you won’t have to fake out Arduino. Arduino will compile and link without having to do anything special

This may not be the ideal file organization for sizable projects, but being able to use standard Arduino procedures means that you can port the code to other environments without having to change anything in the Arduino preferences file. (That means that you can ask for help or code reviews from other people who can run your stuff in their “normal” Arduino setups.)

Regards,

Dave

Heh, I thought I’d tried that. Must’ve missed the obvious first. I’m okay with having the .pde file and all the .h and .cpp files in one directory. I might have tried that when I was still confused about the difference between #include and #include “x”.

You can.

But unless its something large or something general purpose that you want to share with the world, don’t bother.

This is ‘little software’. Its much more important to observe good embedded computing practices and have a fast main loop and avoiding any dynamic memory allocation.

A big sketch in Arduino is when it doesn’t actually fit on the screen :wink:

I would not dream of programming like that in the ‘big software’ of my day job, but its ‘horses for courses’ as they say.

You must have a big screen then, even fairly simple "sensor 1 activated" and "move a bit, then turn a bit" code easily fills mine :)

It's nothing big or fancy, but I doubt I'll be able to keep all my logic straight if I have my "sensor activated" behavior code mixed in with my "okay now move according to the current state" code, all mixed in with my "display stuff on the outputs" code.

It's a fairly basic bot, and the main loop spins through pretty quickly, but there's (going to be) a fair bit of stuff that I don't want to be either blocking or "do it now" stuff, so I'm splitting the behavior into

Core.pde - the main loop, basic input and output handling (serial notifications and control for example) Wheels.h - Everything to make the bot move. So things like Wheels.setSpeed(targetSpeed, timeToAccel) Antenna.h - The environment-sensing stuff. For example "Antenna A was touched, but hasn't been released yet"

My main loop then consists of really just

void loop()
{
  Antenna.update();
  Wheels.update();
}

If performance starts to suffer, I'll unroll some of that stuff, but I don't want to make my code look like my wiring until I really have to ;)

If performance starts to suffer, I'll unroll some of that stuff, but I don't want to make my code look like my wiring until I really have to

LOL, what a great statement. :D Almost made me spit my coffee all over my monitor/keyboard.

Thank you for that. Is it open sourced, can I use that statement in the future without attribution?

Lefty

All my Worldly WisdomsTM are available for re-use by sending a notarised fax to my agents in Geneva, with a wire-transfer for the processing fee sent to my pub landlord coded as “clearing Mark’s tab”.

Cylindric:
All my Worldly WisdomsTM are available for re-use by sending a notarised fax to my agents in Geneva, with a wire-transfer for the processing fee sent to my pub landlord coded as “clearing Mark’s tab”.

I hate paperwork. I may just use it and let you sue me. I’m too poor to f**k with. :wink:

Lefty

Curses! My bar-tab will live to see another day... Until my robot is complete. Then we'll see! I've said too much. Back to your business.