Community

Simple button readout not working?

I have connected D04 to a pushbutton. Pullup using an external resistor of 3K9. Verfied with a voltmeter that the voltage becomes 0 when presssed and 3.3 when released on pin DO4.
I use to code below:

using System;
using System.Threading;
using Meadow;
using Meadow.Devices;
using Meadow.Foundation;
using Meadow.Foundation.Leds;
using Meadow.Foundation.Sensors.Buttons;
using Meadow.Hardware;

namespace HelloMeadow
{
public class MeadowApp : App<F7Micro, MeadowApp>
{
IDigitalInputPort input;

    public MeadowApp()
    {
        input = Device.CreateDigitalInputPort(Device.Pins.D04, debounceDuration: 20);
        input.Changed += Input_Changed;
        Console.WriteLine("Started...");
    }

    private void Input_Changed(object sender, DigitalInputPortEventArgs e)
    {
        Console.WriteLine("Changed: " + e.Value.ToString()); //+ "); Time: " + e.Time.ToString());
    }

}

}

After deploying it in VS2019 is shows “Started…” on the console. But pressing the button doesn’t return the "Changed: " message. I commented out the e.Time.ToString() part because this was not recognized by the compiler.
Why doesn’t this work?

Let me start my answer by saying that I have not yet gotten my device so basically don’t know what I’m talking about. That being said, I did take a look at the documentation on this problem and noticed the following:
(1) Your code closely follows the example given for Eventing and Observable in the documentation. I suspect that this example may be incorrect and needs to be updated. This is all a work in progress I think.
(2) the Documentation at http://developer.wildernesslabs.co/docs/api/Meadow/Meadow.Devices.F7Micro.html for the CreateDigitalInportPort method shows the following signature ```
public IDigitalInputPort CreateDigitalInputPort(IPin pin, InterruptMode interruptMode = InterruptMode.None, ResistorMode resistorMode = ResistorMode.Disabled, double debounceDuration = 0, double glitchDuration = 0)
I think you left out the InterruptMode which is why your event is not firing.
(3) The Button Interrupt example given at http://developer.wildernesslabs.co/Samples/Netduino/ButtonInterruptEvents/ supports this thought since it defines the call to CreateDigitalInputPort with the additional parameters.

I hope this helps. Maybe someone else will chime in later with something better.

Doug

Hi ddodgen,

I added the InterruptMode in the call:
public MeadowApp()
{
input = Device.CreateDigitalInputPort(Device.Pins.D04, InterruptMode.EdgeFalling, debounceDuration: 20);
input.Changed += Input_Changed;
Console.WriteLine(“Started…”);
}

But I get the following (run time) error:
The program ‘[1548] App.exe’ has exited with code 0 (0x0).
ioctl RegisterGpioIrq failed OperationNotPermitted
Started…

It looks like adding the InterruptMode is not permitted…
Remove it again also removes the error again.

Also tried to change to another pin D05, but no luck.

Maybe my meadow device is broken…

You are declaring the interrupt mode incorrectly. Use something like this:
input = Device.CreateDigitalInputPort(
Device.Pins.D02,
resistorMode: ResistorMode.PullDown,
debounceDuration: 20,
interruptMode: InterruptMode.EdgeRising,
glitchDuration: 20
);

You should also consider adding an internal pull resistor depending on whether your button is tied to high or low input.