I apologize in advance that the title might be very confusing, but I wasn't sure how else to explain this due to my English issue.
I have a form application written in C# using VS2008, and it constantly reads data from an external device. The data read is either 0 (OFF) or 1 (ON). Most of the time, it stays 0, but when something occurs in the system, it becomes 1 and stays 1 for 5 seconds and goes back to 0.
What my program needs to do is to always watch the value change from 0 to 1, and count the number of the occurrences of catching the 1.
The problem is, sometimes the external device has a mistake and changes the value from 0 to 1 on accident for a second or less.
My program needs to ignore and not count the occurrence if the value change from 0 to 1 lasted for less than 1 second, and accept and count the occurrence if the value change from 0 to 1 lasted for 2 seconds out of the 5 seconds life.
I am thinking that basically I can just increment the count only if it stays 1 for more than 2 seconds, and do nothing otherwise.
I tried to use Thread.Sleep(2000)
, but it does not work, and I don't think this is the right way, but I haven't found a solution to achieve this.
private int data; //will contain the data read from the ext. device
private int occurrence = 0;
//Tick runs every 100 seconds
private void MyTick(object sender, EventArgs e)
{
//if data becomes 1
if(data == 1)
{
Thread.Sleep(2000); //wait 2 seconds??? does not work
//if the data stays 1 for 2 seconds, it is a valid value
if(?????)
{
occurrence++; //count up the occurrence
}
}
}
Can someone please give me some advice on what I can do to achieve this?