Controlling 2 stepper motors via BlueTooth (HC-05)

Hello all,

After messing around for days, looking up numerous internet pages and becoming frustrated, I decided it was high time to ask for help.

As stated in the subject title I’m attempting to control two stepper motors via a HC-05 module, alongside an Arduino Uno.

The problem I’m having is that when I upload a simple Linetracker program the motors work fine, they rotate and stop, depending on what two sensors read.

Using the same lines for activating and deactivating the motors as used in the linetracker program but now implementing them in my bluetooth program. The motors do not respond to any of commands. I tested the code first with two LED’s to simulate both motors, forward, left, right, backward, all those commands worked flawlessly with the LED’s.

The application used to send commands to the BT module sends a “WOn” command when the forward button is pressed and keeps sending it as long as the button is pressed. Upon release it sends a “WOff” command.
Same goes for left (“AOn” & “AOff”) , right (“DOn” & “DOff”), backwards (“SOn” & “SOff”).

This is the code I used for testing the LED’s and motors, in which the LED’s function properly but the motors don’t respond.
I am aware that the code can be shortened and cleaned up, which I will work on when it actually does function :slight_smile:
The automode integer is used because we need to be able to switch between two types of driving, BT controlled and an automatic mode (LineTracker).

/*

*/

int ledPinR = 10;
int ledPinG = 11;

String readString;

int step1 = 3;  //Stepper 1
int dir1 = 2;   //Direction stepper 1

int step2 = 5;  //Stepper 2
int dir2 = 4;   //Direction stepper 2

int speed = 300;

int automode = 0;

void setup() 
{
  Serial.begin(9600);
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
}


void loop() 
{
  while (Serial.available()) 
  {
    delay(3);
    char c = Serial.read();
    readString += c;
  }  
  
  if (readString == "AutomaticOn")
  {
    automode = 1;
  }
  if (readString == "AutomaticOff")
  { 
    automode = 0;
  }
  
  
  
  
  
  if (readString.length() >0)  
  {
  Serial.println(readString);
  
    if (automode == 0)
    {
  
      if (readString == "WOn")  //forward on
      {
      BothForward;
      }
      
      if (readString == "SOn") //backward on
      {
      BothBackward;
      }
      
      if (readString == "DOn") //right on
      {
      LeftForward;
      }
      
      if (readString == "AOn") //left on
      {
      RightForward;
      }
  
      if (readString == "WOff")
      {
      BothStop;
      }
      
      if (readString == "SOff")
      {
      BothStop;
      }
      
      if (readString == "DOff")
      {
     BothStop;
      }
      
      if (readString == "AOff")
      {
      BothStop;
      }
      
    }
    
    if (automode == 1)
    {
     Serial.println("Automode on"); 
     delay(100);
    }  
  readString="";  
  }
}

void BothStop ()
{
  digitalWrite(step1, LOW);
  digitalWrite(step2, LOW);
  
  digitalWrite(ledPinR, LOW);
  digitalWrite(ledPinG, LOW);
  
  delayMicroseconds(speed);
}

void BothForward ()
{
  digitalWrite(ledPinR, HIGH);
  digitalWrite(ledPinG, HIGH);
  
  digitalWrite(step1, HIGH);
  digitalWrite(step2, HIGH);
  
  digitalWrite(dir1, HIGH);
  digitalWrite(dir2, HIGH);
  
  delayMicroseconds(speed);
}

void BothBackward ()
{
  digitalWrite(ledPinR, HIGH);
  digitalWrite(ledPinG, HIGH);
  
  digitalWrite(step1, HIGH);
  digitalWrite(step2, HIGH);
  
  digitalWrite(dir1, LOW);
  digitalWrite(dir2, LOW);
  
  delayMicroseconds(speed);
}

void RightForward () 
{
  digitalWrite(ledPinR, HIGH);

	digitalWrite(dir1, HIGH);

	digitalWrite(step1, HIGH);
	delayMicroseconds(speed);
	digitalWrite(step1, LOW);
	delayMicroseconds(speed);
}

void LeftForward () 
{
  digitalWrite(ledPinG, HIGH);

	digitalWrite(dir2, HIGH);

	digitalWrite(step2, HIGH);
	delayMicroseconds(speed);
	digitalWrite(step2, LOW);
	delayMicroseconds(speed);
}

Following is the code I used for testing with a while loop for the forward mode, same result as the previous code.

String readString;

int step1 = 3;  //Stepper 1
int dir1 = 2;   //Direction stepper 1

int step2 = 5;  //Stepper 2
int dir2 = 4;   //Direction stepper 2

int speed = 500;  
int fwmode = 0;  

