此代码用于评估计时器提供的信息。我希望它在计时有效时激活,并且我希望计时器在某些条件发生时重置。我希望代码不断检查这些参数。x、a 和 b 不断变化。
Stopwatch sw = new Stopwatch(); //sets sw as the stopwatch variable
sw.Start(); //starts stopwatch
for (int i = 0; ; i++) //counter
{
if (i % 1000000 == 0) //sets counter limit
{
sw.Stop(); //stops stopwatch
if (sw.ElapsedMilliseconds >= 3000) //when the elapsed time is > 3 secs
{
comPort.WriteLine("1"); //sends a 1 to an arduino
break; //breaks for loop
}
else if (sw.ElapsedMilliseconds < 3000) //when the elapsed time is < 3 secs
{
if ((Math.Abs(x-a) < 150) && (Math.Abs(x-b) < 150)) //criteria to start
{
sw.Start(); //continues stopwatch
}
else if ((Math.Abs(x-a) > 150) && (Math.Abs(x-b) > 150)) //criteria reset
{
sw.Reset(); //resets stopwatch
break; //breaks for loop
}
}
}
}