Friday, February 10, 2017

Arduino Sketch Main Loop and Calling Functions

Part 2 of the Arduino Programming Course
Part 2 of the Arduino programming course explains what a loop is in software and demonstrates how the main loop of an Arduino sketch works.
A function in a sketch performs some task (i.e. performs a function). We say that we "call a function" when we use a function to perform its specified task. This is explained in the second half of this part of the course.

The Main Loop

As seen in the previous part of this course, an Arduino sketch consists of two main functions called setup() and loop(). The loop() function is the main loop in the Arduino sketch. After statements that only need to be run once have finished being executed in the setup() function, program execution starts in the loop() function.
Once program execution has started in the main loop, the statements in the main loop will be executed continuously until the Arduino is switched off or reset. The main loop is where the actual operational functionality of the Arduino takes place – for example, if the Arduino is programmed to be a flashing light sequencer, then the flashing light functionality will be placed in the main loop.

Why is it Called a Loop?

Statements in the loop() function will be executed from top to bottom, until the bottom of the loop() function is reached. When the bottom of the loop function is reached, statements are executed from the top of the loop() function again, thus completing the "loop" as shown in the image below.




Loop Demonstration Sketch

The main_loop sketch shown below, demonstrates how the main loop works in an Arduino sketch. Type the sketch into your Arduino IDE, or copy and paste it into the IDE.
Load the sketch to the Arduino and then open the serial monitor window to see the sketch output text as it runs.
void setup() {
  Serial.begin(9600);
  Serial.println("*** This message will only be displayed on start or reset. ***");
  delay(2000);
}

void loop() {
  Serial.println("-- Arduino now at top of main loop. --");
  Serial.println("--------------------------------------");
  delay(2000);
  Serial.println("Executing instructions in main loop.");
  delay(2000);
  Serial.println("Arduino now at bottom of main loop.\r\n");
}
The text that the sketch prints to the serial monitor window is shown below.

This video shows the above sketch running.

As the above demonstration shows, the text in the setup() function is only displayed once when the serial monitor window is first opened and the Arduino is reset. After this, program execution enters the loop() function and repeatedly executes the statements in the loop from top to bottom and back to the top again in a never ending loop.

Functions in the Loop Demonstration Sketch

In the main_loop sketch above, each statement in setup() and loop() consists of a function being called – i.e. being called means that it is executed or run.

The delay() Function

When the delay() function is called in the statement delay(2000); then the delay function causes a waiting period of 2 seconds (2000 milliseconds – there are 1000 milliseconds in one second, also written 1000ms). The time of the delay can be changed by passing a different value to delay(), e.g. 3000 will cause a 3 second delay: delay(3000);

The println() Function

The println() function sends text out of the serial / USB port of the Arduino and is displayed in the serial monitor window.
The println() function is different from the delay() function in that it has Serial and a dot (.) before it: Serial.println("Text to print.");
The reason for this notation (Serial.function_name()) is because the function acts on the serial port or Serial object. You will notice in setup() that Serial.begin() is called. This is the begin() function acting on the serial port – in this case to set it to the desired speed.
These functions that are preceded by an object name (e.g. Serial) are called "methods" in object oriented programming.

A Summary of Functions

The following will hopefully clear up what functions are and the terminology used with them. A deeper understanding of functions will only be possible once we start writing our own functions.

setup() and loop()

setup() and loop() are two special functions that form part of the structure of an Arduino sketch.
We are actually writing these special functions by giving them a function body (between the opening and closing braces: {}) and writing statements in the function body.
The statements in these functions in the above sketch were calling pre-existing functions that perform the tasks that we want, e.g. set up the serial port speed, cause a time delay, write text to the serial monitor window.
The setup() and loop() functions are automatically called at the right time because they are special Arduino functions.

Calling Functions

By calling or using pre-existing functions, we are using code that someone else has already written.
The delay() function has a function body that contains statements that cause it to perform a delay. We do not see these statements or the function body because they are either part of the Arduino programming language or exist in an external function library.

Passing a Value to a Function

When a value (e.g. a number or text string) is used by a function, we must pass the value to the function.

Passing a Value to the delay() Function

We call the delay() function in the sketch as in the following statement:
delay(2000);
The delay value in milliseconds (2000) is said to be passed to the function.

Passing a Value to the println() Function

We pass a text string to the println() function as shown in this statement:
Serial.println("Executing instructions in main loop.");
We must pass the text string to the function so that the function knows what to send out of the serial / USB port. The text between the opening and closing quotation marks ("") is known as a string in programming.



A Commentary on the main_loop Sketch

The main_loop sketch from above can be seen here again, but with commentary explaining what is happening in the sketch.

A 2 second delay has been added so that the next text to be displayed in the serial
monitor window does not appear immediately.
  delay(2000);
}

The statements in the setup() functions have all finished being executed, so now the
loop() function is called automatically.
void loop() {
Now that execution is taking place in the loop, the statements inside the loop()
function will be executed from top to bottom. When the bottom of the loop is reached,
all the statements will be executed again from top to bottom.
 
This text is sent to the serial monitor window and is the first statement to be
executed in the loop.
  Serial.println("-- Arduino now at top of main loop. --");

This text is just to highlight that we are at the top of the loop. Because there is
no delay between the above text and this text, both appear at the same time in the
serial monitor window.
  Serial.println("--------------------------------------");

A two second delay so that the line of text that follows does not appear instantly
with the above text.
  delay(2000);

We are now executing statements (also called instructions) in the middle of the main
Arduino loop. This statement sends more text to the serial monitor window.
  Serial.println("Executing instructions in main loop.");

Another delay so that the line of text that follows does not appear at the same time
as the line of text above.
  delay(2000);

The last statement in the loop and the last text that gets sent to the serial monitor
window before returning to the top of the loop to start executing statements again.
The println() function causes the invisible cursor to move to the next line in the
serial monitor window. This causes the next text sent to the serial monitor window
to appear on the line below the currently printed text.
The \r\n at the end of this text of string moves the invisible cursor down one more
line leaving a blank line between the text printed at the bottom of the loop and the
text printed at the top of the loop.
  Serial.println("Arduino now at bottom of main loop.\r\n");
}

No comments: