 # How to avoid dividing by zero frequency

Hello,

So I have a code that runs a motor and has a potentiometer that changes the speed of the motor.

If the potentiometer is set to zero when turning on, the motor does not work as the potentiometer is turned.

If the potentiometer is not set to zero, then the motor starts at a speed related to how far the potentiometer is turned.

RPMs are not shown unless the potentiometer is turned to zero and then increased again.

If the potentiometer is originally set to display 85rpm, then the motor works.

If the potentiometer is originally set to less than 85rpm, then the display says “0” and the motor either spins slowly or does not spin.

Once the potentiometer is turned to zero and the motor stops, the motor stops working.

If the speed is set to zero quickly and then to a higher speed instantly, then the motor continues to work.

In the code I below, I think the problem is that I am dividing by the pulseIn() function. If the frequency is zero, then I am dividing by zero. How can I avoid this problem?

Thanks

Code.ino (2.45 KB)

If the divisor is zero, don't do the division. Example

``````if (y != 0) z = x/y;
``````