1

侵入を検出する PIR モーション センサーを使用する簡単なアラーム プログラムを作成しています。

私の予想されるロジックは、センサーのトグル ボタンをオンまたはオフにしようとしています。デフォルトでは、最初のクリックがクリックされない限り、センサーは非アクティブ/オフになっている必要があります。その場合、センサーはアクティブになります。

これがコードです

public class Program
{
    static InterruptPort motionSensor = new InterruptPort(Pins.GPIO_PIN_D0, false, ResistorModes.Disabled, InterruptModes.InterruptEdgeLow);

    static OutputPort redLED = new OutputPort(Pins.GPIO_PIN_D9, false);
    static OutputPort greenLED = new OutputPort(Pins.GPIO_PIN_D6, false);
    static OutputPort blueLED = new OutputPort(Pins.GPIO_PIN_D5, false);

    static InterruptPort onboardBtn = new InterruptPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled, InterruptModes.InterruptEdgeLow);
    static bool onOff;

    public static void Main()
    {
        onboardBtn.OnInterrupt += new NativeEventHandler(onboardBtn_onInterrupt);
        motionSensor.OnInterrupt += new NativeEventHandler(motion_onInterrupt);

        while (true)
        {
            Thread.Sleep(Timeout.Infinite);
        }
    }

    static void onboardBtn_onInterrupt(UInt32 data1, UInt32 data2, DateTime Timeout)
    {
        onboardBtn.DisableInterrupt();

        //Motion Sensor
        if (onOff == false)
        {
            motionSensor.DisableInterrupt();
            onOff = true;
        }
        else
        {
            motionSensor.EnableInterrupt();
            onOff = false;
        }

        onboardBtn.EnableInterrupt();
    }

    static void motion_onInterrupt(uint data1, uint data2, DateTime time)
    {

        motionSensor.DisableInterrupt();

        Debug.Print("Movement found ");

        int i = 0;
        while (i < 5)
        {
            blueLED.Write(true);
            Thread.Sleep(100);

            blueLED.Write(false);
            Thread.Sleep(50); 

            redLED.Write(true);
            Thread.Sleep(100);

            redLED.Write(false); 
            Thread.Sleep(50);

            i++;
        }
        motionSensor.EnableInterrupt();

    }


}

実際の結果は次のとおりです。センサーはデフォルトでアクティブです。最初のクリック、まだアクティブです。2 回目のクリック、まだアクティブです。3 回目のクリックで非アクティブになり、その後のクリックは正常に機能します (オンとオフ)。

何が起こっているのか分かりますか?私は何時間もこれを理解しようとしてきました

4

1 に答える 1

1

どちらかセット

static bool onOff = true;

最初にセンサーをアクティブにしたい場合。

またはMain() で:

motionSensor.DisableInterrupt();

最初にセンサーを非アクティブにしたい場合。

于 2015-01-11T16:27:25.280 に答える