Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that's having problems.
Its suppose to read the input from a controller then when the controller is on, it does X in a loop checking every 5mins.
Problem is I get faulty readings from the controller every now and then.
So what I want to do, is add a little counter so it checks it 4 times
and only when it gets 4 in a row it does x.
something like for (int i = 0; i < 4; i++)

into this

C#
public static void Method_6()
{
   Thread.Sleep(2 * HourMs);
   while((method_state_6) && (!phcontroller.Read())) //phcontroller is just a relay that closes when needed
   {
      ph.Write(false);
      Thread.Sleep(2000);
      ph.Write(true);
      Thread.Sleep(5 * MinuteMs);
   }
}


I'm a new and a little lost how to do it
Posted
Updated 4-Dec-12 5:01am
v2

1 solution

You can try something like:
C#
int i = 0;
bool flag = true;
while (i < 4 && flag)
{
flag = inputSource;
}

if (flag)
do X


If flag is true 4 times in a row, it will perform X.
 
Share this answer
 
Comments
ve3tru 4-Dec-12 16:41pm    
OK thanks but I'm a bit of a newb and not sure I understand your whole code.
flag = inputSource; lost me there whats an inputSource
also I guess I need it to wait some between checks. Maybe a min. or so to make sure its not still in a glitch.
thanks for the help
wizardzz 4-Dec-12 16:52pm    
I meant by "inputsource" as your check, assuming it returns boolean (that is what I interpreted by a check). Just put a sleep in the while loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900