Driving a stepper motor with ISR. Issues explained in comment.

I appreciate the template you provided. I adapted it for my purposes but even when I change your time between steps from 25 to 7.75 which should give me close to 1.55 RPS (93 RPM), I'm still not getting anywhere close to that speed.

Attached is my code,,

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 3;
byte stepPin = 2;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 7.75; // milliseconds

void setup() {

     Serial.begin(9600);
     Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(ledPin, OUTPUT);
     
     
}

void loop() {
  
    digitalWrite(directionPin, HIGH);
    curMillis = millis();
    singleStep();
   
}


void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        Serial.print("HiGH");
        digitalWrite(stepPin, LOW);
        Serial.print("Low");
    }
}

and I'll also attach pictures of my fixture and stepper, probably won't matter much but the reason I'm attaching them is I'm starting to think I may have a problem with the driver inside the fixture.

Wondering if anyone has a 200 step stepper and driver to test my code. I just ordered another one I can test on but hopefully if someone has something handy they can give me some insight.

Appreciate the help.

I change your time between steps from 25 to 7.75 which should give me close to 1.55 RPS (93 RPM), I'm still not getting anywhere close to that speed.

Your math does not seem correct.

@200 steps/rev you want 1.55x200 = 310 steps/second which is 3336 microseconds between steps.

unsigned long millisBetweenSteps = 7.75; // milliseconds

You can not use a decimal fraction with an unsigned long, it actually will truncate to 7.

Use microseconds for values in your code.

The serial printing at 9600 baud slow and will slow down the program. Use 115200 or else leave it out entirely.

justconmac:
I appreciate the template you provided. I adapted it for my purposes but even when I change your time between steps from 25 to 7.75 which should give me close to 1.55 RPS (93 RPM), I'm still not getting anywhere close to that speed.

What speed are you getting?

You can't use fractional millisecs because it works in whole numbers. It probably treats 7.75 as 7. If you need more precision you must use micros().

At 1 step every 7 millisecs you get 1000/7 = 142.9 steps per second. With 200 steps per rev that means 0.71 RPS if my maths is correct.

...R

I don't have the EXACT rps but it is pretty close to 10 seconds per revolution. I'll have to redo my math and implement the micros().

Also if my target is 1.55 rps and I have 200 steps to get through wouldn't I want 7.75 milliseconds between steps (1.55/200 = .00775)?

No. It is better to think in terms of steps per second, and to consider your unit analysis

(1.55 revolutions/second ) * (200 steps/revolution) = 310 steps/second = 3.23 ms between steps.

What you are doing is

(1.55 revolutions/second) / (200 steps/revolution) and the value of .00775 has units of ??

I know that's why I deleted the post after I thought about exactly that. Appreciate you responding to that though.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.