Community

ParallaxPIR help

I’m trying to drive a solenoid after motion has started, problem is whenever event handler fires function is runs it 3 times every time…why is this?
I only need the function run once per OnMotionStart event.

Any help on this??

Ill drop the relevant code below:

public class App
{
//Initialize output pins for water valves
OutputPort out1 = new OutputPort(Pins.GPIO_PIN_D0, false);
OutputPort out2 = new OutputPort(Pins.GPIO_PIN_D1, false);
OutputPort outIR = new OutputPort(Pins.GPIO_PIN_D2, false);
OutputPort out3 = new OutputPort(Pins.GPIO_PIN_D3, false);
OutputPort out4 = new OutputPort(Pins.GPIO_PIN_D4, false);
//Initialize HBridge for first two water valves
//HBridgeMotor valve1 = new HBridgeMotor(PWMChannels.PWM_PIN_D5, PWMChannels.PWM_PIN_D6, Pins.GPIO_PIN_D7);

    public App()
    {
        InitializePeripherals();
    }
    public void InitializePeripherals()
    {
        //setting up all the sensors with event handlers
        ParallaxPIR motion1 = new ParallaxPIR(Pins.GPIO_PIN_A0);
        ParallaxPIR motion2 = new ParallaxPIR(Pins.GPIO_PIN_A1);
        ParallaxPIR motionIR = new ParallaxPIR(Pins.GPIO_PIN_A2);
        ParallaxPIR motion3 = new ParallaxPIR(Pins.GPIO_PIN_A3);
        ParallaxPIR motion4 = new ParallaxPIR(Pins.GPIO_PIN_A4);
        //valve1.IsNeutral = true;
        

        motion1.OnMotionStart += Motion1_OnMotionStart;
        motion1.OnMotionEnd += Motion1_OnMotionEnd;
        motion2.OnMotionStart += Motion2_OnMotionStart;
        motion2.OnMotionEnd += Motion2_OnMotionEnd;
        motion3.OnMotionStart += Motion3_OnMotionStart;
        motion3.OnMotionEnd += Motion3_OnMotionEnd;
        motion4.OnMotionStart += Motion4_OnMotionStart;
        motion4.OnMotionEnd += Motion4_OnMotionEnd;
        motionIR.OnMotionStart += MotionIR_OnMotionStart;
        motionIR.OnMotionEnd += MotionIR_OnMotionEnd;
        //Thread.Sleep(Timeout.Infinite);
    }
    private void ToggleValve4()
    {
        
            Thread.Sleep(50);
            out3.Write(true); //open valve for 50 ms                               
            Thread.Sleep(50);
            out3.Write(false); //finish on pulse

            Thread.Sleep(2000); //let valve stay open for 2 seconds

            out4.Write(true);     //off pulse
            Thread.Sleep(50);
            out4.Write(false);
          
    }
 
    private void MotionIR_OnMotionEnd(object sender)
    {
        outIR.Write(false);
        Debug.Print("MotionIR..................motion");
        //throw new NotImplementedException();
    }

    private void MotionIR_OnMotionStart(object sender)
    {
        ToggleValve4();
       
    }

          
    public void Run()
    {
        
    }
}