There are several different types in the Arduino programming language. They each have different applications that can use up the limited RAM in the Arduino. When should i use each?
One should appreciate that the Arduino is an Embedded Processor. Specifically that it has limited resources of RAM and ROM(aka Flash/program space).
Often is the case I see coding use the "int" defaultly. Almost sloppily. If that works great.
BUT!!!! You can run out of room REAL FAST with only 2K of RAM.
I would recommend that you size down your type to the minimum of what it needs to be describing it. One also gets a better understanding of what it really is and its limitations. There is no need for an array of int(16bits) from bytes that were recieved from the SPI, UART or I2C. Or a matrix of RGB's that consume 3 bytes each, being int's. You cut your capacity in half.
The pro/con: PRO: using default int everywhere makes it more inter changeable. The CON is that such interchangeability can lead to unintentional casting errors.
I would also recommend using C99 type def's which can be found at "avr_stdint.html">http://www.nongnu.org/avr-libc/user-manual/group_avr_stdint.html" and elsewhere. It makes things vivid, regards to this aspect. Hence a "int" would be a "int16_t", "unsigned long" be "uint32_t" etc...
It is worth knowing that in some cases a byte or char can not be replaced with int8_t, as such when used with string array functions. But they are still just as vivid.
An overview of the most commonly used variables:
- byte: uses one byte (8 bits), holds numbers from 0 to 255
- char: a signed byte
- boolean: uses 1 byte, but only holds a value of
- int: uses 2 bytes, holds numbers from -32768 to 32767, mostly used in general programming. an
intis what is known as a signed variable, where the last bit controls if it is negative or positive
- long: uses 4 bytes, holds numbers from -2147483647 to 217283648, it is also signed
- long long: uses 8 bytes, can hold -9223372036854775808 to 9223372036854775807, signed
- float: uses 4 bytes, holds decimals from -3.4028235E+38 to 3.4028235E+38.
- double: almost identical to
- string: an array of chars that represent text
- String: not to be confused with
string, this is a class that holds text data with extra functions
Most variable declarations can be prefixed with
unsigned to change the function of the sigh bit. For example,
unsigned int x = 0; would create a variable that holds numbers from 0 to 65535 because the sign bit is used as an extra data bit.