Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X
Post

Fun with 8 LEDs and the Netduino

This entry is part 2 of 2 in the series Netduino Experimenters Kit

SparkFun.com offers a reasonably priced Inventor's Kit for people new to electronics. It currently only offers this bundled with an Arduino Uno, however in the hopes that it will offer a Netduino version, or one without a bundled microcontroller, or simply for people interested in learning multiple platforms, I'm converting the code to a .NET/C# version. The materials of the kit are released under the Creative Commons license. Many of the images, diagrams, and other materials below came directly from or were derived from SparkFun/.:OOMLOUT:.

We have caused one LED to blink, now it’s time to up the stakes. Lets connect eight. We’ll also have an opportunity to stretch the Netduino a bit by creating various lighting sequences. This circuit is also a nice setup to experiment with writing your own programs and getting a feel for how the Netduino works. Along with controlling the LEDs we start looking into a few simple programming methods to keep your programs small.

for() loops – used when you want to run a piece of code several times. arrays[] – used to make managing variables easier (it’s a group of variables).

Parts Required

  • Breadboard
  • 8x 5mm Yellow LED (or any color you’d like)
  • Jumper wire
  • 8x 330ohm resistor (orange-orange-brown)

Project Code

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
 
namespace CIRC_02
{
    public class Program
    {
        //Arrays are groups of variables
        static OutputPort[] leds = new OutputPort[8];
 
        public static void Main()
        {
            //Setup all 8 LED OutputPorts, one for each connected pin
            leds[0] = new OutputPort(Pins.GPIO_PIN_D2, false);
            leds[1] = new OutputPort(Pins.GPIO_PIN_D3, false);
            leds[2] = new OutputPort(Pins.GPIO_PIN_D4, false);
            leds[3] = new OutputPort(Pins.GPIO_PIN_D5, false);
            leds[4] = new OutputPort(Pins.GPIO_PIN_D6, false);
            leds[5] = new OutputPort(Pins.GPIO_PIN_D7, false);
            leds[6] = new OutputPort(Pins.GPIO_PIN_D8, false);
            leds[7] = new OutputPort(Pins.GPIO_PIN_D9, false);
 
 
            while (true)
            {
                oneAfterAnotherNoLoop();
                //oneAfterAnotherLoop();
                //oneOnAtATime();
                //inAndOut();
            }
        }
 
        /// <summary>
        /// Will light one LED then delay for delayTime then light
        /// the next LED until all LEDs are on it will then turn them off one after another.
        /// This does not use a loop and is similar to the method used in CIRC-01
        /// </summary>
        static void oneAfterAnotherNoLoop()
        {
            int delayTime = 100;
 
            leds[0].Write(true);
            Thread.Sleep(delayTime);
            leds[1].Write(true);
            Thread.Sleep(delayTime);
            leds[2].Write(true);
            Thread.Sleep(delayTime);
            leds[3].Write(true);
            Thread.Sleep(delayTime);
            leds[4].Write(true);
            Thread.Sleep(delayTime);
            leds[5].Write(true);
            Thread.Sleep(delayTime);
            leds[6].Write(true);
            Thread.Sleep(delayTime);
            leds[7].Write(true);
            Thread.Sleep(delayTime);
 
            leds[7].Write(false);
            Thread.Sleep(delayTime);
            leds[6].Write(false);
            Thread.Sleep(delayTime);
            leds[5].Write(false);
            Thread.Sleep(delayTime);
            leds[4].Write(false);
            Thread.Sleep(delayTime);
            leds[3].Write(false);
            Thread.Sleep(delayTime);
            leds[2].Write(false);
            Thread.Sleep(delayTime);
            leds[1].Write(false);
            Thread.Sleep(delayTime);
            leds[0].Write(false);
            Thread.Sleep(delayTime);
        }
 
