# Hello World

The Hello World program is usually the first program you will write in any programming language you learn.

Hello World is simply a program that outputs "Hello World" to the user.

We will be using a virtual environment to write programs for an Arduino Uno board. This virtual environment is a real simulation of the Arduino board which means that the program you run here can be used on a real Arduino board.

# Serial Function

Previously, we explained briefly about functions and what they mean in software. There is a separate chapter that covers functions and how to write them. However, to start writing meaningful programs right away we will learn how to use functions from the Arduino library.

Library?

A software library is a collection of code that developers can use in their programs. You can not write everything from scratch yourself. Software libraries allow us to write programs faster by reusing code that other developers wrote.

# Serial Communication

The Serial Monitor communication on an Arduino is a way to communicate between the Arduino board and a computer, usually using a USB wire that connects the Arduino board to our computer. Communication means sending data. Our goal is to send "Hello World" as a message from our program that is running on the Arduino board to our computer screen.

In our case, we don't have to connect anything as we are using a virtual environment.

In the Arduino library, there are two functions we need to use

Serial.begin(9600);

and

Serial.println("your message");

Both functions are part of the Serial library. This is why we are starting our statement with Serial and then . to access functions from this library.

The first function sets the rate of data transmission between the Arduino and the Serial Monitor. You don't have to understand how this fully works at this point. However, 9600 is the rate of transmission of data in bauds/second to be used with the Serial Monitor. Before we can transmit data we must begin the serial.

After starting the Serial we can then use the second function to print a full line onto the Serial output.

How to represent a line in C++?

To write a line to the function we will need to start with " and finish the line with ". This is known as a string of characters.

The function is called println which means "print line". After it prints the characters we pass to it, the function will print a newline character at the end.

What is a new line character?

There is a character in the computer system that represents starting a new line. You usually do not see this character on a website or in a word processing document because it gets automatically rendered as a new line in your text.

On the Arduino platform the new line character is \n. Using the print function like this

Serial.println("your\n message");

notice the addition of \n between "your" and "message" The output in the Serial will then be

your
message

instead of

your message

We will use both functions inside the Setup. We want the Arduino to send Hello World only once when the Arduino is first turned on.

Exercise

Instead of printing Hello World, print

Hello 
Computer
Answer
void setup() 
{
  Serial.begin(9600);
  
  Serial.println("Hello\n Computer");
}
void loop() 
{

}

# Semicolon

In C++ a semicolon is important at the end of every full statement. In our Hello World example notice that at the end of every line inside the Setup function, there's a semicolon to terminate this statement.

This is part of the syntax of a program in C++. If we write the program with the wrong syntax, the compiler will give us errors and the program will not run.

For example if we run the following program:

void setup() 
{
 Serial.begin(9600);
 
 Serial.println("Hello Computer") //removed the semicolon from here
}
void loop() 
{

}

The output of the compiler will be

/sketch/sketch.ino: In function 'void setup()':
/sketch/sketch.ino:6:1: error: 
expected ';' before '}' token } 
^
Error during build: exit status 1

You can try it yourself. Remove the semicolon from the program and run it in the above Arduino environment.

The error in many cases explains the problem well. In this case the compiler is first telling us that there is an error inside the setup function

/sketch/sketch.ino: In function 'void setup()':

It is also telling us at what line the error is happening, which is line 6 and the first character of that line. You can see it in the second line .ino:6:1

/sketch/sketch.ino:6:1: error: 

And then the compiler is explaining to us what it thinks the problem is. On line 6 the compiler found the character } and it was expecting ;. The semicolon was expected at the end of line 5.

Exercise

The following program has 2 errors. Fix the errors to get the program running and output Hello Hackers.



# Delay Function

We know now that the Arduino will first run the code inside the Setup function once and then keep on running the code inside the loop function over and over again until we turn off the Arduino.

However, sometimes, we will need to tell the Arduino to sleep for a bit. For example let's say we want to print "Hello World" once every second...forever.

This task sounds suitable for the loop function. But if we write our program like this

void setup() 
{
 Serial.begin(9600);
 
}
void loop() 
{
 Serial.println("Hello World");
}

The Arduino will output our message repeatedly so fast. What we want the Arduino to do is to sleep for 1 second before the beginning of every loop.

There is a function in the Arduino library called delay(TIME_IN_MILLESECONDS). We pass the time we want the Arduino to sleep for to this function and the Arduino will not continue processing our lines of code until the time we specify passes. The time we pass is in milliseconds.

The following program will wait for 1 second (1000 millisecond) at the end of every loop iteration.

Note:

  • We used begin in the Setup function for the Serial because we only need to start the Serial once at the beginning of the program. If we put it inside the loop then at every loop iteration the Serial will begin again.

  • At the end of delay(1000) we have a semicolon to end this statement. If we don't put a semicolon we will get a compiler error like we saw before.