Arduino Tutorials

Arduino Sketch Structure, Statement and Function

Arduino sketch structure, Arduino statement and Arduino function

In the previous tutorial, Arduino structure functions and variables, we discussed the various parts of Arduino programming language. We also discussed the various sections of the Arduino text editor, comments in the Arduino sketch, etc. However, this tutorial will give you an intuitive understanding of Arduino sketch structure, Arduino statement and Arduino function.

In this Arduino tutorial, we shall discuss the following:

  • Arduino sketch structure
  • Arduino statement
  • Arduino function

Arduino Sketch Structure

A basic Arduino sketch structure consists of two functions called “void  setup()” and “void loop()”.

BareMinimum Sketch
Figure 1: BareMinimum Sketch

You can see the line comments we discussed in Arduino structure functions and variables, included in the sketch.

Any code that is contained inside the void setup() function curly braces is executed once by the microcontroller. It is called the “setup” because it’s in this function that core codes required to set up the microcontroller are deposited. It runs once the Arduino is powered or when the reset button is pressed.

At the other hand, the void loop() function contains within its curly braces, lines of Arduino codes that are run repeatedly by the microcontroller, as long as the Arduino board is ON.

Any Arduino sketch that must work, surely must contain these two functions, even if no data is inputted inside their curly braces. A sketch that contains the two functions void setup() and void loop() and their curly braces but without any codes inside the braces is called a “BareMinimun” it’s often used to clear the Microcontroller of any pre-existing sketch which we talked about in the previous tutorial “Arduino tutorial for beginners“.

Arduino Statements

Within the functions are statements, these statements are bunch of instructional codes that will be executed by the microcontroller.

Figure 2: Arduino Sketch Sample

In the sketch above, code lines 17, 21, 22, 23, and 24 are statements. You can see that they all ended with a semicolon. Unlike the human sentence and statement which ends with a full stop, the Arduino statement ends with a semicolon. The Arduino IDE will alert an error if the programmer fails to close a statement with a semicolon. You have to be on the look, because this is a common mistake. Note that statements in the IDE are inbuilt functions that were called.  If you look at line 15 and line 20, and look at lines 17, 21, 22, 23 and 24 they seem to have same form. They both contain parenthesis. The difference is that the former are “function declarations for operation initiation”, while the later are “function calls for operation execution”.

When you write void setup(), you are declaring a function that will initiate a onetime operation, but writing pinMode (LED_BUILTIN, OUTPUT), you just called the function pinMode() and assigned an operation of causing LED_BUILTIN to serve as an output pin during sketch execution.

Arduino Function

Functions are used for controlling the arduino board and carrying out computations. Functions in Arduino programming come in two forms:

  1. The inbuilt functions which can be called in a sketch, e.g. pinMode(), delay(), etc. ; check Arduino structure functions and variables.
  2. Unique functions like the void setup() and void loop() as well as custom made functions which can be created by the programmer. The setup and loop are special functions in Arduino programming and they form part of the structure of a basic arduino sketch.

Qualities of a unique function include:

  1. Must have a unique name
  2. The name being followed by opening and closing parentheses () that may or may not contain statements.
  3. Must have a return type. Both setup and loop have a void return type.
  4. Consists of an opening and closing braces {and} which encloses code statements.

Let’s take the Void setup() function in the sketch above as an example

Arduino sample sketch depicting setup and loop functions
Figure 3: Arduino sample sketch depicting setup and loop functions

Function Format

Arduino Function Format
  • Void here means nothing; it means that the function does not categorically have a tangible thing it returns after code execution.
  • Setup is the name of the function.
  • Input values are argument that can be passed to the function for it to work on during execution; we will treat that when we treat custom made functions.
  • Statements are lines of executable codes that are written within the curly braces.

This is an introduction to Arduino function. In our next tutorial, we will delve more into the Arduino programming language.

Summary:

The Arduino structure must contain the Arduino bareMinimun sketch with void setup() and void loop() functions as the basic functions of arduino programming

Arduino Functions are used for controlling the arduino board and carrying out computations. Within the functions are code statements which are task execution  procedures.

Tags
Show More

Related Articles

Leave a Reply

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

Back to top button
Close