Monitor input for a given time interval

Hi, everyone!

I have to wait for an ACK after a command, that I expect to become high within 100us. If the signal won’t switch during this period, the communication failed.
First try is to use a “for” loop, with “max cycle count” calculated as number of clocks in 100us (given 16MHz clock). Something like

boolean gotACK = false;
for(int i = 0; i < MAX_ACK_DELAY; i++)
  if(digitalRead(ackPin) == HIGH)
    gotACK = true;

But the single “for” cycle needs more than a single clk cycle to run, since several operations are involved (increment index, check cycle termination, read digital signal).
Is there a more precise way to monitor the input for exactly 100us?

Thanks!
MIX

Is there a more precise way to monitor the input for exactly 100us?

Yes.

unsigned long start = micros();
gotAck = false;
while(micros() - start < 100)
{
   if(digitalRead(ackPin) == HIGH)
  {
    gotACK = true;
    break;
  }
}

Nice!
I’ll give it a try.

Thanks!

It worked well in managing the time interval length. By the way, the signal I have to monitor is a little too fast (a single 2 us pulse), and this method wasn't reliable (too many missed detection). I had to use interrupts to detect the pulse.

Thanks anyway! MIX