Stepper motor control with serial monitor

Hello friends,
I have just started programming with Arduino,
I try to control my stepper motor with the arduino serial Monitor. the control is to run the motor in two different directions and stop it spontaneously.
The motor will only stop when the required number of revolutions has been reached.
My problem is that I can’t change the direction and stop the motor when I want to.
help:D

//Include the Arduino Stepper Library
#include <Stepper.h>
 
// Define Constants
 
// Number of steps per internal motor revolution 
const float STEPS_PER_REV = 32; 
 
//  Amount of Gear Reduction
const float GEAR_RED = 64;
 
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
 
// Define Variables
 
// Number of Steps Required
int StepsRequired;
 
// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11 
// Connected to ULN2003 Motor Driver In1, In2, In3, In4 
// Pins entered in sequence 1-3-2-4 for proper step sequencing
 
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
 
void setup()
{
  while (!Serial);
  Serial.begin(9600);

}
 
void loop()
{
while (Serial.available())
 {
 char Direction=Serial.read();
Serial.println(Direction);

 if (Direction=='R')
 {
  Serial.println(" CW");
  StepsRequired  =  STEPS_PER_OUT_REV ; 
  steppermotor.setSpeed(700);   
  steppermotor.step(StepsRequired);
  }

if (Direction== 'V')
 {
 Serial.println("CCW");
  // Rotate CCW 1/2 turn quickly
  StepsRequired  =  - STEPS_PER_OUT_REV*2;  
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
 
}
 if (Direction== 'S')
 {
 Serial.println("Stop");
  StepsRequired  =  0;  
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);

 
}

 }
 }

Consider getting a copy of the Arduino Cookbook, It should have what you are trying to do. You may have to use parts of more than one sketch. Posting your schematic, not a frizzy thing showing all power and ground connections will help a lot. Links to the parts would help a lot, there are thousands of stepper motors that are controlled with an Arduino, which do you have?

The Stepper library steps() function blocks. That means that nothing can happen till the command finishes.

You have a couple of choices.

One is, using the Stepper library, to step the motor one step at a time and check for serial input and act upon the input if present between steps. Keep track of the number of steps so you will know when the stepper has reached its target position.

Or use the non-blocking methods in the AccelStepper library. See the examples. The library is available via the IDE library manager.

Edit: Since it seems that this post has had some views of late I would like to add the MobaTools stepper library. It is a newer library that I have been looking at. It offers much of what the Accelstepper library does and is, I think, a bit easier to learn.

1 Like

I appreciate your help.
Thank you very much! :slight_smile: :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.