Planning and Implementing an Arduino Program

CHAPTER 1 - INTRODUCTION

When you are new to programming and the Arduino system it can be very tempting to use one of the example sketches and make changes or add bits onto it to meet your needs.

If what you are doing is even a little bit complex this approach will quickly turn into an incomprehensible mess. There are plenty of questions in the Forum that provide vivid examples of this.

Adding bits and pieces will probably seem fine as long as you have the correct image of the code in your head. But as soon as something doesn't work as expected the mental image will be derailed and figuring out the problem becomes a nightmare - literally - late nights, too much coffee and no progress. More bits are added to try to monitor the progress through the code. And those bits themselves start to cause confusion.

To make matters worse, if you want to get help on the Forum your mess of code will be even less comprehensible to strangers - so you spend more time explaining yourself than getting help.

This Thread shows a simple way of organizing an Arduino project so the code is easy to understand and to debug. And a little bit of organization will actually get your project finished and working more quickly.

The Thread is a series of Lessons that work through all the logic and the steps needed to create the code so that you can follow why it is done that way.

My objective is to write my code that I will understand 12 months later with just a single read-through. Writing code that is easy to understand also makes it much much easier to get assistance on the Forum.

I'm just going to dive straight in and start work on this little project. If you are interested in the philosophy jump to Chapter 9, though it might make more sense after reading Chapter 2.

For these Lessons to make sense we need a moderately complex project. At the same time it should be something that needs few specialized parts so most people can try it. So let's write a program that moves a servo to a position set by a potentiometer, asks the user for input from the Serial Monitor and causes two LEDs to flash at different speeds depending on which of two buttons is pressed.

If you don't have a servo or a potentiometer or LEDS and resistors you can follow along anyway. The program will work perfectly well without those things connected - you just won't see the effect of the code.

I hope the code associated with each Chapter follows properly from the earlier versions but there are 7 versions and some discrepancies may have crept in. I have tested each version on my Uno.

In Reply #14 @JimboZA has very kindly produced a wiring diagram to show all the project connections.

For the more experienced readers

  • if you find errors please let me know so I can correct them.
  • if you just prefer to do things differently, may I request that you write your own Lessons in another Thread :slight_smile:

...R

6 Likes