简体   繁体   中英

prolonged impulse for GPIO input C# UWP

A digital acoustic sensor is connected to my raspberry.
Everytime it detects sound, it sets the input HIGH. But it only appears to be active for a couple of milliseconds.
How can i prolong the signal within my program that it stays up for 500ms?
It is a functionality which I know from PLC contollers.

Diagram
Start: sensor input
Q: prolonged signal

图表
,

Here my approach with the answer of Michael:
But still, it doesn't hold for Task.Delay. It goes off directly.

    public MainPage()
    {
        this.InitializeComponent();
        GPIOinit();
        Serial();
    }


    private void GPIOinit()
    {
        const int PIN_AKUSTIK = 19;

        GpioController gpio = GpioController.GetDefault();  //GPIO-Controller mit Default belegen
        pin_akustik = gpio.OpenPin(PIN_AKUSTIK);
    }


    public async void Serial() 
    {
      //serial configuration

        while (true)
        {
            //frequency of constant while loop 300ms
            Sensor_Akustik();
        }
    } 




    public void Sensor_Akustik()
    {
        pin_akustik.ValueChanged += Pin_ValueChanged;
        pin_akustik.SetDriveMode(GpioPinDriveMode.Input);

         status_akustik = pin_akustik.Read().ToString();
        textblock_DebugAkustik_live.Text = status_akustik;
        Messenger.Default.Send<string, Page_Akustik>(status_akustik);
    }

    private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
        if (args.Edge == GpioPinEdge.RisingEdge)
        {
            sender.Write(GpioPinValue.High);

            Task.Delay(3000).Wait();
        }
    }

You might refer to following code. You can try to update the latched output value for the pin if the pin is configured as an input.

    private void InitGPIO()
    {
        var gpio = GpioController.GetDefault();

        // Show an error if there is no GPIO controller
        if (gpio == null)
        {
            pin = null;
            GpioStatus.Text = "There is no GPIO controller on this device.";
            return;
        }

        pin = gpio.OpenPin(LED_PIN);
        pin.ValueChanged += Pin_ValueChanged;
        pin.SetDriveMode(GpioPinDriveMode.Input);

    }


    private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
        if(args.Edge == GpioPinEdge.RisingEdge)
        {
            sender.Write(GpioPinValue.High);
            Task.Delay(500).Wait();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM