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

Pwm2Color Sample


  • Please log in to reply
No replies to this topic

#1 dab

dab

    Advanced Member

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

Posted 27 August 2010 - 05:40 AM

Hi Netduino fans,

Here's a fun little project that builds on my previous PwmSample and PwmGamma programs.

The Pwm2Color sample controls a 2-color LED (or 2 individual LEDs, if you wish). It uses 2 pushbutton switches as input; one button controls the intensity of the Red LED, and the other button controls the Green LED.

The buttons gradually ramp the LED intensity up and back down, using Gamma correction to make the ramps more linear.

By using a 2-color LED (which is just 2 LED substrates in the same package), you can mix the red and green components in different proportions, creating intermediate shades of yellow and orange. B)

Please see the comments in the code for a description of the circuit. I'll try to post up a schematic later if there's interest.

I've made a few tweaks since the PwmGamma sample, and tried to streamline the code a bit:

  • The code now makes use of the InterruptPort's glitchFilter parameter, so please make sure you are running Netduino firmware 4.1.0.2 or later.
  • Since I'm now using the glitch filter, there's no need to disable and re-enable the interrupts in the interrupt callback method.
  • Note that the same interrupt callback is used to handle the interrupts from both buttons. The interrupt callback uses the data1 parameter to determine which button generated the interrupt.
  • The Channel class is used to encapsulate the PWM port (for the LED), the InterruptPort (for the corresponding pushbutton), and the state variables. This makes the code much simpler, since it can just iterate over Channel objects. It also makes it much easier to extend the sample to use a 3-color LED (literally a 2-line code change). ;)
Here's the code. Have fun!

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

namespace Pwm2Color
{
    // The Channel class encapsulates the PWM port for an LED, the InterruptPort
    // for the corresponding pushbutton switch, and some state variables.
    public class Channel
    {
        public bool ButtonState { get; set; }
        public int PwmIndex { get; set; }
        public int PwmIncrement { get; set; }

        public PWM Led 
        {
            get { return _Led; }
        }
        private PWM _Led;

        public InterruptPort Button 
        {
            get { return _Button; }
        }
        private InterruptPort _Button;

        public Channel(Cpu.Pin LedPin, Cpu.Pin ButtonPin)
        {
            ButtonState = false;
            PwmIndex = 0;
            PwmIncrement = 1;
            _Led = new PWM(LedPin);
            _Button = new InterruptPort(
                ButtonPin,
                true,
                Port.ResistorMode.Disabled,
                Port.InterruptMode.InterruptEdgeBoth
            );
        }
    }


    public class Program
    {
        const int pwmPeriod = 50;
        const uint MaxDutyCyle = 100;

        // Lookup table for the LED brightness.  The table contains the 
        // gamma-corrected values (for gamma = 2.5).  This approximates
        // a linear increase in the perceived brightness.
        //
        // Declaring it as a byte array makes the assembly a little smaller,
        // and the byte values automatically get promoted to uint when
        // setting the PWM duty cycle.
        static byte[] gammaTable = {
             0,  0,  0,  0,  1,  1,  1,  2,  2,   3,
             4,  5,  6,  7,  9, 10, 12, 14, 16,  18,
            20, 22, 25, 28, 31, 34, 37, 41, 45,  49,
            53, 57, 62, 67, 72, 77, 82, 88, 94, 100,
        };

        static Channel[] channels;

        public static void Main()
        {
            // NOTE: You will need to connect 2 LEDs and an additional pushbutton switch 
            // to the Netduino board.  The circuit is as follows:
            //
            // 1. Connect a 100-ohm resistor between the anode of the Green LED and Digital Pin 5.
            // 2. Connect a 100-ohm resistor between the anode of the Red LED and Digital Pin 6.
            // 3. Connect the LED cathode(s) to GND on the Power header.
            // 4. Connect a 1K pull-up resistor between 3V3 on the Power header and Digital Pin 1.
            // 5. Connect one side of the SPST pushbutton switch to Digital Pin 1. (This switch 
            //    controls the Red LED).
            // 6. Connect the other side of the SPST pushbutton switch to GND on the Power header.
            //
            // I used a 2-color LED which has red and green LEDs in the same package.
            // Electrically, it's the same as 2 separate LEDs, so the circuit is virtually the same.
            // If you use a 2-color LED, be sure to get the common-cathode variety.
            // With a 2-color LED, you can "mix" the colors in varying proportions.
            // When both red and green are on, the resulting color is yellow or orange.
            //
            // It should be almost trivial to modify this program to handle a 3-color LED. ;)

            // Allocate the array to hold 2 Channel references.
            channels = new Channel[2];

            // Create each Channel and add it to the channels[] array.
            channels[0] = new Channel(Pins.GPIO_PIN_D5, Pins.ONBOARD_SW1);  // Green channel
            channels[1] = new Channel(Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D1);  // Red channel

            // Bind the interrupt handler to the pin's interrupt event.
            // The same interrupt handler is used for all the channels.
            // The data1 parameter in the handler will determine which button
            // generated the interrupt.
            foreach (Channel ch in channels)
            {
                ch.Button.OnInterrupt += new NativeEventHandler(SwitchInterruptHandler);
            }

            // Create a System.Threading.Timer instance and pass it the timer callback method.
            Timer pwmTimer = new Timer(
                new TimerCallback(PwmTimerCallback),
                null,
                pwmPeriod,
                pwmPeriod);

            Thread.Sleep(Timeout.Infinite);
        }

        public static void PwmTimerCallback(Object obj)
        {
            // Only change the LED brightness when the corresponding button is pushed (true).
            foreach (Channel ch in channels)
            {
                if (true == ch.ButtonState)
                {
                    // Set the pin's new duty cycle.
                    ch.Led.SetDutyCycle(gammaTable[ch.PwmIndex]);
                    ch.PwmIndex += ch.PwmIncrement;

                    Debug.Print(gammaTable[ch.PwmIndex].ToString());

                    if (((gammaTable.Length - 1) == ch.PwmIndex) || (0 == ch.PwmIndex))
                    {
                        // The duty cycle has hit the min or max value.
                        // Start ramping in the other direction.
                        ch.PwmIncrement = -ch.PwmIncrement;
                    }
                }
            }
        }

        // port:  The pin number of the port that generated the interrupt
        // state: The logic state of the port
        // time:  Time of the event
        public static void SwitchInterruptHandler(UInt32 port, UInt32 state, DateTime time)
        {
            foreach (Channel ch in channels)
            {
                if (port == (UInt32)ch.Button.Id)
                {
                    ch.ButtonState = (0 == state);
                }
            }
        }

    }
}

Thanks,
~ David ~




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.