Basic Arduino Programming

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. In this tutorial, we will learn about the Arduino software program, step by step, and how we can write the program without any syntax or compilation error.

Let us start with the Structure. Software structure consist of two main functions −

  • Setup( ) function
  • Loop( ) function
Structure
Void setup ( ) {

}
  • PURPOSE − The setup() function is called when a sketch starts. 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.
  • INPUT − –
  • OUTPUT − –
  • RETURN − –
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 your program to change and respond. Use it to actively control the Arduino board.
  • INPUT − –
  • OUTPUT − –
  • RETURN − –

Data types in C refers to an extensive system used for declaring variables or functions of different 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 you will use during Arduino programming.

voidBooleancharUnsigned charbyteintUnsigned intword
longUnsigned longshortfloatdoublearrayString-char arrayString-object

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 true

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
ASCII Char Table

unsigned char

Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255.

Example

Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y

byte

A byte stores an 8-bit unsigned number, from 0 to 255.

Example

byte m = 25 ;//declaration of variable with type byte and initialize it with 25

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).

Example

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).

Example

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.

Example

float num = 1.352;//declaration of variable with type float and initialize it with 1.352

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.

Example

double num = 45.352 ;// declaration of variable with type double and initialize  

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 −
Arithmetic Operators
Comparison Operators
Boolean Operators
Bitwise Operators
Compound Operators
Arithmetic Operators
Assume variable A holds 10 and variable B holds 20 then −
Show Example
Operator name
Operator simple
Description
Example
assignment operator
=
Stores the value to the right of the equal sign in the variable to the left of the equal sign.
A = B
addition
+
Adds two operands
A + B will give 30
subtraction
-
Subtracts second operand from the first
A - B will give -10
multiplication
*
Multiply both operands
A * B will give 200
division
/
Divide numerator by denominator
B / A will give 2
modulo
%
Modulus Operator and remainder of after an integer division
B % A will give 0
Comparison Operators
Assume variable A holds 10 and variable B holds 20 then −
Show Example
Operator name
Operator simple
Description
Example
equal to
==
Checks if the value of two operands is equal or not, if yes then condition becomes true.
(A == B) is not true
not equal to
!=
Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.
(A != B) is true
less than
<
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(A < B) is true
greater than
>
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
(A > B) is not true
less than or equal to
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
(A <= B) is true
greater than or equal to
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
(A >= B) is not true
Boolean Operators
Assume variable A holds 10 and variable B holds 20 then −
Show Example
Operator name
Operator simple
Description
Example
and
&&
Called Logical AND operator. If both the operands are non-zero then then condition becomes true.
(A && B) is true
or
||
Called Logical OR Operator. If any of the two operands is non-zero then then condition becomes true.
(A || B) is true
not
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is false
Bitwise Operators
Assume variable A holds 60 and variable B holds 13 then −
Show Example
Operator name
Operator simple
Description
Example
and
&
Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) will give 12 which is 0000 1100
or
|
Binary OR Operator copies a bit if it exists in either operand
(A | B) will give 61 which is 0011 1101
xor
^
Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) will give 49 which is 0011 0001
not
~
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
(~A ) will give -60 which is 1100 0011
shift left
<<
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 will give 240 which is 1111 0000
shift right
>>
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
A >> 2 will give 15 which is 0000 1111
Compound Operators
Assume variable A holds 10 and variable B holds 20 then −
Show Example
Operator name
Operator simple
Description
Example
increment
++
Increment operator, increases integer value by one
A++ will give 11
decrement
--
Decrement operator, decreases integer value by one
A-- will give 9
compound addition
+=
Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand
B += A is equivalent to B = B+ A
compound subtraction
-=
Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand
B -= A is equivalent to B = B - A
compound multiplication
*=
Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand
B*= A is equivalent to B = B* A
compound division
/=
Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand
B /= A is equivalent to B = B / A
compound modulo
%=
Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand
B %= A is equivalent to B = B % A
compound bitwise or
|=
bitwise inclusive OR and assignment operator
A |= 2 is same as A = A | 2
compound bitwise and
&=
Bitwise AND assignment operator
A &= 2 is same as A = A & 2

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 −
Decision MakingControl Statements are elements in Source Code that control the flow of program execution. They are −
S.NO.
Control Statement & Description
1 If statement
It takes an expression in parenthesis and a statement or block of statements. If the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
2 If …else statement
An if statement can be followed by an optional else statement, which executes when the expression is false.
3 If…else if …else statement
The if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
4 switch case statement
Similar to the if statements, switch...case controls the flow of programs by allowing the programmers to specify different codes that should be executed in various conditions.
5 Conditional Operator ? :
The conditional operator ? : is the only ternary operator in C.

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 −
Loop ArchitectureC programming language provides the following types of loops to handle looping requirements.
S.NO.
Loop & Description
1 while loop
while loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit.
2 do…while loop
The do…while loop is similar to the while loop. In the while loop, the loop-continuation condition is tested at the beginning of the loop before performed the body of the loop.
3 for loop
A for loop executes statements a predetermined number of times. The control expression for the loop is initialized, tested and manipulated entirely within the for loop parentheses.
4 Nested Loop
C language allows you to use one loop inside another loop. The following example illustrates the concept.
5 Infinite loop
It is the loop having no terminating condition, so the loop becomes infinite.
(0)