Wednesday, May 15, 2013

On Using Arrays in the Arduino IDE

Overview
Arrays are very useful structures that can be used in Arduino. An array is like a group of variables that are all addressed using a coming name. Arrays are useful when dealing with data that needs to be accessed and changed in a systematic way.

When comparing arrays to variables, both can be considered to be like a box. A variable is one box with a name on it. You can put one thing into the box. And you can replace whatever is in the box with something else. An array is like a box with many compartments. The box still has only one name, but the compartments are all numbered...




Creating arrays
Whenever we create a variable, we assign a data type and name and optionally a value to that data type e.g.

byte data_value = 10;

byte input_value;

This is great for single values, but is poor for groups of values or values that are related to each other in some way. We can use arrays, which are basically “groups” of variable. We can think about arrays as being a variable with variables inside of it.

An array can have any data type e.g. int, float, byte. An array can only have one type of data for every variable that is stored inside of it. An array has a set length of values i.e. the number of variables that are stored inside of it.

An array is designated by the square bracket characters '[' and ']'. To create an array, we need to give it a data type, a name, and a predefined length OR data contents e.g.

byte data_values[ ] = {21, 35, 52, 11, 89};

byte input_values[20];

The array “data_values” contains five numbers (21, 25, 52, 11 and 89). The array 'input_values' contains no data (i.e. all zeroes) but has space for 20 bytes.

Arrays should be created at the start of the code, when defining other variables. Each time an array is created, the equivalent amount of RAM is used. This is because creating an array is literally setting aside that amount of space in RAM for the array. Teensy and Arduino have around 2KB of RAM

Each data point in an array is called an “element”. For example in the array:

byte data_values[ ] = {21, 35, 52, 11, 89};

Each of the numbers 21, 35, 52, 11, 89 is an element of the array data_values.

An array is indexed – every element in the array has an index number, starting at index 0 for the very first element in an array.

For example in the array:

byte data_values[ ] = {21, 35, 52, 11, 89};

Element 0 currently holds the value 21. Element 1 currently holds the value 35. Element 2 currently holds the value 52 and so on. To access an element of an array, we can use the index to access that element of the array.

To write to an array element variable:
array_name[index] = x;

To read from an array element:
y = array_name[index];

For example, consider the following array:
byte data_values[ ] = {21, 35, 52, 11, 89};

To write sometime to element 1 (which currently holds the value 21):
data_values[0] = 23; // now element 1 holds 23

To read element 1 to a variable:
data = data_values[0];
// now we have retrieved element 1 to variable  “data”




Examples

Array example: Writing waveform data to a DAC.


Array example: Reading 4 buttons and sending MIDI notes for each button. 

1 comments:

Unknown said...

Thank you. Greetings from Mexico!!!!