Optimal way to code complex projects on an an Arduino?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
SlowSteve
 
Posts: 24
Joined: Mon Mar 03, 2014 11:06 am

Optimal way to code complex projects on an an Arduino?

Post by SlowSteve »

Hello all,

Just wanted to ask a quick question about the "best" way to code up complex projects.

I'm in the middle of making a project with 2x steppers, 1x brushless motor with ESC, 2x theromocouples, LCD screen, RTC, LCD screen, buttons and a few other gadgets - all from Adafruit of course.

The main system is two inter-linked PID loops linking theromocouples, steppers and the motor. The LCD and buttons control a menu to set different modes of operation, the RTC supports the time keping functions and timer/alarm functions and there are some additional layers of algorithms over the top to adjust the main PIDS, based on inputs from the timer, menu etc etc.

I will be using the Mega to do this, although is memory gets very tight I might have to go to the Due instead, although I'd prefer to avoid this. I'm also aware that a complex system like this is going to take a lot of tuning, and I want to try and keep spaghetti code to a minimum.

If I was developing this for a server situation, I would keep the code very loosely coupled and cross-cut everything I could. That makes for simle, easy to develop, but it will also blow the size of the sketches out to multiple megabytes, and need a lot of clock speed - neither of which is practical on a microcontroller


So..... my problem is that, while I know how to code in general, and I'm coming up to speed with Arduino coding, I just don't know how to do about coding for Microcontollers. I'm not really looking for code examples here, I'm looking for advice for how to put this together - the patterns I should be using? Do I fit everything into a single big loop and deal with the spaghetti? Do I break out seperate loops for each discrete section and then glue them together? Do I develop top down - building out the main PIDs, and then add in functions as I go down? Or do I start bottom up - get the sensors working, then the controllers, then the parametrics, then the PIDs,?

ANY advice that anyone can provide would be hugely appreciated - again - I'm not especially looking for code examples, I'm more interested in getting an idea of the mindset I should be working with? Patterns, workflows etc etc

Thanks

Steve

User avatar
Barry914
 
Posts: 448
Joined: Sun Dec 08, 2013 1:26 pm

Re: Optimal way to code complex projects on an an Arduino?

Post by Barry914 »

I'm a newbie to Arduino development, and I haven't done a lot of original code, but what I have done i have treated like any other project; top down, writing high level functions first, then filling in the low level stuff. (you may prefer working from the bottom up.) The major differences between doing this with the IDE rather than a more conventional development environment is not creating libraries of separately compiled objects that can be linked, instead of doing a full compile each time I make a change. That's because the IDE is a bit opaque to me, and because the compile/load time for even a large program is short. As for spaghetti code, it's easy to write incomprehensible code using any programming style. Don't ask me how I know this.

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Optimal way to code complex projects on an an Arduino?

Post by adafruit_support_bill »

Since threading is not an option on this platform. I find that a state machine approach works best for complex projects. The Arduino PID library fits well into that model.

Creating separate C++ classes to encapsulate the state machines for the various subsystems helps to minimize the spaghetti. You will have to wrestle with the IDE a bit if you try to break your project into separate files. You don't have much control over compile order or dependencies. A better approach is to build your classes as libraries and include them.

User avatar
Renate
 
Posts: 291
Joined: Tue Nov 13, 2012 3:21 pm

Re: Optimal way to code complex projects on an an Arduino?

Post by Renate »

My $0.02 on this:

At some point you might want to break free from "sketches" and the IDE.
You might just want to use the AVR compiler and makefiles.
That way you have full control over everything.
You can break stuff into files however you like.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”