Friday, February 10, 2017

Arduino Sketch Structure and Flow

Part 1 of the Arduino Programming Course
In this first part of the Arduino programming course, we look at the basic structure of an Arduino sketch and the top-to-bottom execution of program instructions (or program flow).

Arduino Sketch Structure

A basic Arduino sketch consists of two functions called setup() and loop().

Open the Arduino IDE and select File → Examples → 01.Basics → BareMinimum to see the two functions. These two functions now appear in a default new Arduino IDE window, so it is not necessary to open the BareMinimum example sketch in a new version of the IDE.
Basic Arduino Sketch Structure

What is a Function?

Functions will be covered in more detail later, for now you will just need to know the following about functions:
  • All functions must have a unique name, setup is one example of a unique function name (setup and loop are special functions in Arduino programming and form part of the structure of a basic sketch).
  • The function name is followed by opening and closing parentheses () that may or may not contain something.
  • All functions must have a return type. Both setup and loop have a void return type.
  • The body of a function consists of an opening and closing brace ({ and }).

"Hello, world!" Sketch Example

It is a programming tradition to write a "hello world" program whenever starting to learn a new programming language.
The "hello world" program simply writes the text "Hello, world!" to the screen. The purpose of this program is to verify that your programming environment is properly installed and working. If your "hello world" program works, then you are ready to start learning the new programming language.
The Arduino doesn't have a screen to write the "hello world" text to, but we can use the USB port and serial monitor window.

Writing the Sketch

Modify the BareMinimum sketch that you previously opened as follows:
void setup() {
  Serial.begin(9600);
  Serial.println("Hello, world!");
}

void loop() {
}
Save the modified program as hello_world in your sketches folder by selecting File → Save As... from the Arduino IDE menu and then renaming the file to hello_world.

Running the Sketch

Plug your Arduino into your PC using a USB cable. Click the Upload button to load the program to the Arduino.
Now open the Arduino IDE Serial Monitor Window to see the sketch run and print the text message.
The result of running the sketch should look as follows:

Fault Finding

Programming Errors

Anything in the above lines of code that is typed into the IDE window incorrectly will most likely cause a compile error, so be sure to type everything in exactly as it is shown in the code above. The program is compiled when the Verify button (the tick icon) or the Upload button (the horizontal arrow icon) is clicked.
A compile error will show up in the bottom of the Arduino IDE as shown in the image below.
In this example, the semicolon (;) was left off the end of this line: Serial.println("Hello, world!") which caused the Arduino IDE to display the error message.

Setup Faults

If you had problems uploading the sketch to the Arduino, make sure that the correct Arduino board is selected under Tools → Board and that the correct serial port is selected under Tools → Serial Port.

Baud Rate Setting Fault

If the sketch uploaded successfully, then the only problem that can prevent the text from being shown in the serial monitor window is if the baud rate at the bottom right of the serial monitor window is not set to 9600 as shown in the "Running the hello_world Sketch" image above.

Arduino Sketch Program Flow

In an Arduino sketch, program statements (individual lines of code) are executed or run from top to bottom. This top-to-bottom execution of statements can only be altered by flow control statements.

Parts of a Sketch

The image below shows the parts of an Arduino sketch. Statements are lines of code that are executed as the program runs. Each statement is terminated with a semicolon.
Program structure and parts of an Arduino sketch
Parts of an Arduino Sketch

How the Hello World Sketch Works

In the hello world sketch, statements in the setup() function are run first, from top to bottom. The statement Serial.begin(9600); is the first statement in the setup() function, so it is run first. This statement sets up the speed of the serial port to 9600 baud. The baud setting in the serial monitor window must match this value so that the Arduino and serial monitor window are communicating at the same speed.
The second statement to run in the setup() function is Serial.println("Hello, world!"); which sends the text Hello, world! out of the serial / USB port for display in the serial monitor window. In this statement, any text can be put between the opening and closing quotes ("") and it will be displayed in the serial monitor window.

The setup() Function

Statements in the setup() function are run only once, every time that the sketch is run. The program then starts executing statements in the loop() function.
The sketch will run after it has been programmed into the Arduino. Opening the serial monitor window will reset the Arduino and cause it to run the sketch again.
The sketch can also be rerun by pressing the reset button on the Arduino or disconnecting and then reconnecting the power to the Arduino.



The loop() Function

Statements in the loop() function will run continuously from top to bottom and then back to the top.
If the loop() function contains two statements, the first statement will be executed, then the second statement, then the first statement again and so on in a loop.
As there are no statements in the loop() function in our hello world example, program execution will end up in the loop and get stuck there doing nothing.
It is important to have the loop() function in the sketch, even if it is empty, because without it the microcontroller on the Arduino board will try to execute whatever it finds next in memory after the statements in the setup() function have been executed. The microcontroller will try to execute whatever it finds in memory as an instruction, but the loop() function prevents it from doing this by keeping program execution in the loop.
In the next part of this course, we will put some program statements in the loop() function to see how it works.

No comments: