Interaction Design WikiElectrical Engineering

C++ (Arduino) Variables

Arduino is based C++, which has a number of variable types available to us, and some special variables tricks that can be a little confusing but also very helpful when programming microcontrollers.

The Rules For Declaring Variable:

  1. Variable names only contain letters, digits, and underscores

  2. Variable names are case sensitive

  3. Variable names do not contain any whitespace and special characters (for example: #, *, % etc).

  4. Variable names must begin with a letter or an underscore.

  5. We can't use Arduino or C++ keywords as a variable name (for example: void, setup, int)

Arduino Variables

int (2 bytes on uno and other ATMEGA based boards, 4 bytes on SAMD boards)
A whole number between -32 768 and the max value is +32 767.
I.e. int x = 22;

float (4 bytes)
Floating Point number between 3.4028235E+38 to -3.4028235E+38 
I.e. float y = 1.234;

char (1 byte)
A char simply stores a value between -128 and +127, but it’s normally used to represent a character 
I.e. char z = “a”;

String (many bytes)
A collection of characters. The String is a class, not a primitive data type and it’s specific to Arduino. It provides a simpler alternative to creating an array of chars, and provides some extra functionality
I.e. String testString = “Arduino”;

boolean/bool (1 bit)
A binary variable, that stores either a 1 or a 0. You can can use either boolean or bool.

The 1 or 0 are typical represented as true and false.
I.e. bool state = false;

Unsigned modifier.

Some variables can be combined with the unsigned modifier. An unsigned value means that it no longer has negative or positive indicator. For example, and unsigned int can have a value between 0 and have a max value of 65 535

I.e. Unsigned Int x = 65 500;

Additional C++ Variables

byte (1 byte) A single byte of storage i.e a value between -128 and +127,

long (4 to 8 bytes) Like an int, but with twice as much storage
double (4 to 8 bytes) Like a float, but with twice as much storage