Arduino Programmer’s Reference (C++)

We offer you the most comprehensive and complementary reference for the Arduino programmer. The directory is constantly get better and updated.

Operators

Setup

Loop

Control operators

if

If..else

for

switch case

while

do... while

break

continue

return

goto

Syntax

; (semicolon)

{} (braces)

// (single line comment)

/* */ (multiline comment)

#define

#include

Arithmetic operators

= (assignment operator)

+  (addition)

- (subtraction)

* (multiplication)

/ (division)

% (remainder of the division)

Comparison operators

== (equal)

!= (not equal)

< (less than)

> (more than)

<= (less or equal)

>= (more or equal)

Logical operators

&& (and)

|| (or)

! (not)

Access pointers

* pointer

& link

Bitwise operators

& (bitwise AND)

| (bitwise OR)

^ (bitwise XOR or exclusive OR)

~ (bitwise NOT)

<< (bitwise left shift)

>> (bitwise right shift)

Compound Operators

++ (increment)

-- (decrement)

+= (compound addition)

-= (compound subtraction)

*= (compound multiplication)

/= (compound division)

&= (compound bitwise AND)

|= (compound bitwise OR)

Data

Constants

HIGH | LOW

INPUT, INPUT_PULLUP, OUTPUT

true | false

integer constants

floating point constants

Data types

void

boolean

char

unsigned char

byte

int

unsigned int

word

long

unsigned long

short

float

double

string - array of characters

String - object

arrays

Type conversion

char()

byte()

int()

word()

long()

float()

Variable scope and qualifiers

Variable scope

static

volatile

const

Auxiliary function

sizeof()

Libraries

EEPROM

SD

SPI

SoftwareSerial

Wire

Auxiliary classes

Класс Serial

Класс Stream

Libraries for Leonardo

Keyboard

Mouse

Functions

Digital I/O

pinMode()

digitalWrite()

digitalRead()

Analog I/O

analogReference()

analogRead()

analogWrite() - PWM

Only for Due

analogReadResolution()

analogWriteResolution()

Advanced I/O

tone()

noTone()

shiftOut()

shiftIn()

pulseIn()

Time

millis()

micros()

delay()

delayMicroseconds()

Mathematical calculations

min()

max()

abs()

constrain()

map()

pow()

sqrt()

sq()

Trigonometry

sin()

cos()

tan()

Random numbers

randomSeed()

random()

Bits and bytes

lowByte()

highByte()

bitRead()

bitWrite()

bitSet()

bitClear()

bit()

External interrupts

attachInterrupt()

detachInterrupt()

Interruptions

interrupts()

noInterrupts()

Arduino is a platform that allows many engineers from around the world to create their projects with minimal investment. First of all, it is a special microcontroller with a control system of the same name and libraries built in C++. Accordingly, if you plan to create something unique, you should learn all the nuances that Arduino programming has.

Let's make a brief description of the Arduino programming and clarify points that you should pay attention to if you are doing this for the first time.

Books for you

Arduino Essentials

Arduino Essentials

This book is for you if you are a hobbyist wanting to develop projects based on Arduino as its main microcontroller platform or an engineer interested in knowing what the Arduino platform offers, then this book is ideal for you.

Read more and download

Exploring Arduino

Exploring Arduino

This book is for Arduino enthusiasts of all experience levels. Chapters build upon each other, utilizing concepts and project components from previous chapters to develop more complex ideas.

Read more and download

Arduino Basics

Before embarking on a specific task at Arduino, it is best to have a basis in the field of programming. Therefore, let's consider what this term generally means.

Programming is the task of certain algorithms translated into a computer language in order to perform a specific task set by the user. Accordingly, absolutely any project is built on a phased block model, which describes what your microcontroller needs to do and how to do it.

To simplify the work of users in Arduino, ready-made libraries of functions have been created; you just need to enter commands from them to achieve a goal. Naturally, in this way you will not achieve much, but to create your own libraries you will need knowledge of the C++ language on which the firmware of the chip is built.

A key feature of the system is that the characteristics of the Arduino can be improved with the help of additional components, and you can always adjust them for a specific project. Accordingly, your only limitation is the knowledge of the language and its capabilities, as well as your own imagination.

All functions are built from the simplest operand, which are characteristic of C ++. These operands are variables of various types and methods of their use. Therefore, any function used in the microcontroller to receive information or send a signal is a set of simple operations that is recorded in the main library. And you will be limited until you get enough experience and practice to understand which library and for what purpose you should write.

The main drawback of designing arduino with complex projects is that you have to write code from scratch and select components for the system, so it is better to first practice the simplest tasks.

Also, keep in mind that the writing language of the system libraries is low-level, and accordingly, consists of the simplest commands, as opposed to high-level python or pascal, which are user-friendly. On the other hand, it is also multi-paradigm, therefore, it is suitable for solving any problem using the programming paradigm convenient for you.

The most commonly used OOP. C++ itself has a core of numerous libraries and additional functions or methods, so if you are going to understand everything fundamentally, you should start by learning the language from scratch.

Arduino programming features

It is the language on which the system is based that is the main feature of Arduino programming. After all, despite the fact that the board itself and work with it are quite simple, with a low threshold of entry, it will take several years to master a low-level programming language and perfectly master it.

Arduino programming has both advantages and disadvantages, and you should study both sides of the question to understand what you are dealing with and what to expect from the microcontroller in principle, while working with it. Among the advantages of Arduino, users mark:

  1. Low threshold of entry. This item will be in flaws, as due to the simplicity of the system and the lack of requirements for the basis of programming in the network walks many libraries written in a terrible way. It will take more time to figure out how they work than to create their own. And standard functions from developers are not enough for serious tasks.
  2. Extensive community. This is the main advantage of Arduino in front of its competitors, because you will find users who create projects on it, both Russian-speaking and English-speaking. But if you want to get really valuable tips and immerse yourself in the work of the community, you should still learn English. Since most of the problems that you will meet, have long been solved in Google, but, often, the answers are in English.
  3. A large number of libraries for different cases. But, as already described above, it has its drawbacks.

There is a programming Arduino and a number of significant drawbacks:

  1. The low entry level turns most of the libraries, with which the network is filled, into completely useless garbage. After all, some of them work just slowly and are written without any knowledge of the fundamentals of algorithmization, and some do not work at all, and it is not clear why the authors created them. To find support for a specific project, it is necessary to shovel a few English-language forums or create functions from scratch on their own.
  2. The complexity of programming in C ++. In fact, it is one of the most complex multi-paradigm programming languages ​​for creating firmware and low-level tasks. However, if you have had experience with it and know at least the basic algorithms, as well as working with at least one other multi-paradigm PL, especially using object-oriented programming, it will be much easier for you to get used to it.
  3. Low response speed of the chips themselves and their weak characteristics. Yes, Arduino microcontrollers can be customized for a specific task, re-buy components and sensors, but this plays a cruel joke with them. Since the developers do not know why they will use their offspring, they average all indicators to significantly reduce the cost of the final product. As a result, people who create simple crafts, overpay for unnecessary power, and those who are engaged in robotics or automation of some processes, have to re-buy and solder many additional fees.

As you can see, Arduino has many nuances, and is not as friendly for beginners as it looks at first glance. On the other hand, if you have the slightest experience with programming languages, it will be much easier for you to get comfortable.

How to start using Arduino correctly

If you have never programmed before, and this is your first experience, then the Arduino microcontroller programming will be much easier if you start with the basics. Of course, when you have no complicated projects in your plans, you can work on ready-made libraries and at the same time disassemble what their functions consist of. This is one of the good ways to learn, but then you should look for sets of functions that were written by professionals to be sure that they are correct. Otherwise, you may see the wrong solution to the problem and, as a result, apply those in your projects.

But where better to start with the basics and devote at least a week to the development of algorithmization and learn how to break their projects into blocks, and those - into concrete steps. Such a construction of flowcharts will be useful for you in the future. When you learn the whole basis, you can move on to practice and self-study in C ++, any simplest projects or tasks prepared on the Internet will do. At this stage, your goal will be to understand the basic paradigms and learn how to use them, as well as explore the possibilities of the language, so that you clearly know what it can and can sensibly evaluate the feasibility of your projects.

Programming Arduino Microcontrollers

Arduino programming itself is divided into three stages:

  1. Create or download a complete library of functions.
  2. Download these libraries to the permanent memory of the chip. This is also called firmware.
  3. Enter these functions in the command line, for example, AT, so that the board performs certain actions.

If you are doing something simple, and you have enough basic firmware, you can skip the first two points.

The most simple projects using Arduino

Examples of simple projects with Arduino are many, for example, you can:

  1. Create a light sensor that will adjust the special LED lamps to the brightness that is needed in the room.
  2. Automate any things in your home. For example, on-off lights, door openings, and more.
  3. Automation greenhouse.

Although it sounds scary enough, for most of these projects, thanks to the abundance of information on them on the Internet, you will not spend a lot of time and effort.