0

Controlling Stepper Motor with Arduino Serial Monitor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Controlling Stepper Motor with Arduino Serial Monitor

by hyunterx on Mon Aug 19, 2019 12:50 pm

Hi there, I am looking to control my stepper motor's speed with arduino's serial monitor, I am using UNO and Adafruit Motor Shield V2.3

There are 4 speed divided into 4 categories a, b, c and d, the idea is that when you press 'a' the stepper motor would rotate on the selected RPM, 'b' another RPM and so-on. Press 's' and motor would stop

Everything is fine when i press 'a'. However when I want to change to 'b' there isnt any response from the motor and it will continue rotating in 'a' setting.

Attached is my code for the above scenario. Please advice

Thank you

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


void setup() {
  Serial.begin(9600);     
  Serial.println("Stepper Test");

  AFMS.begin(); 
  //AFMS.begin(1000);
}

void loop() {

  char n;
  if (Serial.available() > 0)
   {
      int command = Serial.read();
   {

   if (command == 'a')
   {
    Serial.println("Setting Speed to 19.1 RPM");
    delay (1000);
      do
      {
        myMotor->setSpeed(19.1);
         myMotor->step(10000,FORWARD, DOUBLE);

      } while (Serial.available() == 0);
      Serial.read();   
   }
   else if (command == 'b')
   {
       Serial.println("Setting Speed to 38.2 RPM");
    delay (1000);
        myMotor->setSpeed(38.2);
        myMotor->step(10000,FORWARD, DOUBLE);
   }
   else if (command == 'c')
   {
    Serial.println("Setting Speed to 57.3 RPM");
    delay (1000);
    myMotor->setSpeed(57.3);
    myMotor->step(10000,FORWARD, DOUBLE);
   }
   else if (command == 'd')
   {
    Serial.println("Setting Speed to 76.4 RPM");
    delay (1000);
     myMotor->setSpeed(76.4);
    myMotor->step(10000,FORWARD, DOUBLE);
   }
 else if (command == 's')
   {
    myMotor->release();
  ;
   }
   }
}
}

hyunterx
 
Posts: 10
Joined: Thu Aug 15, 2019 11:38 am

Re: Controlling Stepper Motor with Arduino Serial Monitor

by adafruit_support_bill on Mon Aug 19, 2019 1:11 pm

Code: Select all | TOGGLE FULL SIZE
myMotor->step(10000,FORWARD, DOUBLE);

The "step" function is a blocking function. So it will not return until after the motor has completed the 10000 steps. If you want to remain responsive to serial input, you need to take one step at a time and monitor the serial input. The AccelStepper library is the easiest way to do that. There is a link to it here: https://learn.adafruit.com/adafruit-mot ... or-arduino

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling Stepper Motor with Arduino Serial Monitor

by hyunterx on Mon Aug 19, 2019 1:23 pm

adafruit_support_bill wrote:
Code: Select all | TOGGLE FULL SIZE
myMotor->step(10000,FORWARD, DOUBLE);

The "step" function is a blocking function. So it will not return until after the motor has completed the 10000 steps. If you want to remain responsive to serial input, you need to take one step at a time and monitor the serial input. The AccelStepper library is the easiest way to do that. There is a link to it here: https://learn.adafruit.com/adafruit-mot ... or-arduino

Essentially i want the motor to keep rotating until I input another setting. Using AccelStepper would mean that I would have to wrap the functiomn? May I ask what is wrap?

hyunterx
 
Posts: 10
Joined: Thu Aug 15, 2019 11:38 am

Re: Controlling Stepper Motor with Arduino Serial Monitor

by adafruit_support_bill on Mon Aug 19, 2019 1:39 pm

Have a look the AccelStepper examples that come with the motor shield library. They include the necessary wrapper code.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.