Convert frequency to delay formulae

Hello, I am having some trouble commanding a stepper. I use two wires (direction and step = sensPin and tactPin), a variable used for delay (pause) and a variable used for frequency (_freq). The frequency (like direction and number of steps) is provided via serial communication. All these variables are integers, the ones provided via serial are processed using atoi function. This is my code:

    //  command the stepper
    stepsDone = 0;
    pause = (1/_freq)*1000;
    Serial.print(" pause: ");
    Serial.println(pause);
    //  sens
    digitalWrite(sensPin, _dir);
    //  tact
    while (stepsDone <= _steps) {
      digitalWrite(tactPin, LOW);
      digitalWrite(led, LOW);
      delay(pause);
      digitalWrite(tactPin, HIGH);
      digitalWrite(led, HIGH);
      delay(pause);
      stepsDone++;
      if (Serial.available() > 0) {
        digitalWrite(tactPin, LOW);
        digitalWrite(led, LOW);
        stepsDone = 0;
        break;
      }
    }

The turning led on and off is the check of this program. The problem is that conversion formulae ( pause = (1/_freq)*1000;) it's not working properly. If I am providing the value 1 for frequency (that means 1 step by one second), the pause variables works fine and receives the value of 1. The led blinks properly by 1 second. If I am providing value 2 (or other value) for frequency, the pause variable gets 0, I don't know why, because it should get 500 (that's half of a second because frequency 2 means 2 steps in a second). I have tried using float and then parse, but I get the same, 0.00 and 0.

The if (Serial.available) is just used for stopping the stepping, but I don't see any harm in using it. All code seems right, but it doesn't working. My second question is how to use delayMicroseconds() for higher frequencies? Should I use an if statement or just use this function instead delay(), but I have read in the specification that it can not handle properly values higher than 16383.

Thank you.

This is my code:

sp. "This is part of my code:"

but I have read in the specification that it can not handle properly values higher than 16383.

So, use "delayMicroseconds" when it is appropriate and safe to do so, and "delay" for other situations.

    pause = (1/_freq)*1000;

1 is an int. 1000 is an int. If _freq is an int, then 1/_freq is going to be 0 for any value of _freq other than 1.

    pause = (1.0/_freq) * 1000.0;

would produce different results.

fulminator:
The problem is that conversion formulae ( pause = (1/_freq)*1000;) it's not working properly. If I am providing the value 1 for frequency (that means 1 step by one second), the pause variables works fine and receives the value of 1. The led blinks properly by 1 second. If I am providing value 2 (or other value) for frequency, the pause variable gets 0, I don't know why, because it should get 500 (that's half of a second because frequency 2 means 2 steps in a second).

When you're doing integer arithmetic, the order that you evaluate expressions matters. There is a big difference between
pause = (1/_freq)*1000; and pause = 1000/_freq;

Also note that your approach of pausing for the step period will cause the speed to be too low since you don't account for the execution time of the loop. If you want accurate speed it's better to record the time of the last step and wait until the elapsed time exceeds your interval. After you have taken the step, increment the 'last step time' by the interval so that the next step is timed based on when the previous step was due, not when it actually happened. That way you will avoid timing slip and maintain an accurate output frequency.

Thank you very much for the info. Formulae for frequency works now.

Can you provide, please, more on counting the execution of the loop? I should be using millis() ? Can you please provide code?

And can someone tell me how to use delayMicroseconds() for higher frequencies?

Thank you.