Editing Smarter Arduino Programming - Tips and Tricks
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 3: | Line 3: | ||
= Introduction = | = Introduction = | ||
− | This draft is for an instructable | + | 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. | 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. | ||
Line 19: | Line 19: | ||
= Easier Search = | = Easier Search = | ||
− | Most programs consist of a bunch of subroutines. If you have a list near the top of the program ( perhaps as comments ) it is easy to highlight them ( perhaps with a double click ) | + | Most programs consist of a bunch of subroutines. If you have a list near the top of the program ( perhaps as comments ) it is easy to highlight them ( perhaps with a double click ) it ctrl-f ( for find ) and then zip down to the routine ( or calls to it ) in a flash. |
In the example I have a help routine rptHelp() with the relevant subroutine names as comments that serves this purpose, and in the loop() is a small command interpreter that dispatches to different subroutines. Each can serve double duty as an assistant to finding the code. Both are near the top of the program so you do not have to scroll a long distance to find them. | In the example I have a help routine rptHelp() with the relevant subroutine names as comments that serves this purpose, and in the loop() is a small command interpreter that dispatches to different subroutines. Each can serve double duty as an assistant to finding the code. Both are near the top of the program so you do not have to scroll a long distance to find them. | ||
Line 39: | Line 39: | ||
</pre> | </pre> | ||
− | Then at the top of BlinkInstruct. | + | Then at the top of BlinkInstruct.h put the line |
<pre> | <pre> | ||
Line 58: | Line 58: | ||
= Add Some Documentation = | = Add Some Documentation = | ||
− | Many program become difficult to use and modify because of | + | Many program become difficult to use and modify because of inadaque documentation. This can even trip up the autho of the program. So add some documentation. But where: |
* Start with good naming, often if names are good you are done. | * Start with good naming, often if names are good you are done. | ||
Line 72: | Line 72: | ||
= Serial Monitor = | = 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 | + | 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. |
+ | |||
== Commands == | == Commands == | ||
Line 78: | Line 79: | ||
Many programs boot up, set up, and run a loop forever doing one thing over and over. | Many programs boot up, set up, and run a loop forever doing one thing over and over. | ||
− | I like to make my programs | + | I like to make my programs mor interesitng by letting a user control them using the serial monitor ( or other terminal program, see especially: [[Python Smart Terminal]] ). I have written a library like object SerialCmd to support this. SerialCmd.cpp and SerialCmd.h are used over and over in my different programs, they are essentially library programs and you can just use them as is without every looking at them. |
So here I will not discuss there internals but just how they are used in the sample program. | So here I will not discuss there internals but just how they are used in the sample program. | ||
Line 95: | Line 96: | ||
== Version == | == Version == | ||
− | |||
− | + | The command v version of software prints the version of the software: something like "SerialCmdMaster with Blink Ver4 2017 11 17.2" | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== Help == | == Help == | ||
− | |||
− | |||
= F...ing Strings = | = F...ing Strings = | ||
Line 117: | Line 105: | ||
I use lots of messages which means lots of string literals like: | I use lots of messages which means lots of string literals like: | ||
− | |||
Serial.println( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) | Serial.println( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) | ||
− | + | ||
this can use a lot of dynamic memory ( which you have less of than program memory ). Change your code to: | this can use a lot of dynamic memory ( which you have less of than program memory ). Change your code to: | ||
− | |||
Serial.println( F( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ) | Serial.println( F( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ) | ||
− | |||
− | and the string goes in program memory ( which you normally have lots of ). | + | 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 ); | ||
+ | |||
= Blink a Pin - Time It = | = Blink a Pin - Time It = | ||
− | |||
− | + | ||
+ | |||
+ | |||
+ | |||
= A better IDE = | = A better IDE = | ||
Line 139: | Line 139: | ||
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. | 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. | ||
− | + | = Draft material = | |
+ | |||
+ | |||
+ | |||
+ | Better Arduino Programs with Serial Commands | ||
+ | |||
+ | |||
+ | A lot of programs for the arduino are very simple and limited. Some only repeat what is on the arduino.cc site. Lets take blink. You compile it, Run it. And ( with luck ) the led blinks. Want to change how many times it blinks or how often and you change the code, recompile, upload, and run again. A better way is to alter the program so you can tell it what to do at run time with a serial monitor, or with another terminal program ( I recommend my …... ). | ||
+ | |||
+ | Here is how. | ||
+ | |||
+ | A good way to see how this is done is to look at a program that implements this approach. Download the attached zip file, unzip and put the results in a directory called SerialCmdMaster wherever you keep your arduino programs Then open the location in arduino.exe. You will see 4 files each in its own tab. Or go to github () and you can look at the files in your browser. | ||
+ | |||
+ | You will find 4 different files, one for each of the two parts of the program. SerialCmd.cpp and SerialCmd.h are used over and over in my different programs, they are essentially library programs and you can just use them as is without every looking at them. SerialCmdMaster.ino and SerialCmdMaster.h are where the type of programming I am recommending live. I have used the strategy of using a header file SerialCmdMaster.h for the important constants in the code, neither of the .h files have executable content. | ||
+ | |||
+ | So most of the action is in SerialCmdMaster.ino which is what I will discuss now. | ||
+ | |||
+ | The whole program is divided into a bunch of subroutines including the familiar setup and loop. I put a comment section with some documentation at the top. Then, pretty much as usual in a C program are some includes and variable declarations. All straight forward. | ||
+ | |||
+ | The next subroutine is my tiny little help system. It is just a bunch of serial print statements that print one line for each command in the system. | ||
+ | |||
+ | So let me digress from how the internals of the program work to how it works when you connect it to a serial terminal ( of the serial monitor ) | ||
+ | |||
+ | It boots up, starts its serial port and prints the help text. | ||
+ | |||
+ | One of the commands that it then available is h followed by a newline, the newline is automatically appended if you turn …...... What does h show help do? It just prints the same help text shown at startup. | ||
+ | |||
+ | The command v version of software prints the version of the software: something like "SerialCmdMaster with Blink Ver4 2017 11 17.2" | ||
+ | |||
+ | In general all commands are just a single letter. To add a bit more functionality to the commands you can also stick a number on the command: "unn set micro seconds" might look like u22 which would cause a variable in the program to be set to 22 microseconds. "bnn blink_1 n times uses micros only " which might be given as b2000 calls the blink_1 subroutine and causes it to blink 2000 times using the microsecend value set with the u command. | ||
+ | |||
+ | So the help command prints out a reminder of the commands. On each line of the help printouts there may be a comment that help you search for the subroutine that supports the command. | ||
+ | |||
+ | Often the loop subroutine is found at the bottom of the program. I put it right after the help because it implements the help by getting the command letter ( and number if any ) and then using a switch statement calls the proper subroutine. The call statements in it are also useful in searching the rest of the code. | ||
+ | |||
+ | The rest of the code is just a series of pretty simple subroutines that implement the commands the help system names. | ||
+ | |||
+ | This code has little practical purpose except to show how the serial command system works ( and as my test framework for SerialCmd ) and to run a few basic blink routines. The idea is that you make a copy, rename the bits of pieces, and transform it into your own code with a command interface. | ||
+ | |||
+ | This code has a connection to another instructable because it is an example of some of the techniques discussed there: | ||
[[Category:Arduino/RaspberryPi]] | [[Category:Arduino/RaspberryPi]] |