Here's a little sample that uses a PWM pin to control the brightness of an LED. I like to call it PwmSample, for lack of a better name.
What it does:
When you press and hold the pushbutton, a connected LED will gradually brighten and dim. If you release the pushbutton, the LED stays at its current brightness. Pushing the button again will resume the brighten/dim cycle from the current value.
How it works:
This sample combines several techniques from the previous samples. It uses an InterruptPort for the pushbutton input; the SwitchInterruptHandler() method is called when the InterruptPort detects a rising or falling edge due to the pushbutton.
The sample also uses a Timer callback to do the actual LED brightness control. Every 20 milliseconds, the PwmTimerCallback() method is called. This method checks whether the pushbutton is closed (the buttonState variable). If so, then it increments (or decrements) the PWM pin's duty cycle.
Circuit:
Note that you'll need to connect an external LED to the Netduino board (it won't work with the built-in LED, since it's not connected to a PWM-enabled pin).
I used Digital Pin 5 in the sample; you can use any PWM-enabled pin (5, 6, 9, or 10), but make sure to change the Pin definition on line 34.
I don't have a schematic editor handy, but the circuit is simple enough. Just connect one end of a 100-ohm resistor to Digital Pin 5. Connect the other end of the resistor to the anode (+) lead of your LED. Then connect the cathode (-) lead of the LED to one of the GND pins on the Netduino Power header.
Here's the code:
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace PwmSample { public class Program { const int pwmPeriod = 20; const uint MaxDutyCyle = 100; static bool buttonState; static int pwmDutyCycle; static int pwmIncrement; static InterruptPort button; static PWM led; public static void Main() { pwmDutyCycle = 0; pwmIncrement = 1; buttonState = false; // NOTE: You will need to connect an LED to Digital Pin 5 on the Netduino board. // Use a 100-ohm (brown-black-brown) resistor between the LED anode (+) and Digital Pin 5. // Connect the LED cathode (-) to one of the GND pins on the Power header. // // You can use any other PWM-enabled pin (5, 6, 9 or 10), but also remember to change // the Pin parameter in the PWM constructor below. led = new PWM(Pins.GPIO_PIN_D5); button = new InterruptPort( Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); // Bind the interrupt handler to the pin's interrupt event. 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 button is pushed (true). if (true == buttonState) { // Set the pin's new duty cycle. pwmDutyCycle += pwmIncrement; led.SetDutyCycle((uint)pwmDutyCycle); Debug.Print(pwmDutyCycle.ToString()); if ((MaxDutyCyle == pwmDutyCycle) || (0 == pwmDutyCycle)) { // The duty cycle has hit the min or max value. // Start ramping in the other direction. pwmIncrement = -pwmIncrement; } } } public static void SwitchInterruptHandler(UInt32 data1, UInt32 data2, DateTime time) { button.DisableInterrupt(); buttonState = (0 == data2); button.EnableInterrupt(); } } }
- Kamal likes this