Doubt about the use of interruptions


Considering a scenario where I have a wheel encoder that increments a counter through the use of interrupts. What happens if at some point I need to take a distance reading with an HC-SR04 sonar (which has a considerable delay)? Will I miss wheel encoder events during this time? Or would my HC-SR04 distance readings be compromised by wheel encoder interrupts?


If you’re using pulseIn() to get the distance, this function does not block interrupts, so your ISR counting will work. Of course it takes a small bit of time away from the other code and this could have an impact on the precision of the reading. The more you call the ISR during the measurement, the more likely you’ll have a bit of an impact… should not be massive though if you spin the encoder at reasonable speed

You won't miss any encoder events. The ISR should be basically be EncoderCount++, so there's little prospect of it interfering with your sonar either.

the encoder is connected to a DC motor, it would be around 2000 interrupts per second. Each interrupt does a count++

As a comparison, Timer0 interrupts run every ms (roughly) - and your arduino seems to work right ?

apparently is working. Are you trying to say that 2000 interrupts per second is too much for the Arduino?

No, he's saying the opposite. The 1ms interrupt from Timer0 has no adverse effect on the Arduino, so it's unlikely a 0.5ms interrupt will, either.

A millisecond is a long time in Arduino-land.