Skip to content

Code_Design

Carl edited this page Apr 30, 2022 · 9 revisions

4.1 Introduction - Coding

Coding the cloudSmoker project proved to be one of the steepest learning curves that I faced during this project. I ended up with a large code base over 1600 lines long (including debug scaffolding) and spanning ~20 library files!

At the project start, I was only a novice C coder (think Arduino "blink" program) and I really didn't have knowledge of the deeper particulars of C++ and, importantly, almost no understanding of object oriented programming concepts and the application of OOP within C++. This certainly changed over the course of this project and I've emerged with a much stronger coding foundation.

I started preparing for my coding journey by working through through a free online C++ course at learncpp.com. Working through this course was time consuming but certainly helped me get started.

4.2 IDE

Given the size of this project, I decided to move away from the Arduino Integrated Development Environment (IDE) software and to Micorsoft's Visual Studio Code and the PlatformIO plugin. Although I was faced with another learning curve, the depth of features in a professional IDE like VS Code / PlatformIO certainly helped me code this large project.

4.3 Code Design

My philosophy was to try to code the project as effectively and professionally as possible. Recognising that the code was going to be lengthy, I did my best to modularise the code base by separating, as much as possible, functionality into multiple files consisting of both public and personal library modules. Not only did I hope that this would make the code easier to understand and to debug, but it would also potentially make the code more portable for future projects. At the end of the day, modularisation also turned out to be a major source of frustration and time, as pursuing this approach often led to compiler and linker errors until I learned to more effectively manage critical interaction issues between files including variable, function and object definition, declaration, scope, duration, passing arguments and return values.

After investigation, I decided that the core of my program would be best implemented in a State Machine framework. I really like the State Machine approach as it provides a clear and logical way to structure and code the required functionality. I took the time to draw up a State Machine diagram for the cloudSmoker project and referred to this often as I coded.


As seen in the state diagram, the required states can be grouped into two main functions within the code: ) menu display and control and 2)temperature measurement and reporting

4.4 Display and Control

Similar to the Bald Engineer, I found that a significant effort was required to code the menu system, display it on a simple 1602 (16 char by 2 lines) backlit LCD and provide menu interface through the rotary encoder and associated button switch. The linked article has some excellent tips and tricks on LCD buffering and display.

Working with text in C / C++ and a microcontroller is quite tricky and full of gotcha moments. Newbies often gravitate to using Arduino's string class but, as explained in the classic blog post The Evils of Arduino Strings, the String class has so many shortcomings (particularly heap memory fragmentation0, that it is better use lower level C-strings.

Clone this wiki locally