Earlier I wrote that I got a Netduino Go Starter kit for Christmas. I've worked with it from time to time. This is a little late posting, because I finished my first project in February. I thought I'd share it here and perhaps I might get some ideas for future projects.

You get several things with the starter kit including an RGB LED, button, and potentiometer. I put these together as an RGB flasher. The concept is simple. The LED flashes with color changes on an interval. The button starts and stops the flashing. And the potentiometer controls the rate of the flashing.

To start this I created a class with the following interface (full class code at the end of the article):

class RgbFlasher : IDisposable {
     RgbFlasher()
     Start()
     Dispose()
}

This class pretty much controls everything. The Main method creates and starts a RgbFlasher and waits as shown below.

 public static void Main()
 {
     var flasher = new RgbFlasher();
     flasher.Start();

     while (true)
     {
         Thread.Sleep(1000);
     }
 }

Overall, it was a fun, short project and was easy to put together. I'm looking for a little more of a challenge the next time around.

Full listing for the RgbFlasher class

using NetduinoGo;
using System;
using System.Threading;

 

public class RgbFlasher : IDisposable
{
    private readonly RgbLed rgbBtn;
    private readonly Button toggleBtn;
    private readonly Potentiometer delayMeter;
    private Timer timer;

    private readonly Random rand;
    private bool isFlashing = false;

    private const int DelayBase = 1750;
    private const int DelayMin = 250;

    public RgbFlasher()
    {
        // init components
        rgbBtn = new RgbLed();
        delayMeter = new Potentiometer();
        toggleBtn = new Button();
        toggleBtn.ButtonPressed += (sender, state) => isFlashing = !isFlashing;

        rand = new Random((int)DateTime.Today.Ticks);
    }

    public void Start()
    {
        int delay = GetDelay(delayMeter);
        timer = new Timer(new TimerCallback(Flash), null, 0, delay);
    }

    private void Flash(object state)
    {
        // change the color while led state is flashing; button
        // controls that state
        if (isFlashing)
        {
            byte r = (byte)rand.Next(255);
            byte g = (byte)rand.Next(255);
            byte b = (byte)rand.Next(255);

            rgbBtn.SetColor(r, g, b);
        }

        // set the timer based on the potentiometer
        int newDelay = GetDelay(delayMeter);
        timer.Change(newDelay, newDelay);
    }

    private static int GetDelay(Potentiometer meter)
    {
        return (int)(DelayBase * meter.GetValue()) + DelayMin;
    }

    public void Dispose()
    {
        if (rgbBtn != null) rgbBtn.Dispose();
        if (toggleBtn != null) toggleBtn.Dispose();
        if (delayMeter != null) delayMeter.Dispose();
        if (timer != null) timer.Dispose();
    }
}