ARDUINO – VARIABLES & CONSTANTS

https://www.tutorialspoint.com/arduino/arduino_variables_and_constants.htm

Copyright © tutorialspoint.com

 

Advertisements

Before we start explaining the variable types, a very important subject we need to make sure, you fully understand is called the variable scope.

What is 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 −

  • Inside a function or a block, which is called local variables.
  • In the definition of function parameters, which is called formal parameters.
  • 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;
   y = 0; actual initialization
   z = 10;
}

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.

The following example uses global and local variables −

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 – DATA TYPES

 

https://www.tutorialspoint.com/arduino/arduino_data_types.htm

Copyright © tutorialspoint.com

 

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.

void Boolean char Unsigned char byte int Unsigned int word
long Unsigned long short float double array String-char array String-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 2byte2−byte value. This yields a range of -32,768 to 32,767 minimumvalueof215andamaximumvalueof(215minimumvalueof−215andamaximumvalueof(215 – 1).

The int size varies from board to board. On the Arduino Due, for example, an int stores a 32-bit 4byte4−byte value. This yields a range of -2,147,483,648 to 2,147,483,647 minimumvalueof231andamaximumvalueof(231minimumvalueof−231andamaximumvalueof(231 – 1).

Example

int counter = 32 ;// declaration of variable with type int and initialize it with 32

Unsigned int

Unsigned ints unsignedintegersunsignedintegers 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 216216 – 1). The Due stores a 4 byte 32bit32−bit value, ranging from 0 to 4,294,967,295 2321232−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 4bytes4bytes, 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 4bytes4bytes. Unlike standard longs, unsigned longs will not store negative numbers, making their range from 0 to 4,294,967,295 2321232−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 ATMegaandARMbasedATMegaandARMbased, a short stores a 16-bit 2byte2−byte value. This yields a range of -32,768 to 32,767 minimumvalueof215andamaximumvalueof(215minimumvalueof−215andamaximumvalueof(215 – 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 4bytes4bytes 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 64bit64bit precision.

Example

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