Vous êtes sur la page 1sur 11

Arrays

Code Lesson #2

Wednesday, February 15, 2012

Arrays = a list of values


A variable stores only one value; for example: int delayTime = 250 An array can store a list of values. Last week we used an array in our script toneMelody. The array was named melody and contained seven values, which were musical notes the frequency for which the program retrieves from the pitches.h le: int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

Wednesday, February 15, 2012

How to Set Up an Array


As with a simple variable, you indicate what kind of variable it is and give it a name: int melody Then you add open and closed brackets to indicate its an array, followed by an opening curly bracket: int melody [ ] { Then you list the values, in a specic order, separated by a comma: int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4 } ;

Be careful to nish with the closing curly bracket and the usual semi-colon!

Wednesday, February 15, 2012

Getting Information from an Array


You retrieve values from an array based on that values position in the array. Is it the rst item? the fourth item? etc. BUT its tricky, because Arduino follows the C programming language by starting to count from zero. Therefore, the third item in an array would actually be #2 when youre trying to retrieve it! Thus if you had a speaker attached to pin 8 and wanted to play the note G3 that rst appears as the second value in our melody array, you would write: tone (8, melody[1]); This tells Arduino to use pin 8 to play the second note in the melody array. Notice that a value can appear more than once in an array. Why? Because this will enable us to easily create sequences, such as when we play musical notes, some of which repeat or perhaps a series of blinking lights!

Wednesday, February 15, 2012

Arrays & Repeat Loops


We introduced the for control structure on the last page of our rst coding lesson. The basic idea is to create a repeat cycle that will do the same thing for a list of numbered items, be it a list of LEDs or a list of musical notes. Thus it should make sense that a for repeat loop and an array are often used together to create a sequence. Heres the basic structure of a for statement, taken straight from the Arduino reference guide:

Wednesday, February 15, 2012

Translation!
for (int x = 0; x <100; x++) What this does is: 1. Create a variable named x with an initial value of 0 (zero) 2. Sets up a condition for an action that will keep repeating so long as x is less than 100; in other words, it will stop when x = 99. 3. Each time it loops through this statement, it will add 1 (one) to the value of x. Remember that x++ is just a shortcut for x = x +1. println (x); What this does is print the value of x to the serial monitor. The result would look like this: 0 1 2 3 4... all the way up to 99

Wednesday, February 15, 2012

Example of Repeat Loop + Array


If we go back to our toneMelody script we can see an example of an array being used within a for statement repeat loop. First we have our array with our eight notes: int melody[ ] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; Because the script uses eight notes, it sets up a variable called thisNote that it can repeat 8 times: for (int thisNote = 0; thisNote < 8; thisNote++) Notice that it is counting from 0 to 7, which gives us eight numbers. To play the tune, it must play note 0, then note 1, then note 2, etc. tone(8, melody[thisNote],noteDuration); So what happens? Arduino cycles through this script until thisNote equals 7, but each time it is calling a dierent note from the array. But what is noteDuration? Look at the original toneMelody script and see if you can gure it out. Youll nd another array that lists the duration of each note in sequence, and they are called as part of the same for statement.

Wednesday, February 15, 2012

Practice #1
Write the code that will create an array named ledPins that will access, in the following order, these pins: 0, 1, 2, 3, 2, 4, 2, 1, 3, 0, and 4. Answer on next screen!

Wednesday, February 15, 2012

Answer #1

int ledPins [ ] = {

0, 1, 2, 3, 2, 4, 2, 1, 3, 0, 4} ;

Wednesday, February 15, 2012

Practice #2

Write a line of code that turns on LED pin #3 the rst time it appears in this sequence. Answer on next screen!

Wednesday, February 15, 2012

Answer #2

digitalWrite (ledPins[4], HIGH);

Wednesday, February 15, 2012

Vous aimerez peut-être aussi