# Introduction

Arduino is an open-source electronics platform that allows one to easily control hardware. An Arduino board is powered by a microcontroller. It is not as powerful as a CPU, the brains behind the computer you are using right now, but it has enough power to control hardware electronics. It does this mainly by specifying connections to output a voltage to and to read a voltage from. We control the output and input of the Arduino pins by writing a program using the C++ programming language.

There are many Arduino boards. The simplest board to use and learn with is the Uno board, which you can see above. The Uno board has pins that you can connect wires to. Each pin has a label next to it. We will cover later what these labels mean.

# Sketch

A Sketch is an Arduino program that gets compiled and uploaded to the Arduino board. To tell the Arduino how to control the electronics that we connect to it, we need to write a program or Sketch that tells the Arduino what we want it to do.

//this is a comment
//setup function, runs only once when the Arduino starts
void setup() {

}
//loop function, runs over and over again
void loop() {

}

The above code is the main structure of every Sketch (Arduino program) in C++. We will go through each part separately.

# Comments

The first line of the program is a comment. A comment means that this line will not become part of the program compiled and uploaded to the Arduino board. It is just available for the human reader of the program.

What is a compiler?

The Arduino board does not understand the code that we write directly. Our C++ code will go through another program known as a compiler that will read our code, validate that it does not have errors, and then generate machine code that the Arduino can understand. This process is known as compiling the code.


In C++ we can have two types of comments: a one-line comment and a comment block.

# One-line Comment

The comment needs to start with //. Anything that comes after the // will be ignored by the compiler.

# Block Comment

A block comment can contain multiple line comments. To start the block we use /* and to end the block we use *\. A block comment will be ignored by the compiler of Arduino, but might have useful information for the reader of the program. For example when you submit your solution to your teacher, at the beginning of your program file you can have a block comment that shows your information.

/*
    Student ID: 0123456
    Team Name: Cool Team
    This program does blablabla 

*/

# Arduino Functions

A function is a way to group lines of code and give them a name. Whenever we use that name, the program will call all the lines of code inside of that function. In the next sections we will learn more about functions and how to write our own.

The Arduino software simplifies writing a program to control the board by defining two functions for us. We will be writing our code inside these functions.

# Setup Function

The setup function in the Sketch program will be called ONCE by the Arduino board when we first turn it on. This is important to keep in mind. Whatever we wish to use at the beginning of the program only, we will write that code inside the setup function.

void setup() { 
    //write the code that will run ONCE
    //when the Arduino is first turned on
}

# Loop Function

The second function in a Sketch program is the loop function. This function will be called over and over again until you turn off the Arduino board. This is where you will write the code that will control your electronics.

When we are controlling electronics, we often want to do a repeated task. For example, imagine that you've connected a push-button and an LED to your Arduino board. And you want the LED to turn on whenever the push-button is pressed.

In the loop function, we will write code that will do the following:

void loop() { 
    /*
        IF the push-button is pressed 
            turn on the LED
        IF the push-button is NOT pressed 
            turn off the LED
    */
}

This code will repeat, looping at a high speed fast enough so that the moment we press the push-button, our code will be able to check if it has been pressed and decide whether to turn on the LED.