I'm trying to work out how to capture a
Pin.ValueChanged
event in VB.NET.
I have a PIR switch attached to GPIO Pin #21 on my raspberry Pi 3B. When PIR movement is detected it goes high to 3.3v and when no movement is detected for a set time it goes low.
I want the app to turn on a LED tied to GPIO Pin #26 when the PIR detects movement and goes high and off when it goes low.
I have seen some VB.NET examples that use a timer to check every second or so to see if the pin is high or low which I have tried and the circuit does work but its easy to miss events with a timer. I would like to use an event handler in VB.NET.
In my google searches all I can find is C# examples using a lines similar to this one.
buttonPin.ValueChanged += buttonPin_ValueChanged;
Then the procedure to do something if the event happened.
private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
}
But I have no idea on how to convert this to VB.NET. All the conversion websites create incorrect code in VB.for the line -
buttonPin.ValueChanged += buttonPin_ValueChanged;
What I have tried:
I tried the following VB.NET code but got an error.
AddHandler PIR_Pin.ValueChanged, AddressOf test
Private Sub test(ByVal sender As GpioPin, ByVal args As GpioPinValueChangedEventArgs)
If PIR_Pin.Read = GpioPinValue.High Then
LED_Pin.Write(GpioPinValue.High)
Else
LED_Pin.Write(GpioPinValue.Low)
End If
End Sub
This is the error I get in VS 2017
System.ArgumentException
HResult=0x80070057
Message=Delegate to an instance method cannot have null 'this'.
Source=System.Private.CoreLib
StackTrace:
at System.Delegate.DelegateConstruct(Object target, IntPtr slot)
at PIR_Test.MainPage..ctor() in C:\Users\Richard\Documents\Visual Studio 2017\Projects\Raspberry Pi Tests\PIR Test\MainPage.xaml.vb:line 22
Can someone shed any light on how to get the event trigger in VB.NET for a GPIO pin on a Raspberry Pi running Windows 10 core IOT.
Thanks in advance.