Question on Button events

Question on Button events.

I am using TinyCLR 0.6.0 on a G120E Dev Board 1.2

I never receive the RisingEdge event on any of the buttons.
Up, Down, Left, Right, Select

Example for the UP buttonUP

> namespace TinyCLR_G120E_0._6._0
> {
>     class Program
>     {
> 		static GpioPin buttonUP;
> 		static GpioPin led1;
> 		
> 		static void Main()
> 		{
> 			led1 = GpioController.GetDefault().OpenPin(GHIElectronics.TinyCLR.Pins.G120E.GpioPin.P3_27);
> 			led1.SetDriveMode(GpioPinDriveMode.Output);
> 			
> 			GpioController GPIO = GpioController.GetDefault();
> 			
> 			buttonUP = GPIO.OpenPin(G120E.GpioPin.P2_10);
> 			buttonUP.SetDriveMode(GpioPinDriveMode.InputPullUp);
> 			buttonUP.ValueChanged += ButtonUP_ValueChanged;
> 			
> 			// test returns true
> 			// var test = buttonUP.IsDriveModeSupported(GpioPinDriveMode.InputPullUp);
> 				
> 			buttonUP.ValueChanged += ButtonUP_ValueChanged;
> 			
> 			while (true)
> 			{
> 				Thread.Sleep(500);
> 			}
> 			//	
> 		}
> 		// End main
> 		
> 		private static void ButtonUP_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
> 		{
> 			Debug.WriteLineIf(true, ""ButtonUP was pressed or released"");
> 		
> 			if (e.Edge == GpioPinEdge.FallingEdge)
> 			{
> 				led1.Write(GpioPinValue.Low);
> 				Debug.WriteLineIf(true, "Up Btn FallingEdge: " + e.Edge.ToString());
> 			}
> 			// I never receive the RisingEdge event on any of the 5 buttons
> 			else if (e.Edge == GpioPinEdge.RisingEdge) 
> 			{
> 				led1.Write(GpioPinValue.High);
> 				Debug.WriteLineIf(true, "Up Btn RisingEdge: " + e.Edge.ToString());
> 			}
> 		}
> 		//
>     } // End class Program
> } // End namespace

What am I doing wrong? (If anything)

And how do you show formatted text. The code example is filled with extra spaces or tabs?

I ran your code on a G80 Dev Board and got both rising and falling edges. I will keep playing with it.

I hat the experience with some GHI Gadgeteer Button Modules that some modules work and some don’t (esp. Vers. 1.3) . May be that it is a mechanical/electrical problem of the buttons (e.g. contact bouncing) and not of the code.

I agree with you. However, I questioned the fact that all of the five button responded in the same way.

I would think that I would get random hits or misses on the RisingEdge events.

That’s true. Is there an option to activate the glitch filter?

RisingEdge not being raised is a known issue for some devices, including G120, that should already be fixed in the ports repo for the next release.

You should be able to call Read in the handler instead of checking the parameter to work around this issue.

1 Like

Thanks for the information.

It is not a problem for me because I was just trying our a few things with the latest release on my G120E developer board and I ran into the problem.

I thought I may have done something wrong in the code.

It seems to be come back with v0.7.0.0:

using System;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Threading;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Pins;

namespace testEMX_Button
{
    class Program
    {
        private static GpioPin _button;
        private static GpioPin _led;
        private static GpioPin _ledMainboard;

        static void Main()
        {
            Setup();
            Loop();
        }

        private static void Setup()
        {
            _led = GpioController.GetDefault().OpenPin(FEZSpider.GpioPin.Socket11.Pin4);
            _led.SetDriveMode(GpioPinDriveMode.Output);
            _led.Write(GpioPinValue.High);

            _ledMainboard = GpioController.GetDefault().OpenPin(FEZSpider.GpioPin.DebugLed);
            _ledMainboard.SetDriveMode(GpioPinDriveMode.Output);
            _ledMainboard.Write(GpioPinValue.High);

            Thread t=new Thread(BlinkingLed);
            t.Start();

            _button = GpioController.GetDefault().OpenPin(FEZSpider.GpioPin.Socket11.Pin3);
            _button.SetDriveMode(GpioPinDriveMode.InputPullDown);

            _button.ValueChanged += _button_ValueChanged;
        }

        private static void BlinkingLed()
        {
            while (true)
            {
                _ledMainboard.Write(GpioPinValue.High);
                Thread.Sleep(200);
                _ledMainboard.Write(GpioPinValue.Low);
                Thread.Sleep(800);
            }
        }

        private static void _button_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            Debug.WriteLine("Button event: " + (e.Edge == GpioPinEdge.FallingEdge ? "Falling Edge" : "Raising edge"));
        }

        private static void Loop()
        {
            while (true)
            {
                Thread.Sleep(20);
            }
        }
    }
}

Output:

Attaching deployed file.

   Assembly: testEMX_Button (1.0.0.0)  Attaching deployed file.

   Assembly: mscorlib (0.7.0.0)  Attaching deployed file.

   Assembly: GHIElectronics.TinyCLR.Devices (0.7.0.0)  Resolving.

The debugging target runtime is loading the application assemblies and starting execution.
Ready.

'GHIElectronics.TinyCLR.VisualStudio.dll' (Managé) : 'd:\documents\visual studio 2017\Projects\testEMX_Button\testEMX_Button\bin\Debug\pe\..\GHIElectronics.TinyCLR.Devices.dll' chargé, chargement des symboles ignoré. Le module est optimisé et l'option du débogueur 'Uniquement mon code' est activée.
'GHIElectronics.TinyCLR.VisualStudio.dll' (Managé) : 'd:\documents\visual studio 2017\Projects\testEMX_Button\testEMX_Button\bin\Debug\pe\..\testEMX_Button.exe' chargé, symboles chargés.
Le thread '<Aucun nom>' (0x2) s'est arrêté avec le code 0 (0x0).
Button event: Falling Edge
Button event: Raising edge
Button event: Falling Edge <--
Button event: Falling Edge <--
Button event: Falling Edge <--
Button event: Raising edge
Button event: Falling Edge
Button event: Raising edge
Button event: Falling Edge
Button event: Raising edge

This issue was never receiving any rising edge events, but it looks like you receive some. You just got a few falling edge ones in a row, correct? I believe the DebounceTimeout property is currently supported, try increasing that slightly.

I’ll give it a try next week to change DebounceTimeout

DebounceTimeout is a clue. By default it is 20 000 ms ! I set it to 100 ms and it’s better. (I don"t know what is the best value).

1 Like

that naturally depends on the used switch and vary from only a few ns until few ms (but 20sec is far from reality)
do you have an oscilloscope ?
good read about that A Guide to Debouncing

1 Like

Not all switches have the documentation, but some will call out the bouncing time period in the datasheet. It will be affected by resistance and capacitance. Best to capture the signal changes on a scope and measure the actual value for your switch in your circuit. If that isn’t possible then definitely check the datasheet for an appropriate value.