CHAPTER 1 ■ ARDUINO BASICS
Code Construct Description
digitalWrite(...) Sets digital pin value (HIGH or LOW)
Serial.begin() Initializes serial monitor
Serial.print() Logs message on serial monitor
Serial.println() Logs message on serial monitor with new line
delay(ms) Adds a wait in processing
setup() Standard Arduino function called once
loop() Standard Arduino function called repeatedly
if Checks for a true/false condition
if ... else Checks for a true/false condition; if false goes to else
// Single-line comment
/ / Multiline comment
#define Defines a constant
#include Includes an external library
Table 1-1. (continued)
You can explore the complete language at https://www.arduino.cc/en/Reference.
The Arduino IDE provides a very simple and clean interface to write code. Normally
you would structure your code in three parts:
- External libraries: Includes all required libraries. A library is a
fully developed and tested piece of code that you can include and
use in your code. For instance, if you wanted to communicate
over the Internet using an Ethernet connection, instead of writing
all of that code from scratch, you could simply import and include
the Ethernet library using #include <Ethernet.h>. - Constants and variables : Defines all constants and variables
that will be used to read and manipulate data. Constants do not
change, so you can, for instance, use them for port numbers on
the board. Variables can change, so they can be used for reading
sensor data. - Functions : Provides implementation of all custom and standard
functions. A function encapsulates a specific functionality. It is
recommended to put your code in functions, especially when you
are looking to reuse that piece of code. Functions help avoid code
duplication.
Listing 1-1 provides an example of code that is structured according to points
discussed previously.