Code gets complex and hard to understand, any C books to recomend?


I start to get the grip on coding for Arduino & hardware, I learned to filter sensor-data, I learned to make the code so I easily can add or remove more hardware, I make functions for error and unpredicted values and all kind of things.

My new problem is the code gets complex and too hard to understand. Often I think I done it all right, but it don't work as expected and it's hard to track down why.

So I guess it's time to learn more about software architecture and maintenance. I found this two books, but maybe some of you have a better book to recommend? (or tutorial or whatever?).

Thanks for all help!

My new problem is the code gets complex and too hard to understand. Often I think I done it all right, but it don't work as expected and it's hard to track down why.

Do you split the code into sensibly named functions and use sensibly named variables ?

Auto formatting the code helps to see program structure and compiling every time some significan portion of code allows problems to be uncovered early

Functions can be tested on their own by passing known values to them that should produce certain outputs. Functions that have not yet been written in full can emulate data input by setting variables explicitly to simulate reading sensors etc

Don't forget that Arduino programs can be split into multiple tabs within the IDE. Grouping functions with a similar purpose or those that interact with particular sensors on IDE tabs with sensible names can help understand what does what.

When a function has been written and tested it can largely be used in the same way as the built in Arduino functions that are known to work.

Functions that may be used in more than one program can be put into libraries and used again with confidence.

Classes can be created so that objects to carry out specific tasks can be created in the knowledge that they work and, of course, more than one object of a type can be created if the class is written correctly.

Version control, even if it is only done by giving program versins a number as well as a name is important. It allows you to get back to a version known to work and to review changes to find where a problem was introduced.

Hi yes, in my latest project i have 400 lines of code, I follow the rule that if you can't tell what the function is doing in two sentences, it's to complex and need to be broken down to several functions.

I use time in finding good variable names and function descriptions. I'm very strict in formatting the code to increase readability.

I write in quasi-code first, so I can focus on the big lines, not if the code is compiling.

Yes this is the first project i made where most of the functions are made in a test-script, here I use arrays to "feed" the functions with what is commonly sensor input. When they work I past it back in the main program.

I can try to split the code more into classes, maybe on class for reading data, one for filtering and organize data, one for decide what action to do based on the input and so on, that's a good idea.

Yes I know about the 'extern' keyword... but I thought it would do my code even more difficult to read splitting it over several pages?

Versions control, here I have a job to do!

Object oriented. That gets you to complex.

Finite state machine. That dovetails with object oriented.


There is a flawed assumption in the software entropy theory which the author essentially regurgitates without question. I would not buy that book.


That one looks promising. I might buy that one.

The source code is available here...

Make lots of notes as you go along, use lots of comments, use functions to clean up the body of the sketch.
One thing is certain, 6 months down the line, you will have forgotten why you did this that and the other. Making changes while you are working on a biggish sketch is fairly easy, but go back to an old sketch, and it all looks like Double Dutch. Folk go on about indenting etc. - absolutely essential - put a comment at the start and end of functions
How many times do you run an Arduino and wonder what the sketch is on it? You can’t easily download the the sketch and reverse engineer it.
I’ve got in the habit now of of going to the new tab menu (very top right in the IDE screen), select “RENAME”, copy with CTRL+C (no contextual COPY menu), then pasting the name of the sketch into the SETUP part of the sketch so that you can at least print off the name and possibly where to look. Having got used to working with PLC’s, where you have the luxury of downloading code as well as uploading - not being able to pull code from that unknown chip makes you wish you had numbered it up and kept a note of the sketch.
Version control can get out of hand especially if you have save on compile or upload set - use save as so you have a working version to go back to - it’s a sort of backup and restore. Make sure you know why the last revision is different to the one before.

Thanks for advise. Yes Serial.print the version and name in setup sound smart, then you know what's kode actually in the Arduino board.

Thanks Coding badly, then I go for that book.

What about Mocup-tools when planning the code? Just to ruffly draw the big lines?

What I use...
(I get my from Staples when they are on sale. The ones I use are only sold in-store.)'s+Black+Waterproof+Fountain+Pen+Ink+Bulletproof