Sensor Change Event programming

Supporting Visual Studio on Windows
gtdixon
Phidgetsian
Posts: 7
Joined: Fri Feb 09, 2007 9:35 am
Contact:

Sensor Change Event programming

Postby gtdixon » Wed Jul 06, 2011 7:25 am

A novice programmers question.

I wanted to detect a motion sensor change , capture the sensor value, test the value and if appropriate open up a programme which records video from an SBC card.
I could not figure out how I could use Phidgets sensor change events so resorted to polling motion sensor every couple of seconds and, if motion was detected, switching on the recording program.
After capturing 30 seconds of video polling begins again and switches off the recording when there is no motion. Code is as follows;

In Main after phidget attached;

do
{
motion = ifKit.sensors[7].Value;
Thread.Sleep(2000);
}
while (motion<550 && motion >450);
//open record by sending batch file to commmand line
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "c:\\cphidget\\muxogg.bat";
proc.Start();
//record = true;
//record for 30 sec
Thread.Sleep(30000);

do
{
motion = ifKit.sensors[7].Value;
Thread.Sleep(2000);
}
while (motion < 450 || motion > 550);
//close record by batch file shutting down video record program
System.Diagnostics.Process proc1 = new System.Diagnostics.Process();
proc1.EnableRaisingEvents = false;
proc1.StartInfo.FileName = "c:\\cphidget\\taskkill.bat";
proc1.Start();
//record = false;






It would be much more elegant if I could detect the first execution of the sensor which confirms movement and start recording, whilst ignoring further sensor change events until the sensor values show no significant movement.
I cannot do this because further sensor changes trigger more instances of "start recording". When I try to set a flag of record=true after first instance, this is ignored when new sensor change detected.

Your advice and if possible code example would be appreciated.

erik
King of the Lab
Posts: 476
Joined: Fri Mar 06, 2009 12:42 pm
Location: Calgary, Canada
Contact:

Re: Sensor Change Event programming

Postby erik » Wed Jul 06, 2011 8:51 am

This may just be because all the code wasn't included, but it doesn't look like there is an if-statement on the record flag.

Take a look at the SensorChangeEvent in the InterfaceKit example. This is where you can set your flags, and start a new thread that will start the new recording process. Here's some pseudo-code:

Code: Select all

bool currently_recording = false;    //global

Main()
{
    Set-up Phidgets and event handlers;
    Phidget open();
    waitForAttachment();
    while(true) {  sleep(5 seconds); };  //empty while loop to keep program running infinitely and keeping CPU usage down

    return;
}

SensorChangeEvent(EventArgs)
{
    if(!currently_recording)
    {
        currently_recording = true;
        Thread StartRecording = new Thread(RecordThread);
        StartRecording.Start();
    }
}

RecordThread()
{
    OpenNewProcess();
    Thread.Sleep(30 seconds);
    CloseNewProcess();
    currently_recording = false;
}



While the camera is recording, SensorEvents might fire but since the flag is still set, it skips creating the new process. When 30 seconds have elapsed, then the flag is cleared and recording can begin at the next SensorEvent.

Also, you can increase the sensitivity property of the sensor and/or the DataRate to play with what change in SensorValue will result in an event being fired.

gtdixon
Phidgetsian
Posts: 7
Joined: Fri Feb 09, 2007 9:35 am
Contact:

Re: Sensor Change Event programming

Postby gtdixon » Thu Jul 07, 2011 2:38 am

Thanks for the reply so promptly. I had tried doing something similar to what you suggest but have problems declaring a global boolean variable eg currently_recording.
When I use an if statement for currently_recording it is not recognised and I get back an error: an object reference is required for the nonstatic field, method or property...
Seems global variables not acceptable in C#.


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 0 guests