Arduino Programming Basics

In the getting started guide, you uploaded your first sketch that shows “Hello World!”. In this tutorial, you’ll learn how each part of that sketch works.

Here is the Hello World program.

Comments

The first few lines of the above sketch are a comment:

Everything between the /* and */ is ignored by the Arduino when it runs the sketch (the * at the start of each line is only there to make the comment look pretty, and isn’t required). It’s there for people reading the code: to explain what the program does, how it works, or why it’s written the way it is. It’s a good practice to comment your sketches, and to keep the comments up-to-date when you modify the code. This helps other people to learn from or modify your code.

There’s another style for short, single-line comments. These start with // and continue to the end of the line. For example, in the line:

the message “Declaring a variable to set the delay between text blink” is a comment.

Libraries

Libraries provide extra functionality for use in sketches, e.g. working with hardware or manipulating data. e.g. in O Watch you use the TinyScreen library tinyscreen.h and several sensor libraries. In the example above you have the following line at the top to include the TinyScreen library:

This library is required for all O Watch programs.

To use an additional library in a sketch such the one for the BMP280 pressure sensor, select it from the Sketch > Import Library menu. This will insert one or more #include statements at the top of the sketch and compile the library with your sketch. Or you can manually type in the include statement. Because libraries are uploaded to the board with your sketch, they increase the amount of space it takes up. If a sketch no longer needs a library, simply delete its #include statements from the top of your code. (Note: we noticed that when you add TinyScreen library from the IDE menu, it adds the font.h as well. This will give you an error when you compile. Please type in the include statem for TinyScreen.h to your program or delete the line with font.h if it appears.)

Variables

variable is a place for storing a piece of data. It has a name, a type, and a value. For example, the line from the sketch above declares a variable with the name delaytime, the type int, and an initial value of 1000. It’s being used to set a time value to be used for setting delays in the program. Every time the name delaytime appears in the code, its value will be retrieved. In this case, we could have chosen not to bother creating the delaytime variable and instead have simply written 1000 everywhere we needed to specify a delay. The advantage of using a variable is that it’s easier to change the delay time: you only need to edit the one line that assigns the initial value to the variable.

Functions

function (otherwise known as a procedure or sub-routine) is a named piece of code that can be used from elsewhere in a sketch. For example, here’s the definition of the setup() function from the example:

The first line provides information about the function, like its name, “setup”. The text before and after the name specify its return type and parameters. The code between the { and } is called the body of the function: what the function does.

You can call a function that’s already been defined (either in your sketch or as part of the Arduino language). For example, the line delay(delaytime); calls the delay() function, passing it the parameterdelaytime. The parameter is used by the delay() function to decide the amount of milliseconds to wait before proceeding to the next step.

The delay() causes the Arduino to wait for the specified number of milliseconds before continuing on to the next line. There are 1000 milliseconds in a second, so the line:

creates a delay of one second.

setup() and loop()

There are two special functions that are a part of every Arduino sketch: setup() and loop(). The setup() is called once, when the sketch starts. It’s a good place to do setup tasks like setting pin modes or initializing libraries. The loop() function is called over and over and is heart of most sketches. You need to include both functions in your sketch, even if you don’t need them for anything.

Arduino Language Reference

Please refer to Arduino Language Reference page for a complete list of commands and functions. Note that we will not be using most of the I/O functions of Arduino (e.g. pinMode, digitalWrite/Read, AnalogWrite/Read,..) in O Watch programming because most of the limited I/O will be handled by TinyScreen library functions.

<< Back: Arduino Software (IDE) | Next: O Watch Programming >>