From 1064554b87274f4136460312286ff89b3b94f034 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 28 May 2015 10:28:27 +0200 Subject: [PATCH] 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. --- libraries/Stepper/src/Stepper.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/Stepper/src/Stepper.cpp b/libraries/Stepper/src/Stepper.cpp index 57a7fefab..814149915 100644 --- a/libraries/Stepper/src/Stepper.cpp +++ b/libraries/Stepper/src/Stepper.cpp @@ -195,10 +195,11 @@ void Stepper::step(int steps_to_move) // decrement the number of steps, moving one step each time: while(steps_left > 0) { + unsigned long now = micros(); // move only if the appropriate delay has passed: - if (micros() - this->last_step_time >= this->step_delay || micros() < this->last_step_time) { + if (now - this->last_step_time >= this->step_delay) { // get the timeStamp of when you stepped: - this->last_step_time = micros(); + this->last_step_time = now; // increment or decrement the step number, // depending on direction: if (this->direction == 1) {