2017-06-16 53 views
0

我正在通過Visual Studio和windows 10物聯網嘗試C#樹莓。只是讓我永恆,使一切工作,但我慢慢到達那裏。我在他們過時的2年老教程之後得到了我的LED工作。我現在試圖添加一個按鈕到我的麪包板來打開和關閉LED燈......可悲的是,這根本不起作用。這就像ValueChanged永遠不會被觸發。c中的樹莓3按鈕#

我遵循這個指南:https://developer.microsoft.com/en-us/windows/iot/samples/pushbutton

我的按鈕連接,如:1只腳直接接地,另一個到GPIO18(引腳12)

thequestion到來之前,是的,我曾嘗試使用GPIO5太。我剛回到GPIO18,因爲它之前正在處理我的Python腳本。

這是我試圖運行的代碼,但有按鈕的問題(LED被罰款):

public sealed class StartupTask : IBackgroundTask 
{ 
    private GpioController gpio = GpioController.GetDefault(); 
    private GpioPin pinRed; 
    private GpioPin pinBlue; 
    private GpioPin pinButton; 
    private GpioPinValue pinValue; 
    private const int BLUE_PIN = 19; 
    private const int RED_PIN = 26; 
    private const int BUTTON_PIN = 18; 

    public void Run(IBackgroundTaskInstance taskInstance) 
    { 
     Debug.WriteLine("initialising"); 

     InitGpio(); 
     if (pinRed != null) 
     { 
      pinValue = GpioPinValue.High; 
      pinRed.Write(pinValue); 
      pinBlue.Write(pinValue); 
     } 
    } 

    private void InitGpio() 
    { 
     gpio = GpioController.GetDefault(); 

     if (gpio == null) 
     { 
      pinRed = null; 
      pinBlue = null; 
      pinButton = null; 
      Debug.WriteLine("Failed starting GPIO"); 
      return; 
     } 

     pinValue = GpioPinValue.Low; 

     pinRed = gpio.OpenPin(RED_PIN); 
     pinRed.Write(pinValue); 
     pinRed.SetDriveMode(GpioPinDriveMode.Output); 

     pinBlue = gpio.OpenPin(BLUE_PIN); 
     pinBlue.Write(pinValue); 
     pinBlue.SetDriveMode(GpioPinDriveMode.Output); 

     pinButton = gpio.OpenPin(BUTTON_PIN); 
     if (pinButton.IsDriveModeSupported(GpioPinDriveMode.InputPullUp)) 
     { 
      Debug.WriteLine("Is supported"); 
      pinButton.SetDriveMode(GpioPinDriveMode.InputPullUp); 
     } 
     else 
     { 
      Debug.WriteLine("Not supported"); 
      pinButton.SetDriveMode(GpioPinDriveMode.Input); 
     } 

     pinButton.DebounceTimeout = TimeSpan.FromMilliseconds(50); 
     pinButton.ValueChanged += buttonValueChange; 

     Debug.WriteLine("GPIO initialised"); 
    } 

    private void buttonValueChange(GpioPin sender, GpioPinValueChangedEventArgs e) 
    { 
     Debug.WriteLine("here"); 
     if (e.Edge == GpioPinEdge.FallingEdge) 
     { 
      Debug.WriteLine("Button push"); 

      pinValue = (pinValue == GpioPinValue.Low) ? GpioPinValue.High : GpioPinValue.Low; 
      pinRed.Write(pinValue); 
      pinBlue.Write(pinValue); 
     } 
     else 
     { 
      Debug.WriteLine("Button release"); 
     } 
    } 
} 

是,大量的調試線,正如我所說,嘗試對樹莓C#(InputPullUp被支持)。 buttonValueChange中的「here」永遠不會被觸發。我在Python上完成了完全相同的佈線設置,並且在相同的引腳上工作完美無瑕。

+1

就在'pinButton.DebounceTimeout = TimeSpan.FromMilliseconds(50)'之前;'你能添加類似'var dm = pinButton.GetDriveMode();'並檢查實際設置了哪種驅動模式? –

+0

剛剛檢查過,它會返回:InputPullUp – Psychokiller1888

回答

0

這裏建議ValueChanged not firing with C# Win10 Iot「當Run方法結束時,除非創建了延遲對象,否則後臺應用程序結束。」

因此,你可以嘗試這行添加到您的Run代碼:

var deferral = taskInstance.GetDeferral(); 

,看看它是否有幫助。

+0

謝謝您的回答,但這並不能解決問題。事實上,我對此非常厭煩,以至於我在Visual Studio上嘗試了python,並在其下面爲我的raspi製作了一個快速按鈕項目,並且它的工作原理完美無瑕,並帶有同一個版面。 – Psychokiller1888