Difference between revisions of "Smarter Arduino Programming - Tips and Tricks"
Russ hensel (talk | contribs) |
Russ hensel (talk | contribs) |
||
Line 87: | Line 87: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Draft Sections to Write = | = Draft Sections to Write = |
Revision as of 09:33, 3 February 2018
Smarter Arduino Programming
Contents
Introduction
This draft is for an instructable not yet written. Note that this is an article started by Russ Hensel, see "http://www.opencircuits.com/index.php?title=Russ_hensel#About My Articles" About My Articles
The following are some tips for better quicker programming of Arduinos. Much of the advice applies to other platforms so you may want to take a quick look even if you do not use the Arduino. This advice applies to the standard computer arduino ide, but applies to other environments as well. Some of the tips are fairly well known/standard but are often not used in the code I see published.
Easier Reading while Coding
Much of coding of all but the smallest programs involves scrolling up and down in the code looking for subroutine, names, signatures, code snippets... This scrolling makes you loose your place where you are actually writing the code. Simple solution: Open the file in another editor for read only access. You can still copy and paste between the two copies. From time to time refresh the read only copy.
Use Header Files
This is a very standard practice by C programmers, but I rarely see it in instructable projects.
Header files or .h files are commonly used for any settings that are used in the code that you might want to change without searching through the code. Very often this is done with pound defines. For example if you have a program foo.ino make a text file foo.h. In foo.h you might have:
- define BLINK_PIN 3 // blink this pin
- define BLINK_DELAY 44 // ms delay in blink routine
Then at the top of foo.ino put the line
- include "foo.h"
Finally use the #defines in the code something like
delay( BLINK_DELAY )
I am not sure what the official way is to get the arduino.exe to open the .h files ( and .cpp files.... ). My method is just to put the files in the directory with the code ( when the code is not open ). The next time you open the file the .h and other files will be show in the project, each in its own tab.
If this is unclear Google it, you will find a ton of info out there.
Easier Search
I usually have a little list of the important subroutines in my program in comments at the top of long programs. Double click on the name; hit ctrl-f ( for find ) and then zip down to the routine ( or calls to it ) in a flash
A better IDE
I have not looked hard but there may be more powerful IDE's for the Arduino. They may take some effort to find and setup, I have not done this, but if you do not like arduino.exe look around. Let me know what you like in the comments.
Add Some Documentation
I put another .h file in my projects called readme.h. It is not included in any other file but is a good location for scratch work, example code, and longer notes that you might not want to put in the code.
Comments in this file do not need // to mark them as comments because the file is never complied.
Arduino.exe will automatically open the file so it is always at hand when you are working on your code.
Serial Monitor
Always have a serial monitor as part of your code. This lets you more easily see what is going on in the program and optionally control it. I have another instructable that give a quite powereful way to do this that is also simple to implement. If no serial monitor ( terminal program ) is connected there is generally no harm, the bits just go right in the bit bucket for recycling.
F...ing Strings
I use lots of messages which means lots of string literals like:
Serial.println( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
this can use a lot of dynamic memory ( which you have less of than program memory ). Change your code to:
Serial.println( F( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) )
and the string goes in program memory ( which you normally have lots of )
Version
I do a lot of programs and a lot of revisions. Sometimes I am not working with the version that I want. So my code always includes a version that it sends to the serial port when the program starts and any time I ask the program for it. In the header file:
- define VERSION_ID F( "SerialCmdMaster with Blink Ver4 2017 11 17.777" )
In the program:
Serial.println( VERSION_ID );