control stepper motor

hello,how to control a stepper motor without using any delay funtion?

There are two completely separate questions in that.

Do you know how to control a stepper motor using delay() functions? If not, start with that because it is easier. Look at the example Stepper sketches.

Do you know how to replace delay() functions with millis()? If not, look at the Blink Without Delay example sketch.

...R

TO R yeah,i know how to control a stepper motor using delay() functions

i just looted at millis().do you mean using division and if to simulate PWM?

PWM? Its a stepper, not a DC motor.

When you used delay() you just tell the Arduino to go to lunch for (say) 500 millisecs.

The idea with millis() is to record the time at the start of an event and then periodically check whether 500 millisecs has elapsed. If it has do whatever you need - and maybe record the time again for the next event.

Imagine you are reading a book while waiting for a chicken to cook. You know it will be done at 18:30 so every so often you raise your eyes from the book to check the time.

Something like this pseudo code

``````unsigned long interval = 500;
unsigned long startMillis = millis();

unsigned long currentMillis();

void loop() {
currentMillis = millis();
if (currentMillis - startMillis > interval) { // time has elapsed
// do stuff
}
// do other stuff

}
``````

...R

TO Mark a stepper motor need the pulse to drive a motor,isn't it? so i use delay function to simulate a PWM function

but i need to write some algorithm like fuzzy,so i can't let the system hold on over there or you have another method?

TO R
i need to write some algorithm like fuzzy,so i can’t let the system hold on over there
is your method is ok for me?

sorry everybody my english is not well please try to understand what i mean

Yes, you use the Arduino to produce pulses to control a stepper motor but they are NOT pwm pulses. PWM is often used to control the speed of a DC motor. It is also used by stepper motor driver boards to limit the current flowing through a stepper motor, but that process is invisible to the Arduino.

Sorry, I don't understand what you mean by

i need to write some algorithm like fuzzy,so i can't let the system hold on over there is your method is ok for me?

...R

if i use delay function,arduino can't do anything else when it enter delay(), is it? my sensor return value,and i need to use the value to calculate,then give the motor a speed but if arduino enter delay(),it can't give the motor a speed immediately

or am i think too much?

Re-read reply #4 above and look at the BlinkWithoutDelay example to see how to work without delay.

PWM normally means constant frequency signal with variable pulse-width or duty-cycle, not a stream of pulses that can change rate. I'd call the latter a variable-rate pulse-stream perhaps.

The normal way to control a stepper is with the Stepper or AccelStepper libraries which handle the timing for you - you can look at how they work for inspiration and ideas.

Incidentally,is the example "stepper.h" using delay() funtion?

winsonkao: Incidentally,is the example "stepper.h" using delay() funtion?

I haven't tried the Stepper library (just wrote my own code) but someone else said it does block everything until it has completed the move. I believe the AccelStepper library doesn't block things.

...R

unsigned long interval = 500; unsigned long startMillis = millis();

unsigned long currentMillis();

void loop() { currentMillis = millis(); if (currentMillis - startMillis > interval) { // time has elapsed // do stuff } // do other stuff

}

run2.ino: In function 'void loop()': run2:7: error: assignment of function 'long unsigned int currentMillis()' run2:7: error: cannot convert 'long unsigned int' to 'long unsigned int ()()' in assignment run2:8: error: pointer to a function used in arithmetic run2:8: error: ISO C++ forbids comparison between pointer and integer

what happened?

If you look back to my post you will see that I described my contribution as "pseudo code" which meant that I didn't take any care to see it would compile - it was just meant to give you a general idea from which you could develop a workable program.

The biggest problem you should easily have spotted - there is no setup() function. This probably caused the compiler to go crazy.

...R