LOGO

Arduino Christmas Lights Ornament Project - DIY Guide

December 11, 2011
Topics:DIYArduino
Arduino Christmas Lights Ornament Project - DIY Guide

Arduino Arrays and Flashing Lights: A Christmas Ornament Project

This article represents the subsequent step in our Arduino learning journey. We will explore and implement the use of Arrays to create a Christmas tree ornament featuring diverse flashing light patterns. This project is well-suited for engaging children in basic soldering skills – simply mount the LEDs onto a piece of cardstock, and power can be supplied by a standard 9v battery.

This serves as a fundamental lesson in beginner Arduino programming. Even if you don't intend to use this as an ornament, it is highly recommended to prototype it on a breadboard.

Understanding the Core Concepts

It's important to note that this tutorial is designed for beginners and doesn't aim to introduce groundbreaking techniques. It's a practical exercise to illustrate the application of Arrays and For loops when managing a large number of LEDs (or other output components).

If you haven't already, reviewing the previous articles in this series would be beneficial:

  • What Is Arduino & What Can You Do With It?
  • What Is An Arduino Starter Kit & What Does It Contain?
  • More Cool Components To Buy With Your Starter Kit
  • Getting Started With Your Arduino Starter Kit – Installing Drivers & Setting Up The Board & Port
  • Fritzing, a free tool for drawing circuit diagrams
  • A Closer Look At The Structure Of An Arduino App & The Example Blink Program

Required Components

For this project, you will require at least 8 or 9 LEDs, preferably in red or green. Additionally, you'll need a resistor for each LED, a breadboard, and some connecting wires. The Ooomlout starter kit, which I recently acquired, provides excellent value and includes an abundance of LEDs and resistors, along with a convenient breadboard and Arduino case.

Here’s the completed project:

arduino-project-flashy-christmas-lights-ornaments-1.jpg

And here's a video demonstrating its functionality.

Wiring the Circuit

The wiring diagram, created in Fritzing, is straightforward. Connect the positive (longer) lead of each LED to pins 2 through whatever number you need (up to pin 13). Connect the negative (shorter) leads to ground, incorporating a resistor in-line with each LED. A 560 Ohm resistor is suitable for this application. This completes the wiring process.

arduino-project-flashy-christmas-lights-ornaments-2.jpg

The Software Approach: Avoiding Repetition

Consider how to control all these LEDs through code. A repetitive approach might look 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
}

It becomes apparent that managing 9 LEDs in this manner will quickly become cumbersome. The solution lies in utilizing Arrays, which, as a refresher from our Programming 101 discussion on datatypes, are essentially lists.

Defining and Using Arrays

The syntax for defining an array is as follows (place this as the first line in your code):

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

The square brackets signify that the variable 'leds' will be an Array. The curly braces enclose the list of pin numbers that the array will store.

To access elements within an Array, you use the index number. The index begins at 0 and extends to one less than the total number of elements (so, with 9 items, the last element has an index of 8).

Accessing an element is done like this:

leds[0]

In our case, this would retrieve the number 2, as it's located at index 0 in the array.

Implementing For Loops for Iteration

However, this alone isn't sufficient. We also need a way to iterate through each element of the LEDs array. This is where a for loop comes into play. The syntax is:

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

For example:

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

This translates to:

  • Initialize a variable, i, to zero.
  • Continue looping as long as i is less than 9 (values 0, 1, 2, 3, 4, 5, 6, 7, 8).
  • Increment i by 1 in each iteration (i++ is shorthand for i = i+1).

The loop will repeat as many times as there are LEDs, and each time, the variable i can be used as needed.

Applying Arrays and For Loops in Code

We'll use this structure initially within the setup function to configure all pins as output:

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

Notice how we avoided writing nine separate lines of code by using a 'for' loop to repeat the pin configuration process.

We can now apply the same principle in the main program loop to sequentially turn on each LED:

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

Try this code. The complete code for this project is available for download here if you prefer not to type it out, although typing it yourself is encouraged for better learning.

Adding Randomness to the Sequence

Now that we have a sequential lighting pattern, let's introduce some randomness. 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 iterating through the LEDs sequentially, we now randomly select an LED to illuminate.

This concludes today's lesson. You should now have the knowledge to create new sequences and experiment with loops. As a challenge, my wife devised a sequence and programmed it herself, using only the code and lessons provided. See if you can replicate her sequence!

Feel free to share your questions, suggestions, or any issues you encounter in the comments section.

#Arduino#Christmas lights#ornaments#DIY#project#flashing lights