Project Home

Code Longhorn

Project LightCraft

Controlling the Lights



Software

Now that the hardware is set up, let's take a look at the software. Software is the programs that tell the hardware what to do. We've already seen an example of a piece of software that controls the Arduino board: the Blink program. Now let's begin looking at software to control the lights through the Arduino board.

To begin, you'll first need to download the Code Longhorn framework. Right click on the link below and then save the file into your home directory. (Note that if you don't specify the directory, it will save to your Downloads directory, and you'll want to move it from there to your home directory.)

Code Longhorn Framework

Then, using the terminal from which you opened the Arduino IDE, type the following commands, which will unzip and expand the archive you just downloaded:

gunzip codelonghorn_framework.tar.gz
tar -xvf codelonghorn_framework.tar

Next, we need to open the Project LightCraft framework. From within the Arduino IDE, select File->Open. From there, navigate to your home directory and click on the codelonghorn folder. Within that directory, you'll find a grid_framework folder. Double click on that and and then again on grid_framework.ino.

The Arduino sketchbook called grid_framework should be opened.

This framework already consists of a few files (indicated by the tabs across the top), and you may add your own as you progress in the project. The pre-existing files are briefly described here, and we'll look at some of them more in-depth below.

Task 3.1: Blink Light (0,0)

Before you can use the lights we'll need to do one more thing: add a library to your Arduino setup. A library is a collection of already-written functions available for you to use. The framework for this project depends on functions and data structures defined in the FastLED library. To use this library, you first need to add it to your Arduino environment.

To add the FastLED library, you'll want to find the Sketch menu option on the top toolbar of the Arduino IDE. Click on Sketch, click on Include Library and then click Manage Libraries..., and a new window will pop up. Search for FastLED, and then click Install.

Now, let's look at the tests file. At the top of this file, there is a function called blink_light_0_0_example, which is provided for you.

You'll notice that this code is making changes to a data structure called leds: the framework is set up so that you make changes to the lights in the leds data structure but those changes are not reflected in the actual lights immediately. Instead, when you are ready for your changes to be reflected in the lights, you call the function FastLED.show(), which calls the show() function in the FastLED library, and will mirror your changes into the actual lights.

You'll also notice, though, that there is no call to FastLED.show() in blink_light_0_0_example(), but there is a call to FastLED.delay(). Here's why:

While FastLED.show() updates the lights with your changes, we often want the changes we've made to appear for some amount of time before the lights change again. To help us do that, the FastLED library provides a function called delay, which you call in the exact same manner as show(), with one addition: the delay function takes one argument. That argument tells the program how long it should wait in milliseconds before proceeding to the next instruction. For example, if you would like to delay for one second, use the command:
FastLED.delay(1000)
delay() also calls show(), so you don't have to call one and then the other each time you want to update your lights. (Why might you want to show() without providing a delay?)

Now take a careful look at blink_light_0_0_example() and convince yourself that you understand its contents. You'll also notice that this function is called from the loop() function inside the grid_framework file. When you are ready, test the example by uploading it to the Arduino board (use the arrow pointing to the right as you did to blink the LED). The lights will first perform some tests where they alternate PaleVioletRed and SteelBlue, and then the code in the loop() function will begin to execute. In this case, that code is our blink_light_0_0_example().

If the code works properly, you should notice the first light (light 0) in the first strand (strand 0) blinking white. (Notice that we number from 0 and not 1!)

Now, change the code to do the following things:

Good job! You've learned how to control light (0,0). Next, we'll learn how to control more than one of the lights. ;)
Before you move on, turn your cups to red so that a member of the camp staff can check your code, and switch drivers.

Task 3.2: Blink Light (0,0) and Light (2,4)

For our next test, let's blink both light (0,0) (i.e., the light on strand 0 at position 0) and light (2,4) (i.e., the light on strand 2 at position 4). Recall that we have seven strands of seven lights. If the first light is number 0, where do you suppose light 4 is?

Using the information you learned in Task 3.1, complete the blink_light_0_0_and_2_4() function in tests so that the first and last lights blink. Test your function by commenting out the call to blink_light_0_0_example in the loop() function in grid_framework and uncommenting the call to blink_lights_0_0_and_2_4() in that same function. Please ask if you need help!

Woot, woot! Before moving on to controlling all the lights, turn your cups to red so that a member of the camp staff can check your code, and switch drivers.


Task 3.3: Blink All Lights

In your C++ tutorial, you learned how to use nested loops. Using nested loops, complete the blink_all_lights() function so that every light blinks. Once again, test it by commenting out any calls to other functions in the loop() function in grid_framework and uncommenting the call to blink_all_lights() in that same function.

Congratulations! You are able to control the lights! Now you are ready to begin doing more complicated things.
First, turn your cups to red so that a member of the camp staff can check your code, and switch drivers.
Next, move on to creating basic patterns!