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.
This is basically like the Event Handlers tutorial on the Projects page. The main difference is that the pushbutton acts like a toggle - push once to turn off the LED, then push again to turn it back on.
Another slight modification - the interrupt handler method calls DisableInterrupt() at the start of the method, and EnableInterrupt() at the end. This seems to fix an issue where pressing and holding the button would sometimes cause the LED to change state when the button was released.
I think this might be due to switch bounce, so multiple interrupts were getting "stacked up" and handled after the first interrupt method returned.
Anyway, here's the code. Let me know if there's interest in posting the entire VS solution. Enjoy!
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace ToggleButton
{
public class Program
{
static bool ledState;
static InterruptPort button;
static OutputPort led;
public static void Main()
{
// Set the initial state of the LED to on (true).
ledState = true;
led = new OutputPort(Pins.ONBOARD_LED, ledState);
button = new InterruptPort(
Pins.ONBOARD_SW1,
false,
Port.ResistorMode.Disabled,
Port.InterruptMode.InterruptEdgeLow);
// Bind the interrupt handler to the pin's interrupt event.
button.OnInterrupt += new NativeEventHandler(SwitchInterruptHandler);
while (true)
{
Thread.Sleep(Timeout.Infinite);
}
}
public static void SwitchInterruptHandler(UInt32 data1, UInt32 data2, DateTime time)
{
button.DisableInterrupt();
// Invert the previous state of the LED.
ledState = !ledState;
// Set the LED to its new state.
led.Write(ledState);
// Un-comment the following line if using level interrupts.
// button.ClearInterrupt();
button.EnableInterrupt();
}
}
}
Another slight modification - the interrupt handler method calls DisableInterrupt() at the start of the method, and EnableInterrupt() at the end. This seems to fix an issue where pressing and holding the button would sometimes cause the LED to change state when the button was released.
Have you tried to enable glitch filter: button = new InterruptPort(..., true, ...) ? The debouncing time is controlable via CPU.GlitchFilterTime [^] property.
Have you tried to enable glitch filter: button = new InterruptPort(..., true, ...) ? The debouncing time is controlable via CPU.GlitchFilterTime [^] property.
I haven't tried the Glitch filter yet, thanks for the suggestion.
Do you happen to know what the default glitch filter time is (or what value is appropriate to debounce a pushbutton)?
Is there any way that piezo speaker can be on/off too upon pressing the toggle button? I have a project to do, where song will be play through piezo if the button is been press & led on, and the song will be stop when the button been press again & led off.
speaker.SetDutyCycle(0); //set to 0, turn the piezo off momentarily
Thread.Sleep(pauseTimeInMilliseconds);
}
Thread.Sleep(1000);
}
}
}
}
Next, Button.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace PushButton_OutputMusic
{
class Button
{
InterruptPort button;
OutputPort led;
bool ledState;
public void buttonToggle()
{
ledState = true;
led = new OutputPort(Pins.ONBOARD_LED, ledState);
button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);
//bind the interrupt handler to the pin's interrupt event
button.OnInterrupt += new NativeEventHandler(SwitchInterruptHandler);
while (true)
{
Thread.Sleep(Timeout.Infinite);
}
}
public void SwitchInterruptHandler(UInt32 data1, UInt32 data2, DateTime time)
{
button.DisableInterrupt();
//invert the previous state of the LED
ledState = !ledState;
//set the LED to its new state
led.Write(ledState);
button.EnableInterrupt();
}
}
}
Is this correct? The song auto play after i deploy. Then i pressed the button, the song stop and it starts automatically in few seconds. My objective is to make the song start and stop when pressing the button on Netduino..
Hi zee,
Your button class isn't being used in your main program, so the button is acting as a software reset button (its default state).
Chris
Hi Chris,
But it works as it stopped the song and continue it automatically after few seconds. Could you guide me how should i put the Button class in Program.cs? Is it supposed to be in 2 different classes as shown above or all in one class?