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

ToggleButton sample


  • Please log in to reply
12 replies to this topic

#1 dab

dab

    Advanced Member

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

Posted 13 August 2010 - 06:39 AM

OK, here's my ToggleButton sample from the other thread "Using InterruptPorts".

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();
        }

    }
}

Thanks,
~ David ~

#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 August 2010 - 06:47 AM

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.

#3 dab

dab

    Advanced Member

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

Posted 13 August 2010 - 06:57 AM

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)?
Thanks,
~ David ~

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 August 2010 - 07:01 AM

Do you happen to know what the default glitch filter time is

The official documentation [^] says 7.68 ms.

#5 José Ángel

José Ángel

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts
  • LocationSpain

Posted 13 August 2010 - 11:17 AM

I have playing with the example on my silly emulator and works great :D http://forums.netdui...indpost__p__277

#6 dab

dab

    Advanced Member

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

Posted 13 August 2010 - 06:05 PM

I have playing with the example on my silly emulator and works great :D

http://forums.netdui...indpost__p__277


Cool, thanks! I've been meaning to try out your emulator. So many projects, so little time... :(
Thanks,
~ David ~

#7 zee

zee

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 26 March 2013 - 01:20 AM

Hi Dab,

 

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.



#8 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 26 March 2013 - 01:57 AM

Put your song code in a separate thread.  Start and stop the thread using the button toggle code.



#9 zee

zee

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 27 March 2013 - 02:39 AM

Put your song code in a separate thread.  Start and stop the thread using the button toggle code.

 

Hi Dave,

 

You mind explain to me the steps? I am soooo noob in this netduino. Really new to me. Is it add new class or add new project?

I did tried to add both, but received few errors. Putting them in a different project, no errors but how to link? Add class, received lots of errors.

Appreciate your help :)



#10 zee

zee

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 27 March 2013 - 05:17 AM

Program.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;
using System.IO.Ports;
using System.Text;
 
namespace PushButton_OutputMusic
{
    public class Program
    {
        private static SecretLabs.NETMF.Hardware.PWM speaker = new SecretLabs.NETMF.Hardware.PWM(Pins.GPIO_PIN_D5);
 
        static System.Collections.Hashtable scale = new System.Collections.Hashtable();
 
        public static void Main()
        {
 
            while (true)
            {
                GetSound();
             
            }
        }
 
        private static void GetSound()
        {
             
            //low octave
            scale.Add("c", 1915u);
            scale.Add("d", 1700u);
            scale.Add("e", 1519u);
            scale.Add("f", 1432u);
            scale.Add("g", 1275u);
            scale.Add("a", 1136u);
            scale.Add("b", 1014u);
 
            // high octave
            scale.Add("C", 956u);
            scale.Add("D", 851u);
            scale.Add("E", 758u);
            scale.Add("F", 191u);
            scale.Add("G", 170u);
            scale.Add("A", 153u);
            scale.Add("B", 136u);
            scale.Add("C2", 127u);
 
            // silence ("hold note")
            scale.Add("h", 0u);
 
            int beatsPerMinute = 90;
            int beatTimeInMilliseconds = 60000 / beatsPerMinute; //60,000 milliseconds per minute
            int pauseTimeInMilliseconds = (int)(beatTimeInMilliseconds * 0.1);
 
            string song = "C1C1C1g1a1a1g2E1E1D1D1C2h"; //old macdonald
            song += "C1C1G1G1A1A1G2F1F1E1E1D1D1C2h"; //twinkle little star
 
            while (true)
            {
 
                for (int i = 0; i < song.Length; i += 2)
                {
                    //song loop
 
                    string note = song.Substring(i, 1); 
                    int beatCount = int.Parse(song.Substring(i + 1, 1)); 
 
                    uint noteDuration = (uint)scale[note];
 
                    speaker.SetPulse(noteDuration * 2, noteDuration); 
 
                    Thread.Sleep(beatTimeInMilliseconds * beatCount - pauseTimeInMilliseconds);
 
                    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..



#11 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 30 March 2013 - 08:49 PM

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

#12 zee

zee

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 01 April 2013 - 03:25 AM

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?



#13 sebswed

sebswed

    Advanced Member

  • Members
  • PipPipPip
  • 45 posts
  • LocationSweden

Posted 15 April 2013 - 05:55 AM

Hi there.

 

I see in the code: "ONBOARD_SW1, false, Port.ResistorMode.Disabled",

Should that not be set to pull upp or down to debaunce the switch?


Netduino Plus 2 (v4.2.2.2)





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.