MANAGING THE ARDUINO CODE


Congratulations! If you have reached this point, you have assembled just about all of the Arduino electronics. However, that is only half of the puzzle. Now we must focus on the other half: the Arduino code.

Before you begin editing and uploading code to the Arduino, we need to first make sure we have a basic understanding of Arduino code and computer programming in general. It’s likely that some of you will have never seen computer code before. Computer code is simply a set of instructions that the computer (or in this case, the Arduino) reads in order to complete various tasks. Code is quite literally the computer’s language. Just like any language, you need to know how to “speak” it correctly. Otherwise, the computer will get confused and won’t know what you want it to do.

Begin by watching the following two videos:

Arduino code is based off a very common coding language called C++ and is fairly easy to use once you understand it. Below are some common components of Arduino code that you will encounter during this activity. Review each of these before moving on to Step 1. Also, we strongly encourage you to also read through this tutorial that explains some of these components in more detail.

*Although this can seem a little overwhelming to some, especially if this is your first time coding, be assured that you will not be required to write any new code yourself for this activity!


Sketches

When you write Arduino code and save it as a file, that file is called a sketch. It contains all your code.


Arduino IDE

The Arduino Integrated Development Environment (IDE) is the software we use on our computers to write Arduino code, save sketches, and upload the sketches to the Arduino. So in the same way that we use Microsoft Word to write and save word documents, we use the Arduino IDE to write and save Arduino sketches.


Libraries

An Arduino library is essentially a bundle of extra code that you can use in your sketches. Some libraries come pre-installed with the Arduino IDE, while many others can be downloaded manually. Many sensors and other electronic components have their own libraries that perform specific tasks related to that component.

To include a library in your sketch, simply use the #include statement at the top of the sketch. Each library will have a header (.h) file that will immediately follow the #include statement. When you upload the sketch to the Arduino, any included libraries will also be uploaded.


Variables

A variable is simply a place to store data in the code. It has a name, a data type, and a value. The name and value can be just about anything, and the value can change (hence the term “variable”). There are a limited number of variable data types from which you can choose.


Functions

A function is a block of code that can be used in your sketch to execute a specific action (or a sequence of actions). They are especially useful if you want to execute the same actions or commands multiple times. Instead of repeating the same code each time, you only have to write it out once and can then refer back to it as many times as you want. You can create your own functions or use built in functions that come with the libraries that were pre-installed with the Arduino IDE.

In the sketch, a function’s name is followed by two parentheses, such as myfunction(). You can also put variable names inside the parenthesis if you want to pass that information into the function. When first declaring (i.e., creating) a function, you must first write out the variable data type that the function will return. For example, if you want a function that calculates and returns a temperature in Fahrenheit, the type of variable that it returns should be a number. You could use a number variable with no decimal places, such as an integer (int), or one with decimal places, such as a floating-point number (float). Following the data type, you must then write the name of the function and the parentheses. The body of the function, where the actual tasks, actions, and calculations are carried out, is contained entirely within curly brackets { } that follow.

For example:

float myfunction(float tempC)     // declare data type, function name, and parenthesis with tempC variable to pass in
{                                 // curly bracket to open
float tempF;                      // declare new floating point temperature variable
tempF = (1.8 * tempC) + 32;       // convert from Celsius to Fahrenheit
return tempF;                     // return new temperature variable
}                                 // curly bracket to close

Later in your sketch, if you wish to call your new function, you simply use the following syntax:

float tempF;                   // declare a variable that will store the returned temperature variable tempF
tempF = myfunction(tempC);     // calculate tempF by passing tempC into the function, which performs the conversion


setup() and loop()

Both setup() and loop() are void functions that must be included in every Arduino sketch. A void function is one that does not return any specific information to the sketch, but instead only executes various tasks and actions, such as writing data to an SD card.

The setup() function is called only once, often near the beginning of the sketch. It is used to complete various setup tasks such as setting pin modes, initializing SD cards, and sensor calibration.

The loop() function is also called only once, at the very end of the sketch. It repeats over and over and over again (i.e., loops), from top to bottom, until the Arduino is turned off or loses power. It is used to complete all the various commands, tasks, and calculations that make up the heart of your project.


Comments

Within the code, you can add comments. These are lines of text that the Arduino ignores. They are typically used by the programmer to document the code and explain to others how it works.

There are two ways to add comments in an Arduino sketch. If you have a comment that is on only one line, you can add a // right before the comment. The Arduino will ignore everything after the double forward slash on that line. If you have comments than span multiple lines, you can put a /* right before the comments and a */ immediately after, and the Arduino will ignore everything in between.

For example:

tempF = myfunction(tempC);     // this is a single line comment

/* This is a multiple line comment
because it spans more than one line
and is often useful for longer explanations
or descriptions of the code */


Writing to Serial or SD Card

The Serial.print() function writes whatever you put within the parentheses (usually text or data) to the Serial monitor on your computer (learn more about Serial communication). For example, if you write Serial.print("Print this line"), the Arduino will write Print this line to the Serial monitor. If you want to write something and then move to a new line in the Serial monitor for the next print statement, you would use the Serial.println() function.

Similarly, you can write directly to the SD card that is connected to the Arduino. To do so, you would replace “Serial” with the name that was linked to the SD card when you opened it earlier in the code. To learn more, see the SD print documentation and SD.open() function.