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.