Arduino Workshop - Chapter 4 - Using Arrays

Sharing buttons:


so far in all of the examples that

you've created or looked up variables

have been used in fairly simple ways

well we're going to take a look at

another way to use variables called

arrays arrays are a data structure which

allows multiple values to be grouped

together in an easily accessible method

if you can remember back to chapter two

we talked about variables and how they

can be thought of as containers sitting

on a shelf well to extend that analogy

if a variable is a single container then

think of an array as a subdivided set of

drawers on a shelf you can still access

that space on the shelf but rather than

being a single container there are

different miniature containers at that

space an array is structured like so

let's take a look so in the Arduino IDE

and there are four key elements to an

array you have the array data type in

this example we're using int much the

same as we with another variable we have

array1 that could be called anything

could be called Sydney doesn't have to

have the word Rainer it's just a unique

name in the same way that a variable has

a unique name it has the array index

just there and then it has the array

content which we have here inside of our

curly bracket and each element is

separated by commas there are four

unique elements or pieces of data inside

there each of which isn't it now you can

create different types of arrays however

an array can only hold a single type of

data so if you declare an array as an

int then it can only hold integers like

the example data now like variables

arrays can be declared first and defined

later which allows you to create an

array with blank spots in it and add

data to it later on in your program the

array name follows the same conventions

as a standard variable it must be unique

it is case sensitive and has a scope

global or local the index refers to the

position of the array context when

declaring an array you can define the

number of elements or places if you wish

that you have in your array and finally

the array contents is the data that is

inside your array

so let's take a look at some examples of

how you can declare and define arrays so

adding on from the example here I've

created for different types of arrays we

have array1 in which we said that we

have four elements to the array and

inside we have these four pieces of data

11 5 4 and 9 now we have a raid 2 in

which we've left the index blank so

we're not explicitly telling it how many

elements are in our array but you can

see that that has 9 elements here in

fact we can add a zero to that 0 index

it all in fact with 10 to the under

there so the example a little better for

1 through to 10 so there are 10 elements

in this array and once it goes through

that the compiler will automatically

sort out how many elements are in there

and populate that yourself so you don't

always have to define number of elements

in a write a little bit more efficient

now here we've created in a race 3 we've

said that there are 10 elements in the

array and we've left off the content now

that is the same concept if we're

declaring an integer just and a variable

we could go array a sum value and

instead of being letting it equal

something we're just declaring and

defining it later on where we just leave

out the equal signs and the value and

instead just put that semicolon there to

terminate the one well in the same way

we've said we've left out the equal

signs and the curly brackets in the

content and it will populate an array

with ten blank spaces in which we can go

and fill in later on

now arrays don't just have to be in here

we've got a child class or a character

where it uses the ASCII table and it

creates character type elements and the

character is 8 bits or one byte of data

we said that there are six elements in

the array we have h-e-l-l-o now you'll

notice that's only 5 we've left space

because there is actually a terminating

character in that array there are six

elements to it those arrays don't just

have to be in now to access an array you

use its name and the index number of the

element that you wish to access

something to keep in mind though is that

as this many things in programming

arrays as 0 index

this means that the first element in an

array is indexed as number zero and the

second element as index one and so on

and so forth

whilst it can definitely be a bit

confusing at times it makes a lot of

sense once you start getting used to it

so let's take a look at another example

let's say in void loop I wish to access

part of an array

well we think as we said standard

variable you don't have to use the data

pipe again you simply use the array name

so let's take array 1 then inside my

square bracket here is where I use the

index to reference a particular element

in the array so if I say array 1 0 that

means I'm accessing that 0 index of the

very first element in the array index

number 0 which is 11 so array one

element 0 would equal 11 likewise if it

is index 1 that is the second space in

the array which is 5 2 would be 4 and 3

would be 9 and you have 0 1 2 3 which is

4 elements in total but if it's

confusing as I said but you do get used

to it likewise I can access even though

put the word hello there in my array I

can actually access each individual

letter so rather than taking a string

for example and saying all right string

some word equal to load and I've only

got access to the string as a whole

without some extra string manipulation

and data control I can simply say

alright I want to find out what the

second letter is in this array and I

know that it's an 8 or I could leave

that blank and fill in another word

later on and then break it apart in the

same method which is really really cool

so a really handy use in arrays that

you'll see later on is we can create a

for loop so we create a for loop

standard counting loop into I and let's

say I wanted to use array 2 as for

