# Planning and Implementing an Arduino Program

**URL:** <https://forum.arduino.cc/t/planning-and-implementing-an-arduino-program/252364>\
**Category:** Programming\
**Created:** [August 17, 2014, 12:37pm UTC](https://forum.arduino.cc/t/planning-and-implementing-an-arduino-program/252364 "2014-08-17T12:37:28Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [August 17, 2014, 12:38pm UTC](https://forum.arduino.cc/t/planning-and-implementing-an-arduino-program/252364/3 "2014-08-17T12:38:37Z")

</div>

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()

```cpp
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.

```cpp
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 ...

```cpp
void setup() {

}

void loop() {
    flashLedA();
    flashLedB();
    checkButtons();
    setFlashPeriod();
    askForUserInput();
    getUserResponse();
    moveServo();
    readPotentiometer();
    setServoPosition();
}

```

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.

```cpp
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

```cpp
// 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

---

_[View the full topic](https://forum.arduino.cc/t/planning-and-implementing-an-arduino-program/252364)._
