The general structure of an Arduino robot program
We have already gotten to know the general structure of an Arduino program, consisting of three main parts:
#include
statements and declarations of global variables at the top.- The
setup()
function with all the necessary one-time initializations. It is called automatically at the start of the program execution. - The
loop()
function, which is executed in an infinite loop right after thesetup()
function has finished.
We can define many more functions that we can use inside the setup()
and the loop()
functions. This helps to keep these two main functions concise and readable, and it makes our program easier to maintain and modify. We usually write these functions at the end of the program, underneath the loop()
function. An example of this is the mcp_read_channel()
function that we developed in Chapter 2, Making Robots Perceive the World with Sensors.
This general structure is the same for every Arduino program...