01 April 2010

Arduino KEYWORDS(4)


Arrays

An array is a collection of variables
that are accessed with an index number.
Arrays in the C programming language,
on which Arduino is based, can be complicated,
but using simple arrays is relatively straightforward.

Creating (Declaring) an Array

All of the methods below are valid ways to create (declare) an array.

  int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";

You can declare an array without initializing it as in myInts.

In myPins we declare an array without explicitly choosing a size.
The compiler counts the elements and creates an array of the appropriate size.

Finally you can both initialize and size your array, as in mySensVals.
Note that when declaring an array of type char,
one more element than your initialization is required,
to hold the required null character.

Accessing an Array

Arrays are zero indexed, that is, referring to the array initialization above,
the first element of the array is at index 0,
hence


mySensVals[0] == 2, mySensVals[1] == 4,
and so forth.

It also means that in an array with ten elements, index nine is the last element.
Hence:

int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains random information
// (other memory address)

For this reason you should be careful in accessing arrays.
Accessing past the end of an array
(using an index number greater than your declared array size - 1)
is reading from memory that is in use for other purposes.
Reading from these locations is probably not going to do much except yield invalid data.
Writing to random memory locations is definitely a bad idea
and can often lead to unhappy results such as crashes or program malfunction.
This can also be a difficult bug to track down.

Unlike BASIC or JAVA, the C compiler does no checking to see
if array access is within legal bounds of the array size that you have declared.

To assign a value to an array:

mySensVals[0] = 10;

To retrieve a value from an array:

x = mySensVals[4];

Arrays and FOR Loops

Arrays are often manipulated inside for loops,
where the loop counter is used as the index for each array element.
For example, to print the elements of an array over the serial port,
you could do something like this:

int i;
for (i = 0; i < i =" i">

Example
For a complete program that demonstrates the use of arrays,
see the Knight Rider example from the Tutorials.



Integer Constants

Integer constants are numbers used directly in a sketch, like 123.
By default, these numbers are treated as int's
but you can change this with the U and L modifiers (see below).

Normally, integer constants are treated as base 10 (decimal) integers,
but special notation (formatters) may be used to enter numbers in other bases.

Base               Example    Formatter        Comment

10 (decimal) 123 none

2 (binary) B1111011 leading 'B' only works with 8 bit values (0 to 255)
characters 0-1 valid

8 (octal) 0173 leading "0" characters 0-7 valid

16 (hexadecimal) 0x7B leading "0x" characters 0-9, A-F, a-f valid

Decimal is base 10.
This is the common-sense math with which you are acquainted.
Constants without other prefixes are assumed to be in decimal format.

Example:
101     // same as 101 decimal   ((1 * 10^2) + (0 * 10^1) + 1)


Binary is base two.
Only characters 0 and 1 are valid.

Example:
B101    // same as 5 decimal   ((1 * 2^2) + (0 * 2^1) + 1)

The binary formatter only works on bytes (8 bits)
between 0 (B0) and 255 (B11111111).
If it is convenient to input an int (16 bits) in binary form
you can do it a two-step procedure such as:

myInt = (B11001100 * 256) + B10101010;    // B11001100 is the high byte

Octal is base eight.
Only characters 0 through 7 are valid.
Octal values are indicated by the prefix "0"

Example:

0101    // same as 65 decimal   ((1 * 8^2) + (0 * 8^1) + 1)
Warning
It is possible to generate a hard-to-find bug by (unintentionally)
including a leading zero before a constant
and having the compiler unintentionally interpret your constant
as octal.


Hexadecimal (or hex) is base sixteen.
Valid characters are 0 through 9 and letters A through F;
A has the value 10, B is 11, up to F, which is 15.
Hex values are indicated by the prefix "0x".
Note that A-F may be typed in upper or lower case (a-f).

Example:

0x101   // same as 257 decimal   ((1 * 16^2) + (0 * 16^1) + 1)

U & L formatters

By default, an integer constant is treated as an int
with the attendant limitations in values.
To specify an integer constant with another data type,
follow it with:

  • a 'u' or 'U' to force the constant into an unsigned data format.
    Example: 33u
  • a 'l' or 'L' to force the constant into a long data format.
    Example: 100000L
  • a 'ul' or 'UL' to force the constant into an unsigned long constant.
    Example: 32767ul

1 comment: