侵入を検出する 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 回目のクリックで非アクティブになり、その後のクリックは正常に機能します (オンとオフ)。
何が起こっているのか分かりますか?私は何時間もこれを理解しようとしてきました