output pins and I want pins 1 through 2

tend to be output pins let's say while I

is less than 10 I plus plus a standard

incrementing for loop I could then use

pin mode array

- and you might index put the local

variable hi

and that's really really cool anyway

we'll get to that in a second and we'll

take a look at a more holistic example

to re explain how that works in just a

moment so now that you know a bit about

arrays and how they work

let's put them to good use in a

practical example to make an LED

sequencer using your knowledge of LEDs

wire up eight LEDs to your Arduino board

using whatever things you choose however

I use pins 2 through to nine so eight

LEDs all up and we're going to make them

do some cool things so I've got my board

here fairly simple I put my LEDs with a

resistor going to ground from them so

I'm using a high logic or active higher

logic I should say and then I'm taking

my ground to my Arduino board and the

pin out directly to the LEDs when we

apply five five volt signal to them

they're going to turn on which is cool

so here's a sketch and it's fairly


we've got an array here called LED array

and I'm going to use these for the pin

out to reference which led I'm

controlling so the array contents are to

sequentially through to nine which

corresponds to the pins I'm using pins

two through to nine then delay time

which I'm just using as a counter for

how rapidly we want the LED to sequence

then as I mentioned before inside setup

I'm using this for loop I'm using pin

modes with the array indexes of I and

I'm declaring them all as outputs now

what this does let's go through through

the basics of this because it's a really

good example of using arrays so at first

for this for loop if you know from the

previous chapter when we're talking

about logic statements I would create a

variable and we've initialized is 0 the

first time this runs I is equal to 0 so

what a third is King mode LED array 0

which corresponds to the first element

in our array so 2 now LED array is 0 is

equal to 2 so it would put a 2 or

actually a 2 in place of that and then

it would just define 2 as an output

likewise when when that's finished I

will increment again to 1 now LED array

1 is equal

to second element there which is three

so order replace all of that with a

three and so on and so forth until that

all is done which is really cool because

instead of writing our pinmode output

eight times which is really clunky and

bulky for lines which like add two lines

of code and two spaces for our curly

brackets gets the same thing done and

it's really handy because if we want to

increase the size of a la zero we can

simply add some more elements to it

change the changes of bounding of our

for loop and then we can declare as many

as we want just really cool so that is

our pin modes done now we've got two

separate for loops here really really

simple all we're doing is we're going to

come LEDs on in order until they're all

on so I'm going to turn them off again

in the same order and can play around

with the exact animation really easily

by altering the flow of our four loops

first of all appear again really

expanded counting we're saying that's

inside which is initialized to zero

while I is less than or equal to seven

again it could be in fact you know but

to make it easier you could say while I

is less than eight going to accomplish

the same thing increment I've spent

accounting we're going to write LED

array I so I referencing each pin and

writing at I writing I which is going to

be an order from element 0 1 2 3 4 5 6 7

which is 8 elements alright remember

zero indexing which corresponds to 2 3 4

5 6 7 8 which is the LED pins we're

going to turn them all 1 and wait for

delay time which is equal to 150

milliseconds each time so there's a time

for them to step up an increment then

when it's done that we're going to turn

them back off and wipe back down in the

same way using the exact same concept

but we're initializing I 7 running a

while I is greater than or equal to 0

and B incrementing I each time which is

really cool so let's go ahead and load

this up to our board plug that guy in

short got the right comfort selected and

hit upward and you can see it's actually

already running a headed program on

there just a moment ago you can see the

LEDs turning on and off in order really

really handy now again you could

implement a different slightly different

animation we'll just copy this

place to full up here and then instead

of turning it on we're going to turn it

off so what this will do is turn them

all on and then all I can do a do a

linear work which is really really cool

they're both still leaning out but up

goes in the same direction so you can

see that it's all turning on then they

start wiping off really cool I raise a

really simple but really powerful tools

and that they're really good especially

for controlling outputs where you might

have a whole group of LEDs now eight

LEDs doesn't sound like much but let's

say you wanted to control a 64 or an 8x8

LED matrix well you can use arrays and

rather than having to declare each pin

by itself and the pin mode and accessing

each one by cross-referencing which pin

number you're going to you simply create

it as an array and access each element

that you want so Row one could be

element 0 through 7 then Row 2 is

element 8 through 16 row 3 16 some one

and so forth and you have a really

logical easy to organized way of having

all your pin modes and your where your

pin outputs clumps together really

really cool stuff