        /// <summary>
        /// Will light one LED then delay for delayTime then light
        /// the next LED until all LEDs are on it will then turn them off one after another
        /// 
        /// This does it using a loop which makes for a lot less typing.
        /// </summary>
        static void oneAfterAnotherLoop()
        {
            int delayTime = 100;
 
            for (int i = 0; i <= 7; i++)
            {
                leds[i].Write(true);
                Thread.Sleep(delayTime);
            }
 
            for (int i = 7; i >= 7; i--)
            {
                leds[i].Write(false);
                Thread.Sleep(delayTime);
            }
        }
 
        /// <summary>
        /// Will light one LED then the next turning off all the others
        /// </summary>
        static void oneOnAtATime()
        {
            int delayTime = 100;
 
            for (int i = 0; i <= 7; i++)
            {
                int offLED = i - 1;
                if (i == 0)
                {
                    offLED = 7;
                }
 
                leds[i].Write(true);
                leds[offLED].Write(false);
                Thread.Sleep(delayTime);
            }
        }
 
 
        /// <summary>
        /// This will turn on the two middle LEDs then the next two out
        /// making an in and out look
        /// </summary>
        static void inAndOut()
        {
            int delayTime = 100;
 
            //runs the LEDs out from the middle
            for (int i = 0; i <= 3; i++)
            {
                int offLED = i - 1;     //Calculate which LED was turned on last time through
                if (i == 0)
                {                       //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
                    offLED = 3;         //turn on LED 2 and off LED 1)
                }                       //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                                        //instead we turn off LED 7, (looping around)
                int onLED1 = 3 - i;     //this is the first LED to go on ie. LED #3 when i = 0 and LED 
                //#0 when i = 3 
                int onLED2 = 4 + i;     //this is the first LED to go on ie. LED #4 when i = 0 and LED 
                //#7 when i = 3 
                int offLED1 = 3 - offLED; //turns off the LED we turned on last time
                int offLED2 = 4 + offLED; //turns off the LED we turned on last time
 
                leds[onLED1].Write(true);
                leds[onLED2].Write(true);
                leds[offLED1].Write(false);
                leds[offLED2].Write(false);
                Thread.Sleep(delayTime);
            }
 
            //runs the LEDs into the middle
            for (int i = 3; i >= 0; i--)
            {
                int offLED = i + 1;  //Calculate which LED was turned on last time through
                if (i == 3)
                {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
                    offLED = 0;        //turn on LED 2 and off LED 1)
                }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                //instead we turn off LED 7, (looping around)
                int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED 
                //#0 when i = 3 
                int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED 
                //#7 when i = 3 
                int offLED1 = 3 - offLED; //turns off the LED we turned on last time
                int offLED2 = 4 + offLED; //turns off the LED we turned on last time
 
                leds[onLED1].Write(true);
                leds[onLED2].Write(true);
                leds[offLED1].Write(false);
                leds[offLED2].Write(false);
                Thread.Sleep(delayTime);
            }
        }
 
    }
}

How It Works

This time we use an array of OutputPorts. The array is specified by the square brackets [] in the declaration. Also of note, the declaraction for ‘leds’ is outside of the void Main() function. This is necessary because we need to use that variables in all of our functions. If we declared it within the Main() function it would not be available to any other function.

By storing these values in an array we allow ourselves to simplify code using loop structures. It’s worth reading over the code for the oneAfterAnotherNoLoop() and oneAfterAnotherLoop() functions. These two functions produce identical output, however the latter is far simplier due to it’s use of loops.

Two additional functions, oneOnAtATime() and inAndOut() both produce other lighting effects. The code is fairly simple and is using simple math to determine which LEDs to turn on and off in a given loop iteration. You can change which function is run by commenting and uncommenting which function is called during the while loop within the Main() function.

Further Learning

  1. Try all 4 lighting modes and compare the output to the code
  2. Create a new lighting mode that turns only the odd LEDs on, then only the even ones
  3. Invent your own lighting mode
  4. Add a 9th LED and update the lighting modes to work with the additional OutputPort
Series Navigation<< Making an LED Blink with Netduino