Bluetooth control of stepper motor car

Hello. I am trying to control a 2 wheel drive car that has stepper motors using a bluetooth app. I used the same app for a DC motor and it worked fine. I modified the code for stepper motors and there seems to be an issue. I think the issue is with the step function of the stepper library. It seems to be slowing down the rate of communication between the smartphone and the arduino. I am printing the character sent by the app to the serial monitor and it is taking too long to print or to reflect any changes in directions. Any help in resolving this would be very much appreciated. Thanks in advance.

This is the app that I am using along with an Arduino Uno and a HC05 bluetooth module-

#include <Stepper.h>

char a;
const int stepsPerRevolution = 200;

Stepper left(stepsPerRevolution, 4, 5, 6, 7);
Stepper right(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
  Serial.begin(9600);
  left.setSpeed(60);
  right.setSpeed(60);
}

void loop()
{

  if (Serial.available())
  {
    a = Serial.read();
    Serial.println(a);
  }

  if (a == 'F') //Forward
  {
    left.step(-stepsPerRevolution); //anti cw
    right.step(stepsPerRevolution); //cw
  }

  else if (a == 'B')  //Reverse
  {
    left.step(stepsPerRevolution);  //cw
    right.step(-stepsPerRevolution);  //anti cw
  }

  else if (a == 'L')  //Left
  {
    left.step(stepsPerRevolution);  //cw
    right.step(stepsPerRevolution); //cw
  }

  else if (a == 'R')  //Right
  {
    left.step(-stepsPerRevolution); //anti cw
    right.step(-stepsPerRevolution);  //cw
  }

  else if (a == 'G')  //Left forward
  {
    right.step(stepsPerRevolution); //cw
    //left motor is at a stop
    left.setSpeed(0);
    left.step(stepsPerRevolution);
  }

  else if (a == 'H')  //Left reverse
  {
    right.step(-stepsPerRevolution); //anti cw
    //left motor is at a stop
    left.setSpeed(0);
    left.step(stepsPerRevolution);
  }

  else if (a == 'I')  //Right forward
  {
    left.step(-stepsPerRevolution); //anti cw
    //right motor is at a stop
    right.setSpeed(0);
    right.step(stepsPerRevolution);
  }

  else if (a == 'J')  //Right reverse
  {
    left.step(stepsPerRevolution);  //cw
    //right motor is at a stop
    right.setSpeed(0);
    right.step(stepsPerRevolution);
  }

  else if (a == 'S')  //Stop
  {
    left.setSpeed(0);
    right.setSpeed(0);
    left.step(stepsPerRevolution);
    right.step(stepsPerRevolution);
  }
  delay(5);
}

The stepper library functions block. Nothing else can happen until the step function completes. The AccelStepper library has non-blocking functions.

Alternatively you could use the MoToStepper class of my MobaTools library. It may be a bit easier to handle than AccelStepper - especially with 2 steppers - because you don't have to bother with step generating calls in your sketch. You only tell the lib what your motors should do (all calls are non blocking). The corresponding step generation is done by interrupts in the background.

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