From 8825bb0d92b2b4c9634223fdf01ffd4113f2e7c9 Mon Sep 17 00:00:00 2001 From: fduraibi Date: Sun, 7 Sep 2025 00:21:56 +0300 Subject: [PATCH] A new function to idle the motor after stepping A new function that allow setting the idle option to power down all the motor coils after stepping, in order to reduce power and heat. That also allows manual rotation of the motor. --- keywords.txt | 1 + src/Stepper.cpp | 27 +++++++++++++++++++++++++++ src/Stepper.h | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/keywords.txt b/keywords.txt index fd0db3e..e110ff4 100644 --- a/keywords.txt +++ b/keywords.txt @@ -15,6 +15,7 @@ Stepper KEYWORD1 Stepper step KEYWORD2 setSpeed KEYWORD2 version KEYWORD2 +idelAfterStep KEYWORD2 ###################################### # Instances (KEYWORD2) diff --git a/src/Stepper.cpp b/src/Stepper.cpp index 148de03..98fbf00 100644 --- a/src/Stepper.cpp +++ b/src/Stepper.cpp @@ -88,6 +88,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2) this->direction = 0; // motor direction this->last_step_time = 0; // timestamp in us of the last step taken this->number_of_steps = number_of_steps; // total number of steps for this motor + this->idle = false; // Power down all coils after done stepping // Arduino pins for the motor control connection: this->motor_pin_1 = motor_pin_1; @@ -118,6 +119,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, this->direction = 0; // motor direction this->last_step_time = 0; // timestamp in us of the last step taken this->number_of_steps = number_of_steps; // total number of steps for this motor + this->idle = false; // Power down all coils after done stepping // Arduino pins for the motor control connection: this->motor_pin_1 = motor_pin_1; @@ -150,6 +152,7 @@ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, this->direction = 0; // motor direction this->last_step_time = 0; // timestamp in us of the last step taken this->number_of_steps = number_of_steps; // total number of steps for this motor + this->idle = false; // Power down all coils after done stepping // Arduino pins for the motor control connection: this->motor_pin_1 = motor_pin_1; @@ -226,6 +229,21 @@ void Stepper::step(int steps_to_move) yield(); } } + // If ideling is enabled then power down all coils + if (this->idle == true) + { + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + if (this->pin_count == 4) + { + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, LOW); + } + if (this->pin_count == 5) + digitalWrite(motor_pin_5, LOW); + } } /* @@ -358,6 +376,15 @@ void Stepper::stepMotor(int thisStep) } } +/* + * Allow powering down all coils after stepping, + * to reduce heat, power and allow manual rotation. + */ +void Stepper::idelAfterStep(bool idle) +{ + this->idle = idle; +} + /* version() returns the version of the library: */ diff --git a/src/Stepper.h b/src/Stepper.h index b5578b4..0ea8aa8 100644 --- a/src/Stepper.h +++ b/src/Stepper.h @@ -95,6 +95,9 @@ class Stepper { // mover method: void step(int number_of_steps); + + // Power down all coils after stepping, to reduce heat, power and allow manual rotation. + void idelAfterStep(bool idle); int version(void); @@ -106,6 +109,7 @@ class Stepper { int number_of_steps; // total number of steps this motor can take int pin_count; // how many pins are in use. int step_number; // which step the motor is on + bool idle; // Power down all coils after done stepping // motor pin numbers: int motor_pin_1;