Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

BlinkyTimer sample


  • Please log in to reply
5 replies to this topic

#1 dab

dab

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationBellevue, WA, USA

Posted 15 August 2010 - 06:36 AM

Here's another little sample that I put together called BlinkyTimer.

This basically does the same thing as the Blinky tutorial, but it uses a Timer object with a callback method to turn the LED on and off.

Here's the code. If you think of any cool variations, please share. :D

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace BlinkyTimer
{
    public class Program
    {
        const int blinkPeriod = 250;
        static bool ledState;
        static OutputPort led;

        public static void Main()
        {
            // Set the initial state of the LED to off (false).
            ledState = false;

            led = new OutputPort(Pins.ONBOARD_LED, ledState);

            // Create a System.Threading.Timer instance and pass it the timer callback method.
            Timer blinkTimer = new Timer(
                new TimerCallback(BlinkTimerCallback),
                null,
                blinkPeriod,
                blinkPeriod);

            Thread.Sleep(Timeout.Infinite);
        }

        public static void BlinkTimerCallback(Object obj)
        {
            // Invert the previous state of the LED.
            ledState = !ledState;

            // Set the LED to its new state.
            led.Write(ledState);
        }

    }
}

Thanks,
~ David ~

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 August 2010 - 06:46 AM

Nice. Very clean code, and simple to understand. One quick note: you don't need to put the infinite sleep inside a loop. It will only execute once...so they while(true) is redundant...

#3 dab

dab

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationBellevue, WA, USA

Posted 15 August 2010 - 06:56 AM

Thanks Chris! And thanks for moving it to the new forum.

One quick note: you don't need to put the infinite sleep inside a loop. It will only execute once...so they while(true) is redundant...


Yeah, I keep meaning to fix that. It was in a sample program from the book "Embedded Programming with the Microsoft .NET Micro Framework", but I agree it seems redundant. It might also make the code a little smaller by removing the while loop(?).
Thanks,
~ David ~

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 August 2010 - 06:58 AM

..I agree it seems redundant. It might also make the code a little smaller by removing the while loop(?).


I just updated it for you. Please let me know if you'd like me to change it back.

#5 dab

dab

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationBellevue, WA, USA

Posted 15 August 2010 - 07:01 AM

I just updated it for you. Please let me know if you'd like me to change it back.

You beat me to it. ;) Thanks again, Chris!

(And get some sleep, OK?)
Thanks,
~ David ~

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 August 2010 - 07:11 AM

You beat me to it. ;) Thanks again, Chris!

(And get some sleep, OK?)


No problem. Wait, is it really 3:10am? Alright, g'night :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.