Is there problems to use millis() and micros() in the interrupt method?

Board Uno.
I need to measure the time for a wheels rotation. I have a hall effect sensor to pin 2 and use interrupt.
The question is: How is it with the millis() and micros()? I have read that there is problem to use this in the interrupt method!?
In my solution I have micros() in the interrupt method and everything seems to function OK, and for a very long time two days.
How is it?
Perhaps I should use noInterrupts(); and interrupts(); to prevent timing fault?

attachInterrupt(0, pin_HS, FALLING);

void pin_HS() {

timeHsInterval = (micros() - timeHsIntervalStart);
timeHsIntervalStart = micros();
}

Regard
Ola A.

How is it with the millis() and micros()?

The functions are just fine. They had a nice Christmas in Hawaii, lying on the beach soaking up the sun.

I have read that there is problem to use this in the interrupt method!?

Is that a statement or a question? Questions end with question marks. You can’t expect us to know what you read, or how accurately you acquired the information that you read.

Statements do NOT end with question marks.

The values returned by millis() and micros() do not change during an ISR, so you can’t use them to delay. But, the value returned will be accurate as of when the ISR was called. That is one reason why ISRs should be lightening fast. ALL that your ISR should be doing is noting that, and maybe when, a pulse occurred.

Perhaps I should use noInterrupts(); and interrupts(); to prevent timing fault?

That statement suggests that you do not understand interrupts. So, no, you should NOT do that.

Thanks for your answer. Then I now I'm in the right direction with my project.

About: "I have read that there is problem to use this in the interrupt method!?", "Is that a statement or a question?" How do you interpret it? :slight_smile: It's up to you...

I'm glad your friends millis() and micros() have a nice time. :slight_smile:

Regard
Ola A.

@Olaab Can you post your full code!
It is never recommended to Use functions in Interrupt until there is no other way!
And as per your statement, You can measure rotation time of wheels without using millis() or micros() .
So if you provide your entire code some one could direct you in right path!

It is never recommended to Use functions in Interrupt until there is no other way!

Nonsense. There are plenty of functions that are perfectly acceptable to use in an ISR.

@Olaab, timeHsInterval will occasionally be short by 4 because of a bug in your code.

ok! PaulS, there you can see that there are many opinions on what you can use and not use when using interrupts.

About the code. I am naturally curious about other solutions.

I use this today and so far the time is correct.

Coding Poor.
What do you mean about "timeHsInterval will occasionally be short by 4 because of a bug in your code." Can you please explain?

The code is now some modified

This is the code:

const int hsPin = 2;

volatile unsigned long sensTimeMicro;
long timeHsIntervalStart = 0;
volatile double timeHsInterval = 0;

void setup() {

pinMode(hsPin, INPUT);

attachInterrupt(0, pin_HS, FALLING);

}

void loop() {
}

void pin_HS() {

noInterrupts();

sensTimeMicro = micros();

timeHsInterval = (sensTimeMicro - timeHsIntervalStart);
timeHsIntervalStart = sensTimeMicro;

interrupts();

}

Thanks for all help
Ola A.

void pin_HS() 
{
  noInterrupts();  // <<<<<<<<<< A terrible idea.  Remove this line.

  sensTimeMicro = micros();
        
  // It takes time to execute the following line of code.

  timeHsInterval = (sensTimeMicro - timeHsIntervalStart);

  // Savng the value of micros at the start ensures a single clean fence post.  Original bug fixed.

  // However, sensTimeMicro should be local to this function.

  timeHsIntervalStart = sensTimeMicro;

  interrupts();  // <<<<<<<<<< A terrible idea.  Remove this line.
}

Please tell me more about the “interrupts(); // <<<<<<<<<< A terrible idea.” . I want to learn of course. I’m glad for all constructive input I can get :slight_smile:

//
Ola A.

Actually, disabling interrupts in an ISR won't hurt anything, since they are disabled already. But, it is useless.

Re-enabling them is possible, and sometimes even valid and necessary, but it is NOT something that a newbie without a thorough understanding of the pitfalls of doing so should be doing.

I am guessing you don't really need to disable interrupts.
If you really care about accuracy, then measure the time for, let's say, 8 revolutions of the wheel, then divide that number by 8. Chances are, that is good enough for what you are trying to do.

OK then! I feel everything is under control now. I going on with my project.
Tank you for feedback.
Have a nice New Year.

Regard
Ola A.

http://www.gammon.com.au/interrupts