int ledPinR = 10;
int ledPinG = 11;

char c;

void setup() 
{  
  Serial.begin(9600);

  pinMode(step1, OUTPUT);
  pinMode(dir1, OUTPUT);

  pinMode(step2, OUTPUT);
  pinMode(dir2, OUTPUT);             

}

void loop() 
{
  

  while (Serial.available())
  {
    delay(3);
    char c = Serial.read();
    readString += c;
  }
  

  if (readString.length() >0 )
  {
    //Serial.println(readString);
    
    while (readString == "WOn" || fwmode == 1)
    {
      BothForward;
      fwmode = 1;
      
      Serial.println("Fw On");
    
      while (Serial.available())
      {
        delay(3);
        c = Serial.read();
        readString += c;
      }
      
      if (readString == "WOff")
      {
        fwmode = 0;
        BothStop;
        
        Serial.println("Fw Off");
      }
    
    readString = ""; 
    }
    
  }

 // readString = ""; 
  
  
}

void BothStop ()
{
  digitalWrite(step1, LOW);
  
  digitalWrite(step2, LOW);
  
  delayMicroseconds(speed);
}

void BothForward ()
{
  digitalWrite(ledPinR, HIGH);
  digitalWrite(ledPinG, HIGH);
  
  digitalWrite(step1, HIGH);
  digitalWrite(step2, HIGH);
  
  digitalWrite(dir1, HIGH);
  digitalWrite(dir2, HIGH);
  
  delayMicroseconds(speed);
}

void RightForward () 
{
  digitalWrite(ledPinR, HIGH);

	digitalWrite(dir1, HIGH);

	digitalWrite(step1, HIGH);
	delayMicroseconds(speed);
	digitalWrite(step1, LOW);
	delayMicroseconds(speed);
}

void LeftForward () 
{
  digitalWrite(ledPinG, HIGH);

	digitalWrite(dir2, HIGH);

	digitalWrite(step2, HIGH);
	delayMicroseconds(speed);
	digitalWrite(step2, LOW);
	delayMicroseconds(speed);
}

And this last code is the not-flawless LineTracker thusfar, it does however respond to the sensor input. It stops and restarts the motors depending on the sensors input. The same setup is used for all three programs, whereas only the LineTracker actually has rotating motors.
Also, this has a forward facting InfraRed, which works aswell.

/*
Name:		Stepper_ir_test.ino
Created:	1/15/2018 10:50:00 AM
Author:	yanni
*/

int step1 = 3;
int dir1 = 2;

int step2 = 5;
int dir2 = 4;

int ir = 12;

int IR1 = A0; //connected to analog 0
int IR2 = A1; //connected to analog 1

int stopValue = 100;
//int test = 8;




// the setup function runs once when you press reset or power the board
void setup()
{
	Serial.begin(9600);

	pinMode(step1, OUTPUT);
	pinMode(dir1, OUTPUT);

	pinMode(step2, OUTPUT);
	pinMode(dir2, OUTPUT);

	pinMode(ir, INPUT);

	pinMode(IR1, INPUT);
	pinMode(IR2, INPUT);


}

// the loop function runs over and over again until power down or reset
void loop()
{


	


	if (digitalRead(ir) == HIGH)
	{

			int IR1_Value = analogRead(IR1);
			int IR2_Value = analogRead(IR2);
			
			Serial.println(IR1_Value);
			Serial.println(IR2_Value);
			

			if (IR1_Value >= stopValue && IR2_Value >= stopValue)
			{
				digitalWrite(step1, HIGH);
				digitalWrite(step2, HIGH);
				delayMicroseconds(300);
				digitalWrite(step1, LOW);
				digitalWrite(step2, LOW);
				delayMicroseconds(300);

			}

			else if (IR1_Value > stopValue && IR2_Value <= stopValue)
			{
				digitalWrite(step1, HIGH);
				delayMicroseconds(300);
				digitalWrite(step1, LOW);
				delayMicroseconds(300);
			}

			else if (IR1_Value <= stopValue && IR2_Value > stopValue)
			{
				digitalWrite(step2, HIGH);
				delayMicroseconds(300);
				digitalWrite(step2, LOW);
				delayMicroseconds(300);
			}

	}
}

Any help or critique is welcome, sorry for the long post.

Thanks in advance,
Paul

I presume this line

     BothForward;

is intended to call the function. If so it is not written correctly. It should be

     BothForward();

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

When using Cstrings you must use strcmp() to compare values rather than ==

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

If you can change the program that sends the commands you could make the Arduino code very much simpler if you send single character commands. Between the upper and lower case characters there are 52 options.

...R