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