I've got a giant spaghetti mess of code that I need to break out into more manageable chunks. I'm a C hacker from way back so I don't really get the whole concept of scoping.
I have a file called giantmess.ino . I want to break this up into smaller chunks, small1.[cpp|ino], small2.... and have a set of global variables as well as a set of local variables, but some of the chunks will depend on globals in other chunks.
Is there a reference to this somewhere on how I should structure the files, both in terms of variables and such and the file hierachy itself?
I can do this in C with extern and #include and so on but since I already have a mess I don't want to create another mess.
OK, I should have been more specific. I have functions. I have 1,200 lines of code, broken into functions, in one giant file. It's not really maintainable in any practical way. (Yes it all compiles and even does what I want, mostly.)
I want to break out the functions that handle the keypad into a file, the functions that handle the screen into another, the functions that handle the RS485 comms into another, and so on.
Also, almost every program can be simplified into what I call the Five Program Steps:
Initialization -- everything that's done before the end user knows the program starts (e.g., with an OS, reading register files, opening DB connections, checking printer ports, etc.) Here, most of that goes into the setup() function.
Input -- whatever has to be done to provide the data for the program...reading sensors, reading an SD card, reading a serial port.
Process -- Since virtually every program takes data in one form and converts it to another, it processes the data.
Display -- something is done with the processed data: turn a valve on, show something on an LCD display, send a serial message. Usually, the Input, Process, and Display Steps are bounded by loop().
Termination -- usually, the Initialization Step in reverse...the program cleans up after itself, closing DB's, releasing whatever resources haven't already been released. With the Arduino, most programs don't plan for a Termination Step.
The Five Program Steps are not a bad way to organize your program. A big comment header:
followed by the functions that perform whatever it is that needs to be done. You can use Sideways Refinement to add granularity to the steps. If the code warrants it, you could create files for the steps. To me, the important thing is to know where to go to look for a particular method or variable. A little organization can make debugging a lot easier.