Planning&ImplementingArduinoPrograms

From ArduinoInfo
Jump to navigation Jump to search

Planning and Implementing an Arduino Program


Based on the work of "Robin" in this series of posts on Arduino.cc:
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.



CHAPTER 2 - ORGANIZATION


To repeat, the plan is to create 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.


Every Arduino program must have the functions setup() and loop()

Code: [Select]



void setup() {

}

void loop() {

}



Before we go any further let's try to describe all the "actions" that must happen for this project to work. For the moment don't worry about the order of things - just jot them down as they occur to you. For example ...


  • flash LEDa
  • flash LEDb
  • check the buttons
  • set the flashing period
  • ask the user for input
  • get the user's response
  • move the servo
  • read the potentiometer
  • set the servo position











Without noticing it we now have a huge part of the program designed. Let's make a separate function for each of those actions.

Code: [Select]


void flashLedA() {

}

void flashLedB() {

}

void checkButtons() {

}

void setFlashPeriod() {

}

void askForUserInput() {

}

void getUserResponse() {

}

void moveServo() {

}

void readPotentiometer() {

}

void setServoPosition() {

}






Of course at this stage none of these functions can do anything as they have no code within them - but we'll worry about that later.



Now, how would you go about using all those little functions? Let's try something very simple. What about this ...





Together with the earlier functions this is a complete Arduino sketch that will compile and run - even if it does nothing.

It is easy to write the code so that the order of the function calls doesn't matter - the Arduino does things so quickly that, for example, it will not be obvious that the change in the led flashing does not happen until the next iteration of loop() after the button is pressed.

Although the order doesn't really matter to the Arduino it will be easier to understand what is going on if we organize the functions in the order that seems sensible to us.

Code: [Select]



void loop() {



checkButtons();



setFlashPeriod();



flashLedA();



flashLedB();





askForUserInput();



getUserResponse();





readPotentiometer();



setServoPosition();



moveServo();



}



I hope you can see that a quick read through loop() gives you a very good overview of what the whole program does.

It is very important to keep in mind that the Arduino will repeat all of these functions hundreds or thousands of times per second. And you must take care to ensure each function takes as little time as possible. The faster loop() can repeat the better.

The complete version of the code at this stage is in LessonA.ino

Code: (LessonA.ino) [Select]



LessonA.ino



Initial program with empty functions

void setup() {

}

void loop() {



checkButtons();



setFlashPeriod();



flashLedA();



flashLedB();





askForUserInput();



getUserResponse();





readPotentiometer();



setServoPosition();



moveServo();



}

void flashLedA() {

}

void flashLedB() {

}

void checkButtons() {

}

void setFlashPeriod() {

}

void askForUserInput() {

}

void getUserResponse() {

}

void moveServo() {

}

void readPotentiometer() {

}

void setServoPosition() {

}



...R






zz