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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/* * Hello World * This is a simple program that blinks 'Hello World' * This for the Arduino compatible TinyScreen+ by http://tiny-circuits.com * * This example is in the public domain. * Published 29 Feb 2016 * by O Watch http://theowatch.com * */ #include <TinyScreen.h> //Include TinyScreen library TinyScreen display = TinyScreen(TinyScreenPlus); //Set TinyScreen board type int delaytime = 1000; //Declaring a variable to set the delay between text blink // The setup function runs once when you power on the board void setup() { display.begin(); //Initializes TinyScreen board display.setFlip(1); //Flips the TinyScreen rightside up for O Watch display.on(); //Turns TinyScreen display on display.setFont(liberationSansNarrow_12ptFontInfo); //Set the fornt type display.fontColor(TS_8b_Blue,TS_8b_Black); //Set the font color, font background display.setBrightness(10); //Set display brightness 0 - 15 } // the loop function runs over and over again forever void loop() { display.setCursor(15,25); //set the cursor to x, y position, where to start next print/draw instruction display.print("Hello World!"); //print command delay(delaytime); //delay for 1000 milliseconds or one second display.clearScreen(); //clears the screen blank delay(delaytime); //delay again for one second } |
Comments
The first few lines of the above sketch are a comment:
1 2 3 4 5 6 7 8 9 10 |
/* * Hello World * This is a simple program that blinks 'Hello World' * This for the Arduino compatible TinyScreen+ by http://tiny-circuits.com * * This example is in the public domain. * Published 29 Feb 2016 * by O Watch http://theowatch.com * */ |
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:
16 |
int delaytime = 1000; //Declaring a variable to set the delay between text blink |
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:
12 |
#include <TinyScreen.h> //Include 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
A 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
A 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:
19 20 21 22 23 24 25 26 27 |
void setup() { display.begin(); //Initializes TinyScreen board display.setFlip(1); //Flips the TinyScreen rightside up for O Watch display.on(); //Turns TinyScreen display on display.setFont(liberationSansNarrow_12ptFontInfo); //Set the fornt type display.fontColor(TS_8b_Blue,TS_8b_Black); //Set the font color, font background display.setBrightness(10); //Set display brightness 0 - 15 } |
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 parameter: delaytime
. 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:
34 |
delay(delaytime); //delay for 1000 milliseconds or one second |
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 >>