Problem controlling via bluetooth 3 stepper motors

Hello everyone,

I hope that someone can help me to figure out what is wrong with my little stepper motors project.
I would like to control via bluetooth 3 steppers BYJ48 (ULN2003 driver).
What I would like them to do is:

  • first motor to rotate clockwise when data '1' is received and counterclockwise when data '2' is received;
  • second motor to rotate clockwise when data '3' is received and counterclockwise when data '4' is received;
  • third motor to rotate clockwise when data '5' is received and counterclockwise when data '6' is received;

I am using the #include <Stepper.h> library

I am testing with the app ArduDroid to send the bluetooth data.

I have two problems:

  1. when I send any data the first motor myStepper1 always rotates forward, but I would like it to rotate only if I send the data '1' (for forward rotation) and '2' for backward rotation. In addition when I want to rotate another motor this myStepper1 motor always rotate first. For example if I send the data '3' the myStepper1 rotates first and then also myStepper2 rotates;
  2. all the motors just rotate clockwise even if I send the data to rotate them counterclockwise

I think there is a problem with the code.
Here it is:

#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper1(stepsPerRevolution, 5, 4, 3, 2);     //definisce i pin per i motori
Stepper myStepper2(stepsPerRevolution, 9, 8, 7, 6);
Stepper myStepper3(stepsPerRevolution, 13, 12, 11, 10);
float delay_time;
char state =0;


void setup()
{
Serial.begin(9600);    //bus rate 
myStepper1.setSpeed(60);  //velocità motori
myStepper2.setSpeed(60);
myStepper3.setSpeed(60);
}

void loop()
{
  if(Serial.available()>0)  // controlla se c'è dato bluetooth
    {
    state=Serial.read();
    }

   if(state== '1')
      {         
      myStepper1.step(stepsPerRevolution);
      delay(20);
      }
   if(state == '2')
      {  
      myStepper1.step(-stepsPerRevolution); 
      delay(20);
      }
    if(state == '3')
      {         
      myStepper2.step(stepsPerRevolution); 
      delay(20);
      }
    if(state == '4')
      {  
      myStepper2.step(-stepsPerRevolution);
      delay(20);
      }
    if(state == '5')
      {         
      myStepper3.step(stepsPerRevolution);
      delay(20);
      }
    if(state == '6')
      {  
      myStepper3.step(-stepsPerRevolution);
      delay(20); 
      }
}

Thank you in advance.

Bye

Pietro

I suspect that you would be better using the AccelStepper library as the standard Stepper library blocks the Arduino while a movement takes place so that it won't pick up the next character and then several characters may stack-up to be dealt with.

And your code for receiving data is not robust. It can't ignore unwanted characters or tell when a valid message has arrived.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

If you send a '1', the value is stored in state (which is a stupid name, as it doesn't hold the state of anything). Then, the stepper is moved. Then, loop() ends and starts again. There is no serial data to read, but state still contains '1', so the stepper moves again. Then, loop() repeats, and the stepper moves again.

Is that what you really want?

You REALLY should get your bluetooth device off of the hardware serial pins, so you can use them to debug your program. Debugging by guessing is most unsatisfactory.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Have you written your code in stages?
Did you get you code to work without the bluetooth?

Tom... :slight_smile:

Hi,
Thank you everyone for your support,
Now everything is working as it should.

The second problem is the first that I've sorted out: I've just connected the wires to the ULN2003 driver board in a different order. Now Arduino pins 2,3,4,5 are connected respectively to IN2, IN4, IN3, IN1. And all the motors rotate back and forth. I tried to cut the bluetooth section as TomGeorge suggested and everything was working fine.

#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper1(stepsPerRevolution, 5, 4, 3, 2);     //definisce i pin per i motori
Stepper myStepper2(stepsPerRevolution, 9, 8, 7, 6);
Stepper myStepper3(stepsPerRevolution, 13, 12, 11, 10);
float delay_time;



void setup()
{
Serial.begin(9600);    //bus rate 
myStepper1.setSpeed(60);  //velocità motori
myStepper2.setSpeed(60);
myStepper3.setSpeed(60);
}

void loop()
{

      
      myStepper1.step(stepsPerRevolution);
      delay(20);
      
  
      myStepper1.step(-stepsPerRevolution); 
      delay(20);
      
            
      myStepper2.step(stepsPerRevolution); 
      delay(20);
    
      myStepper2.step(-stepsPerRevolution);
      delay(20);
           
      myStepper3.step(stepsPerRevolution);
      delay(20);
     
      myStepper3.step(-stepsPerRevolution);
      delay(20); 
     
}

The second problem is the weird one. Once I corrected the wires when I used to send 3 or 4 or 5 or a random number as input the first motor used to rotate forward and than backward before. Also noticed this in the debugger, as though 1 and 2 were also sent before 3 or whatever number.

I sorted this pretty by chance substituting the numbers '1' '2' '3' '4' '5' '6' with letter A B C D E F and it works just fine.
I really don't know what was causing that. The thing is that now is working exactly as I wanted also if I plug random letters or numbers as input is doing nothing.

For the future I will probably move to AccelStepper library.

Here is the final code.

#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper1(stepsPerRevolution, 5, 4, 3, 2);     //definisce i pin per i motori
Stepper myStepper2(stepsPerRevolution, 9, 8, 7, 6);
Stepper myStepper3(stepsPerRevolution, 13, 12, 11, 10);
float delay_time;
int state =0;
int flag = 0;


void setup()
{
Serial.begin(9600);    //bus rate 
myStepper1.setSpeed(60);  //velocità motori
myStepper2.setSpeed(60);
myStepper3.setSpeed(60);
}

void loop()
{
  if(Serial.available()>0)  // controlla se c'è dato bluetooth
    {
    state=Serial.read();
    flag=0;
    }

   if(state== 'A')
      {         
      myStepper1.step(stepsPerRevolution);
      delay(20);
      if(flag == 0)
        {
        Serial.println("First motor forward");
        flag=1;  
        }
      }
   if(state == 'B')
      {  
      myStepper1.step(-stepsPerRevolution); 
      delay(20);
      if(flag == 0)
        {
        Serial.println("First motor backward");
        flag=1;  
        }
      }
    if(state == 'C')
      {         
      myStepper2.step(stepsPerRevolution); 
      delay(20);
      if(flag == 0)
        {
        Serial.println("Second motor forward");
        flag=1;  
        }
      }
    if(state == 'D')
      {  
      myStepper2.step(-stepsPerRevolution);
      delay(20);
      if(flag == 0)
        {
        Serial.println("Second motor backward");
        flag=1;  
        }
      }
    if(state == 'E')
      {         
      myStepper3.step(stepsPerRevolution);
      delay(20);
      if(flag == 0)
        {
        Serial.println("Third motor forward");
        flag=1;  
        }
      }
    if(state == 'F')
      {  
      myStepper3.step(-stepsPerRevolution);
      delay(20); 
       if(flag == 0)
        {
        Serial.println("Third motor backward");
        flag=1;  
        }
      }
}

Still thank you

Bye

Pietro