我正在编写一个简单的警报程序,该程序使用检测入侵的 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();
}
}
实际结果是:传感器默认处于活动状态。第一次单击,仍然处于活动状态。第二次点击,仍然有效。第三次点击,不活动,随后的点击按原样工作(打开和关闭)。
知道发生了什么吗?几个小时以来我一直在试图解决这个问题