Yes I have the code but it's a very long one and I can't figure out which parts of the code actually control the stepper speed.
Here is the part of the code that does step modification:
void setDelay() {
if(stepperSpeed == 0) {
step_delay = 1000000000;
}
else {
double tempDelay = double(1.25) / double(stepperSpeed);
step_delay = tempDelay;
}
return;
}
// Returns stepperSpeed using a given value for delay
double setSpeedFromDelay(double delayVal) const {
return double(1.25) / delayVal;
}
// Returns step_delay value
double getDelay() const {
return step_delay;
}
// Move the motor forward or backward
void step1(unsigned long &stepperTimeNew, unsigned long &stepperTimeOld) {
stepperTimeNew = millis();
if(step_number == 30000) {
step_number = 0;
}
// move only if the appropriate delay has passed:
if (double(stepperTimeNew - stepperTimeOld) >= step_delay && stepperSpeed != 0 && !checkOn()) {
// depending on direction:
if (neg == 1) {
stepMotor(step_number % 4);
}
else {
stepMotor(3 - (step_number % 4));
}
stepperTimeOld = stepperTimeNew;
step_number++;
}
return;
}
// Selects appropriate number of fraction steps to take based upon maximum speed
void selectStepMode() {
if(step_delay <= 1) {
// No step modification
digitalWrite(mode_pin1, LOW);
digitalWrite(mode_pin2, LOW);
digitalWrite(mode_pin3, LOW);
}
else if(step_delay <= 2) {
// Half step
digitalWrite(mode_pin1, HIGH);
digitalWrite(mode_pin2, LOW);
digitalWrite(mode_pin3, LOW);
step_delay = step_delay / 2;
}
else if(step_delay <= 4) {
// Quarter step
digitalWrite(mode_pin1, LOW);
digitalWrite(mode_pin2, HIGH);
digitalWrite(mode_pin3, LOW);
step_delay = step_delay / 4;
}
else if(step_delay <= 8) {
// Eigth step
digitalWrite(mode_pin1, HIGH);
digitalWrite(mode_pin2, HIGH);
digitalWrite(mode_pin3, LOW);
step_delay = step_delay / 8;
}
else if(step_delay <= 16) {
// Sixteenth step
digitalWrite(mode_pin1, LOW);
digitalWrite(mode_pin2, LOW);
digitalWrite(mode_pin3, HIGH);
step_delay = step_delay / 16;
}
else {
// 1/32 step
digitalWrite(mode_pin1, HIGH);
digitalWrite(mode_pin2, LOW);
digitalWrite(mode_pin3, HIGH);
step_delay = step_delay / 32;
}
}
I think the problem is that no timers are used in the code. It only uses step modification to change the speed. My max speed is 1.25 rev/sec. When I run at max speed everything's fine. When I turn the speed to 1.249 rev/sec the speed turns down to 0.625. Basically the motor runs with full-step at max speed and as soon as I try to run it at a slightly slower speed, the code starts running the motor at half-step and so on.
I thought this should work with the arduino's setspeed function. So I tried to compile the arduino example for stepper speed and I had the exact same problem. I suppose that aside from the code, something should also be wrong with motor drivers. Is that right? Isn't the setspeed function in stepper library supposed to give me a somewhat better resolution?
Thank you so much for the help. I'm not sure which parts of the code could actually help. Please let me know if you need any specific part of the code or else if you need the whole code.