Programming Tools - Getting to know the structure and nuances of Arduino

LONG ago, when dbase was new, I used to print out my dbase code and use colored pens to marks programming "Nests" so that it was easier to study the code and debug it. For exmple, I would color all my IF/THEN nests one color, Do Whiles another etc etc.

I "think" Arduino code is based on C programming language? (not sure)

I'm wondering if there are any such tools available for the Arduino code that help me identify loops and such visually to help insure no careless mistakes have been made?

I'm finding it difficult to find a good beginners book on the Arduino language also. Anyone know of any good beginners books that explain all the small but critical details?

Asking for help in programming reminds me of the adage...."Give a man a fish and you feed him today, teach a man to fish and you feed him for a lifetime" I get tired of waiting for the "Fish" and really want to learn to do this on my own.

What would be ever cooler would be if I could actually help others....LONG way to go :slight_smile:

Thanks

Hi,

A great free editor is "Notepad++" See:

https://notepad-plus-plus.org/

It knows about many different languages. Set "C++" and open an Arduino .ino file.

It will show the nesting of 'if' and 'while" .

Books:
I've seen/bought many and I think 'Beginning C for Arduino' is excellent.

DISCLAIMER: I am the technical editor of this book...

Thanks Terry. Very helpful. Add Karma Clicked :slight_smile:

I downloaded and installed NotePad++

It's every bit as nice as you said!

Thanks again!

You might have guessed from what terryking228 said, but just to be clear: Arduino sketch .ino files are C++. Not based on C++, real C++.

You can also add .c files to your sketch, which are compiled as C and .S files to your sketch, which are compiled as assembly but almost all the Arduino code you will find is C++.

I used to print out my dbase code and use colored pens to marks programming "Nests" so that it was easier to study the code and debug it. For exmple, I would color all my IF/THEN nests one color, Do Whiles another etc etc.

C++ code pays no respect for whitespace. You can use it or not use it pretty much wherever you want. So if you'll look at code that is written by good coders it will be formatted with the blocks like that indented to a given level. That way it is always easy to see which blocks are nested in which ones.

Most good code editors will do this for you to some extent. In the Arduino IDE press control-T and it magically lines up all your blocks so you can see them clearly.

Delta_G:
C++ code pays no respect for whitespace. You can use it or not use it pretty much wherever you want. So if you'll look at code that is written by good coders it will be formatted with the blocks like that indented to a given level. That way it is always easy to see which blocks are nested in which ones.

Most good code editors will do this for you to some extent. In the Arduino IDE press control-T and it magically lines up all your blocks so you can see them clearly.

I would be "OK" with that, except....(this sums it up fairly well)......

Quote from Arduino Educational Resources...
Beginners programmers, and programmers coming to C from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.

I'm having a difficult time "reprogramming" my brain to understand where Curly Braces should and should not go.

If would be easier for me at this point to just use the words RETURN, ENDIF and NEXT....I think I'd understand better, at least for now.

pert:
You might have guessed from what terryking228 said, but just to be clear: Arduino sketch .ino files are C++. Not based on C++, real C++.

You can also add .c files to your sketch, which are compiled as C and .S files to your sketch, which are compiled as assembly but almost all the Arduino code you will find is C++.

So it's "real" C++

Good to know. Thanks
Eventually that's gonna help me......long way to go I'm afraid

Don't let that little blurb scare you. It's really pretty simple.

{ and } define a code block. A multi-line statement.

An if or a while or a for only run one statement. But by using a { and a } you can have multiple lines of code or even nested blocks.

If you'll indent them the way it shows in the examples then it works out just like the colors you describe using. You already understand the concept, you just have to see that it is the same thing.

if(someThing) {    
    //  everything
    //  inside the {
    //  and the } that matches
    //  is controlled by this if statement
    //  even
    if (nestedThing) {
            // it's nested in there
    }        // closes nestedThing
}            //  closes someThing

//  and everything outside is on it's own
//  not controlled by that if (or for or while)

Python is similar except in python its the indentation that is enforced and you don't use braces. But it is exactly the same concept.

The only other thing to note about closing off statements in braces is that it creates a scope block so any variables you create inside of it are only available inside there.