Introduction: Arduino Sketch Template
This Instructable does not utilize nails, wires (electrical or chicken) glue or cement. It is code that I use for my Arduino projects. After mastering basic bits of code to read and print a temperature/humidity sensor, some might want to consider a code ‘template’ for their increasingly complex coding.
Step 1:
I will not include in this Instructable how limited multi-tasking and/or reading multiple sensors can be accomplished – that may be for a future Instructable. I want to show how to take the step from blink to more complex coding.
A hallmark of good programming is the use of comments. I did not comment every single line, but I hope that you can understand those that I did not. Please send a PM if you have a specific section of code question.
Attachments
Step 2:
The code, as is, does compile and execute – currently on Arduino IDE 1.6.11
I have not tried the code on Teensy (it just came via UPS) or on Raspberry PI/Arduino IDE, and as noted in the code, printf does not compile on Intel Edison/Arduino.
Attachments
Step 3:
When an ‘if’ conditional (in any language) requires more than 3 ‘if’ or ‘else’ commands, it probably needs to be re-written.
‘goto’ commands have been demonized – mostly because of overuse, misuse and slop. They CAN simplify otherwise complex ‘if’ conditionals. A ‘return’ does not allow for exit of routine logic.
The C purists will likely revile and/or burn me in effigy. That’s OK with me – what I have said about them should not or cannot be printed. . I do not like deciphering obscure (but book glorified perfect) code written by others after being the one on call at 2 am.
I have working/finished projects using Ethernet, RTC, LCD, Nrf24L01+, HC-12, DHT11, 3dof, 9dof and many others, some over 1,500 lines of code (on an UNO no less), that started with this ‘template’. This 'template' started out with simple display and loop control; it has evolved as my Arduino projects have increased in complexity.
I have professionally programmed in 23+ computer languages over 45+ years. Arduino is a lot of fun to work with and capable of very complex tasks.
I invite civil & constructive comments or PM – I will ignore and not reply to those who are not.
Comments
7 years ago
What a great instructable! Thanks for sharing!