Notice the presence of curly braces, which are used to group lines of code together. These are particularly useful when you want to give a name to a group of instructions. If you’re at dinner and you ask somebody, “Please pass me the Parmesan cheese,” this kicks off a series of actions that are summarised by the small phrase that you just said. As we’re humans, it all comes naturally, but all the individual tiny actions required to do this must be spelled out to the Arduino, because it’s not as powerful as our brain. So to group together a number of instructions, you stick a { before the block of code and a } after.

You can see that there are two blocks of code defined in this way here. Before each one of them are some strange words:

void setup()

This line gives a name to a block of code. If you were to write a list of instructions that teach Arduino how to pass the Parmesan, you would write void passTheParmesan() at the beginning of a block, and this block would become an instruction that you can call from anywhere in the Arduino code. These blocks are called functions. Now that you’ve created a function from this block of code, you can write passTheParmesan() anywhere in your sketch, and Arduino will jump to the passTheParmesan() function, execute those instructions, and then jump back to where it was and continue where it left off.

This points out something important about any Arduino program. Arduino can do only one thing at a time, one instruction at a time. As Arduino runs your program, line by line, it’s executing, or running, only that one line. When it jumps to a function, it executes the function, line by line, before returning to where it was. Arduino can’t run two sets of instructions at the same time.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *