Documentation - Arduino Programming Language
Documentation - Arduino Programming Language
The Arduino software is open-source. The source code for the Java environment is released
under the GPL and the C/C++ microcontroller libraries are under the LGPL.
Sketch: The first new terminology is the Arduino program called “sketch”.
Structure
Arduino programs can be divided in three main parts: Structure, Values (variables and
constants), and Functions.
Software structure consist of two main functions:
1) Setup( ) function
2) Loop( ) function
1) Syntax-
Void setup ( )
{
PURPOSE: The setup() function is called when a sketch starts. We can use it to initialize the
variables, pin modes, start using libraries, etc. The setup function will only run once, after each
power up or reset of the Arduino board.
2) Syntax-
Void Loop ( )
{
PURPOSE: After creating a setup() function, which initializes and sets the initial values, the
loop() function does precisely what its name suggests, and loops consecutively, allowing the
program to change and respond.
Arduino Datatypes
Data types in C refers to an extensive system used for declaring variables or functions of
diverse types. The type of a variable determines how much space it occupies in the storage
and how the bit pattern stored is interpreted.
The following table provides all the data types that we use during Arduino programming.
Unsigned
Unsigned String-char
Void
The void keyword is used only in function declarations. It indicates that the function is
expected to return no information to the function from which it was called.
Example-
Void Loop ( )
{
// rest of the code
}
Boolean
A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte
of memory.
Example-
boolean val = false ; // declaration of variable with type boolean and initialize it with false
boolean state = true ; // declaration of variable with type boolean and initialize it with
false
Char
A data type that takes up one byte of memory that stores a character value. Character literals
are written in single quotes like this: 'A' and for multiple characters, strings use double quotes:
"ABC".
However, characters are stored as numbers. You can see the specific encoding in the ASCII
chart. This means that it is possible to do arithmetic operations on characters, in which the
ASCII value of the character is used. For example, 'A' + 1 has the value 66, since the ASCII
value of the capital letter A is 65.
Example-
Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it
with character a
Char chr_c = 97 ;//declaration of variable with type char and initialize it
with character 97
Byte
A byte stores an 8-bit unsigned number, from 0 to 255.
Example-
Int
Integers are the primary data-type for number storage. int stores a 16-bit (2-byte) value. This
yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15)
- 1).
The int size varies from board to board. On the Arduino Due, for example, an int stores a 32-
bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of
-2^31 and a maximum value of (2^31) - 1).
Example-
int counter = 32 ;// declaration of variable with type int and initialize it with 32
Unsigned int
Unsigned ints (unsigned integers) are the same as int in the way that they store a 2 byte value.
Instead of storing negative numbers, however, they only store positive values, yielding a useful
range of 0 to 65,535 (2^16) - 1). The Due stores a 4 byte (32-bit) value, ranging from 0 to
4,294,967,295 (2^32 - 1).
Example-
Unsigned int counter= 60 ; // declaration of variable with type unsigned int and initialize it
with 60
Word
On the Uno and other ATMEGA based boards, a word stores a 16-bit unsigned number. On
the Due and Zero, it stores a 32-bit unsigned number.
Example-
word w = 1000 ;//declaration of variable with type word and initialize it with 1000
Long
Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from
2,147,483,648 to 2,147,483,647.
Example-
Long velocity= 102346 ;//declaration of variable with type Long and initialize
it with 102346
Unsigned long
Unsigned long variables are extended size variables for number storage and store 32 bits (4
bytes). Unlike standard longs, unsigned longs will not store negative numbers, making their
range from 0 to 4,294,967,295 (2^32 - 1).
Unsigned Long velocity = 101006 ;// declaration of variable with type Unsigned Long and
initialize it with 101006
Short
A short is a 16-bit data-type. On all Arduinos (ATMega and ARM based), a short stores a 16-
bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a
maximum value of (2^15) - 1).
short val= 13 ;//declaration of variable with type short and initialize it with 13
Float
Data type for floating-point number is a number that has a decimal point. Floating-point
numbers are often used to approximate the analog and continuous values because they have
greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38
and as low as 3.4028235E+38. They are stored as 32 bits (4 bytes) of information.
float num = 1.352;//declaration of variable with type float and initialize it with 1.35
Double
On the Uno and other ATMEGA based boards, Double precision floating-point number
occupies four bytes. That is, the double implementation is exactly the same as the float, with
no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision.
double num = 45.352 ;// declaration of variable with type double and initialize
it with 45.352
Variable Scope
Variables in C programming language, which Arduino uses, have a property called scope. A
scope is a region of the program and there are three places where variables can be declared.
They are:
1) Inside a function or a block, which is called local variables.
2) In the definition of function parameters, which is called formal parameters.
3) Outside of all functions, which is called global variables.
Local Variables
Variables that are declared inside a function or block are local variables. They can be used only by the statements that
are inside that function or block of code. Local variables are not known to function outside their own. Following is the
example using local variables:
Void setup ()
{
}
Void loop ()
{
int x,y;
int z; Local variable declaration
x= 0;
Global Variables
Global variables are defined outside of all the functions, usually at the top of the program.
The global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for
use throughout your entire program after its declaration.
Int T , S ;
float c =0 ; Global variable declaration
Void setup ()
{
}
Void loop ()
{
int x,y;
int z ; Local variable declaration
x= 0;
y=0; actual initialization
z=10;
}
Arduino- Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators:
1) Arithmetic Operators
2) Comparison Operators
3) Boolean Operators
4) Bitwise Operators
5) Compound Operators
Arithmetic Operators
Assume variable A holds 10 and variable B holds 20 then -
Operator Operator Description Example
name simple
Example-
void loop ()
{
int a=9,b=4,c;
c=a+b;
c=a-b;
c=a*b;
c=a/b;
c=a%b;
}
Result-
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1
Arduino- Control Statements
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program. It should be along with a statement or statements to be
executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the
programming languages –
Control Statements are elements in Source Code that control the flow of program
execution. They are:
1)If statement
2)If …else statement
3)If…else if …else statement
4)Switch case statement
5)Conditional Operator
Arduino- Loops
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages –
C programming language provides the following types of loops to handle looping
requirements.
1) while loop
2) do…while loop
3) for loop
4) nested loop
5) infinite loop
Arduino- Functions
Functions allow structuring the programs in segments of code to perform individual tasks. The
typical case for creating a function is when one needs to perform the same action multiple times
in a program.
Standardizing code fragments into functions has several advantages:
1) Functions help the programmer stay organized. Often this helps to conceptualize
the program.
2) Functions codify one action in one place so that the function only has to be
thought about and debugged once.
3) This also reduces chances for errors in modification, if the code needs to be
changed.
4) Functions make the whole sketch smaller and more compact because sections of
code are reused many times.
5) They make it easier to reuse code in other programs by making it modular, and
using functions often makes the code more readable.
There are two required functions in an Arduino sketch or a program i.e. setup () and loop().
Other functions must be created outside the brackets of these two functions.
A function is declared outside any other functions, above or below the loop function.
We can declare the function in two different ways -
1. The first way is just writing the part of the function called a function prototype above the
loop function, which consists of:
1) Function return type
2) Function name
3) Function argument type, no need to write the argument name
Example-
The second method just declares the function above the loop function.
Arduino- Strings
Strings are used to store text. They can be used to display text on an LCD or in the Arduino
IDE Serial Monitor window. Strings are also useful for storing the user input. For example, the
characters that a user types on a keypad connected to the Arduino.
There are two types of strings in Arduino programming:
1) Arrays of characters, which are the same as the strings used in C programming.
The String class, part of the core as of version 0019, allows you
to use and manipulate strings of text in more complex ways
than character arrays do. You can concatenate Strings, append
to them, search for and replace substrings, and more. It takes
more memory than a simple character array, but it is also more
useful.
String()
For reference, character arrays are referred to as strings with
a small ‘s’, and instances of the String class are referred to as
Strings with a capital S. Note that constant strings, specified in
"double quotes" are treated as char arrays, not instances of the
String class
lastIndexOf()
backwards from a given index, allowing to locate all instances
of the character or String.
length()
does not include a trailing null character.)
replace()
replace to replace substrings of a string with a different
substring.
reserve()
memory for manipulating strings.
setCharAt()
the existing length of the String.
startsWith()
another String.
toFloat()
123.00, and 123.00 respectively. Note that "123.456" is
approximated with 123.46. Note too that floats have only 6-7
decimal digits of precision and that longer strings might be
truncated.
toLowerCase()
modifies the string in place rather than returning a new.
toUpperCase()
modifies the string in place rather than returning a new one.