I am making small lidar scanner and I plan to move stepper motor to 360 degrees clockwise and 360 degrees counterclockwise, without delay by using interrupt.
The number of steps per seconds are irrelevant for me.
But I don't understand how implement it with interrupt.
It's no different in or out of an interrupt. You will need a variable to remember and set the direction at the appropriate time (which you have to define). What I would do, is Stepper.step(amount) and then set amount = 1 or amount = -1 depending on which direction you want to go.
mrsrdjani:
I am making small lidar scanner and I plan to move stepper motor to 360 degrees clockwise and 360 degrees counterclockwise, without delay by using interrupt.
The number of steps per seconds are irrelevant for me.
The code in the second example in the link I gave you in Reply #1Simple Stepper Code should be able to do that perfectly well without the complexity of a hardwareTimer or interrupts.
Robin2:
The code in the second example in the link I gave you in Reply #1Simple Stepper Code should be able to do that perfectly well without the complexity of a hardwareTimer or interrupts.
...R
Thank you Robin, I know for your code, and it works same thing like my interrupt. Problem is how to make loop for alternating change direction when it cross n steps.
Try this. Look carefully at all the changes I have made from your version.
void loop() {
curMillis = millis();
static char direction = 'F';
// the word static causes the value to be remembered between calls to the function
static unsigned long stepCount = 0;
unsigned long maxSteps = 400; // change to what ever value you need
unsigned long millisBetweenSteps = 100; // 10 steps per second
static unsigned long lastStepMillis;
if (direction == 'F') {
digitalWrite(directionPin, HIGH);
}
else {
digitalWrite(directionPin, LOW);
}
if (curMillis - lastStepMillis < millisBetweenSteps) {
lastStepMillis = curMillis;
singleStep();
stepCount ++;
}
if (stepCount >= maxSteps) {
if (direction == 'F') {
direction = 'R';
}
else {
direction = 'F';
}
stepCount = 0;
}
}
mrsrdjani:
I never found any similar code and my question is why does static make sense?
In C++ variables are either defined globally (those at the top of the program before setup() ) or locally (those defined within a function). Global variables are available everywhere throughout a program but local variables are only available within the function where they are defined. Normally local variables are created afresh every time the function is called so they cannot remember the value from the previous time the function was called. Using the static keyword tells the compiler that you want the values in the local variable to be retained between calls to the function.