This is the next part in our learning Arduino series, and this time we?ll be learning about and using Arrays to make a little Christmas tree ornament with various flashing sequences. This would be an ideal project to keep the kids occupied if you’d like to teach them basic soldering – just mount the LEDs on a piece of card, and you can get power from a standard 9v battery.

It’s also a key lesson in beginner Arduino programming, though if you don’t plan on actually using this as an ornament I’d strongly suggest breadboarding it out anyway.

Note: This is a very beginner level tutorial and we certainly won?t be breaking any new ground – it?s? just a device to teach the concepts of using Arrays and For loops to deal with a large number of LEDs (or other output devices).

If you haven?t already, now would be a good time to follow along with the other articles in the series:

For this project, you?ll need at least 8 or 9 LEDs in either red or green, a resistor for each of them, a breadboard and some hookup wires. The starter kit from Ooomlout, which I recently purchased myself and is pictured in this tutorial, offers great value for money and has more LEDs and resistors than you?ll ever need, as well as coming with a neat breadboard and Arduino case to keep things tidy.

Here?s the final thing:

And a video of it in action.

Here?s a view of the wiring from Fritzing. It?s very basic – just connect the positive lead of the LEDs to pins 2->whatever (up to pin 13), and connect the negative legs to the ground inline with a resistor. The value I?ve used here is 560 Ohms. That?s it for wiring.

On the software side, think about how you might write to all these LEDs in the code. You could go about it like this:

```int led1 = 2; // first LED on pin 2
int led2 = 3; // second on pin 3
// etc etc

void loop(){
digitalWrite(led1,HIGH);
delay(100);
digitalWrite(led1,LOW);
delay(100);
digitalWrite(led2,HIGH);
// etc
}```

You should be able to see that with 9 LEDs, this is will quickly get tiring. The answer lies with Arrays, which if you can?t remember our Programming 101 about basic datatypes Having introduced and talked a little about Object Oriented Programming before and where its namesake comes from, I thought it's time we go through the absolute basics of programming in a non-language specific way. This... Read More – are basically just lists.

The syntax looks like this (place this as the first line in your code):

`int leds[] = {2,3,4,5,6,7,8,9,10};`

The square brackets indicate that the ?leds? variable is going to be an Array. The curly braces enclose the list of pin numbers that our array will hold.

Now, to use an Array, we need to address it by the index number. The index starts at 0, and will always therefore go up to 1 less than the total number of things inside it (so with 9 items, the last one would have an index of 8).

You write it like this:

`leds[0]`

Which in our case, would fetch the number 2, because that?s what?s at index 0 in our array.

Following so far? Great. That alone isn?t enough for us though – we also need some way to iterate over each element of our LEDs array For that, we will use a for loop. The syntax to do that is like this:

`for(initial variable; condition under which we repeat again; change to variable each iteration)`

For example:

`for(int i = 0; i<9; i++)`

Which says

• start this loop with a variable, i, which has a value of zero
• continue looping only while i is less than 9?(so: 0,1,2,3,4,5,6,7,8)
• each time, add 1 to i (i++ is a short way of saying i = i+1)

So basically, the loop is going to be repeated as many times as we have LEDs, and each time it?s repeated we will have a variable, i, which we can use however we like.

We?ll be using this structure twice to start with. Once inside the setup function to make all our pins turn to output mode, like this:

```void setup(){
for(int i = 0;i< 9;i++){
pinMode(leds[i],OUTPUT);
}
}```

Can you see what we did there? Instead of writing 9 lines of code to declare each individual pin as output, we create a ‘for’ loop to repeat itself 9 times, each time setting another pin.

Now, you should be able to see how we could do the exact same thing in the main program loop to turn each LED on in sequence:

```void loop(){
for(int i = 0;i< 9;i++){
digitalWrite(leds[i],HIGH);
delay(100);
digitalWrite(leds[i],LOW);
}
}```

Try that. You can download the full code for today’s project from here if you?d rather not type it out again (though I encourage you to, as it helps the learning process).

Okay, so now we have a fairly boring lighting sequence. Let?s program another one. Just for fun, let?s make it completely random. Replace the main loop code with this:

```void loop(){
int randomLed = random(0,8);
digitalWrite(leds[randomLed],HIGH);
delay(50);
randomLed = random(0,8);
digitalWrite(leds[randomLed],LOW);
}```

Instead of using a ‘for’ loop to iterate over each LED, we pick a random number from 0-9 instead, and flash that on.

I’m going to leave it there for today as you should now be armed with enough knowledge to program all new sequences and experiment with loops. To prove how easy this all is, I challenged my wife to think of a sequence she?d like to see, and then got?her to program it herself, given only the code and lessons you?ve had so far. She came up with this, so see if you can match that for homework!

1. juan garcia
November 12, 2015 at 7:09 pm

Hello, I have a question. I m planning to make a tree out of LEDs too but I want to charlieplex them. Would it be possible to use this code on charlieplexed LEDs (I am new to coding so this question might not make sense)? My other question is would be possible to make multiple patterns on the arduino and make the arduino randomly choose between each pattern to make the LEDs light differently?

• James Bruce
November 13, 2015 at 9:35 am

You would need different code for Charlieplexing, since it relies on both the 1 signal from one pin, and the 0 signal from another. Essentially you'll be using a co-ordinate system to target the LEDs.

Assuming your patterns are short, you could store in an array, and have an array of arrays to store different patterns. And yes, then you can select from that array at a random index. However, try to achieve the same programmatically - don't store sequences. It'll be more fun trying to come up wiith simple functions to achieve the patterns with loops rather than playing back a defined sequence.

2. wei2912
May 31, 2012 at 9:11 am

Can you make a tutorial on how to make a LED Cube with common materials in the house?

• muotechguy
May 31, 2012 at 2:07 pm

Great idea Wei. Give me a few weeks and I'll see if I can knock something. I'm not sure about "household materials" though; any LED is cube is going to need lots of LEDs and resistors of course!

• wei2912
June 1, 2012 at 2:58 am

I think 4 x 4 x 4 is the best. It is not as expensive as the others, yet it can still create good animations. Sadly you need 64 LEDs and resistors... Household materials will be rather hard to find, but most tutorials I've been looking at want people to solder the metal frame one by one...

• wei2912
June 1, 2012 at 3:31 am

Here's a preview: http://www.youtube.com/watch?v=6mXM-oGggrM&feature=related though it's 8 x 8 x 8

• wei2912
June 3, 2012 at 12:35 pm

I think the metal frame can be replaced with wooden chopsticks (hopefully your house has them) and to connect the LEDs, we can simply use insulated wires.

3. Dom
December 11, 2011 at 11:34 am

Cool tutorial - thanks
This is a great series!