O
O
okliman2019-08-13 16:32:29
Arduino
okliman, 2019-08-13 16:32:29

What code can replace pulsein?

Since "pulsein" pauses the whole code a bit, I need to replace it with some code that won't do that.
Help me please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Kolesnikov, 2019-08-13
@NikitOS_MV

While one code is running, the other is not running.
If you need to perform several tasks at the same time - use JS

T
tsarevfs, 2019-08-13
@tsarevfs

I do not guarantee that the solution is optimal, but you can try this:
By interrupt, subscribe to change the value on the port
https://www.arduino.cc/reference/en/language/funct...
In the handler function, remember the start time. When you call again, calculate the difference. The pseudocode is something like this:

unsigned long time = 0;
const int signalType = HIGH;
int signalDuration = 0;

void onSignal()
{
  if (signalDuration)
    return;

  if (digitalRead(inPin) == signalType)
  {
    time = micros();
  }
  else
  {
    signalDuration = micros() - time;
  }
}

void loop() {
  //...
  if (needToCalculateDuration)
  {
  	needToCalculateDuration = 0;
  	signalDuration = 0;
  	//attachInterrupt(...)
  }
  //...
  if (signalDuration)
  {
  	//detachInterrupt
  	doSmthWithDuration(signalDuration)
  }
  //...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question