Skip to content

Commit 1064554

Browse files
committed
Stepper: optimization on timing calculations
micros() is now called only once per cycle (instead of 3). The rollover check is superflous because the "last_step_time" field is unsigned.
1 parent 0546bf0 commit 1064554

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

libraries/Stepper/src/Stepper.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ void Stepper::step(int steps_to_move)
195195

196196
// decrement the number of steps, moving one step each time:
197197
while(steps_left > 0) {
198+
unsigned long now = micros();
198199
// move only if the appropriate delay has passed:
199-
if (micros() - this->last_step_time >= this->step_delay || micros() < this->last_step_time) {
200+
if (now - this->last_step_time >= this->step_delay) {
200201
// get the timeStamp of when you stepped:
201-
this->last_step_time = micros();
202+
this->last_step_time = now;
202203
// increment or decrement the step number,
203204
// depending on direction:
204205
if (this->direction == 1) {

0 commit comments

Comments
 (0)