HC-05 Bluetooth multi servo control app problem

Hi, I'm trying to build an app in MITAppInventor to control 6DOF Robotic Arm based on 4WD Car Platform with L298N motor drive controled by HC-05 module with Arduino Mega 2560 R3

I have successfully build app to control the 4WD car platform and second separate app to control one servo. Platform is working same like the app with one servo but I have 6 servos to control.

I read about SoftwareSerial and Servo conflict.
Do I need separate servo control board for this project? Is it possible to use only Arduino Mega 2560 R3 to control 6 servos and L298N via HC-05 and Android app?
I don't know the structure of Arduino sketch code for this project and how to program it. I've done app in MIT for this project attached below.
Please see and let me know.


Why use software serial on a Mega?
What are you using all the hardware serial ports for?

The simplest code that I manage to work and build app for it was like below for car platform L298N control:

//no speed control


char t;
 
void setup() {
pinMode(12,OUTPUT);   //left motors forward
pinMode(11,OUTPUT);   //left motors reverse
pinMode(10,OUTPUT);   //right motors forward
pinMode(9,OUTPUT);   //right motors reverse

Serial.begin(38400);
 
}
 
void loop() {
if(Serial.available()){
  t = Serial.read();
  Serial.println(t);
}
 
if(t == 'F'){            //move forward all motors to rotate in forward direction
  digitalWrite(12,HIGH);
  digitalWrite(10,HIGH);
}
 
else if(t == 'B'){      //move reverse all motors rotate in reverse direction
  digitalWrite(11,HIGH);
  digitalWrite(9,HIGH);
}
 
else if(t == 'L'){      //turn right left side motors rotate in forward direction, right side motors doesn't rotate
  digitalWrite(12,HIGH);
}
 
else if(t == 'R'){      //turn left right side motors rotate in forward direction, left side motors doesn't rotate
  digitalWrite(10,HIGH);
}

 
else if(t == 'S'){      //stop all motors deactivated
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
  digitalWrite(9,LOW);
}
delay(100);
}

and for only one servo control with slide bar that I tested and build app for it was:

// servo bluetooth controled slider control 

#include <Servo.h>

Servo myservo;


const int Pin = 2; // 
char Text;

String Spilt;

String angle;

int pos = 0;    // variable to store the servo position

int k1;


void setup() {

Serial.begin(38400);

pinMode (Pin, OUTPUT); 


myservo.attach(Pin);


}


void loop() {


  if(Serial.available())

  {

    Text = Serial.read();  

    Spilt = Spilt + Text; 


if (Text == '*') {

      Serial.println(Spilt);

      Spilt = Spilt.substring(0, Spilt.length() - 1); // Delete last char *

      k1 = Spilt.indexOf('*');

      angle = Spilt.substring(0, k1);

      myservo.write(angle.toInt());

      delay(15);  

  

      Spilt = "";   

  }

      }

 

} 

Everything was working and the apps I build was good too but the project is for 6 servos and one app controlling servos and car platform on one screen. So far I have two separate apps controlling one servo and the other to control car platform.

Hardware serial ports I use like below:
8-13 to connect and control L298N motor drive
7-2 to connect 6 servo signal pins
1-0 is for TX/RX for HC-05
That's all. I still have ''Digital Pins'' left but I'm not going to use them because its only 6 servo project.

I'm not familiar with servos. Ingeneral you should be creating an array of 6 servo objects.

For the servo, it looks like you're receiving an angle followed by a '*'. To control individual servos, you can send something like

num,angle*

where num is the number of the servo to control, angle is the required angle and '*' is the end of the command. The comma between num and angle separates the two numbers which makes it easier to parse.

Life is easy ( :wink: ) if you use 0..5 for num so it can act as the index into the array of servos.

I just found some app on GooglePlay thanks to developer Iones Costa
Link to the app in GooglePlay

The code is working too. I can control 6 servo in the same time. Everyone can download the code from the app.

#include <Servo.h>
#define M1 2
#define M2 3
#define M3 4
#define M4 5
#define M5 6
#define M6 7
int v1;
int v2;
int v3;
int v4;
int v5;
int v6;
void hhhhhh(char* Comando);
void iiiiiiii(char* Comando);
Servo servo_M6, servo_M5, servo_M4, servo_M3, servo_M2,servo_M1,servo_M12;
char buffer[18];
void setup()
{
Serial.begin(38400);
Serial.flush();
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(M4, OUTPUT);
pinMode(M5, OUTPUT);
pinMode(M6, OUTPUT);
servo_M1.attach(2);
servo_M2.attach(3);
servo_M3.attach(4);
servo_M4.attach(5);
servo_M5.attach(6);
servo_M6.attach(7);
}
void loop()
{
if(Serial.available() > 0)
{
int C = 0;
delay(100);
int Digitados = Serial.available();
if(Digitados > 15) Digitados = 15;
while(Digitados--)
{
buffer[C++] = Serial.read();
}
hhhhhh(buffer);
}
}void hhhhhh(char* Comando)
{
char* Executa;
Executa = strtok (Comando, " ,");
while(Executa != NULL)
{
iiiiiiii(Executa);
Executa = strtok (NULL, " ,");
}
for(int x=0; x<16; x++)
{
buffer[x]='\0';
}
Serial.flush();
}
void iiiiiiii(char* Comando)
{
if(Comando[0] == 'A')
{
v1 = strtol(Comando+1, NULL, 10);
v1 = constrain(v1,0,180);
servo_M1.write(v1);
}
if(Comando[0] == 'B')
{v2 = strtol(Comando+1, NULL, 10);
v2 = constrain(v2,0,180);
servo_M2.write(v2);
}
if(Comando[0] == 'C')
{
v3 = strtol(Comando+1, NULL, 10);
v3 = constrain(v3,0,180);
servo_M3.write(v3);
}
if(Comando[0] == 'D')
{
v4 = strtol(Comando+1, NULL, 10);
v4 = constrain(v4,0,180);
servo_M4.write(v4);
}
if(Comando[0] == 'E')
{
v5 = strtol(Comando+1, NULL, 10);
v5 = constrain(v5,0,180);
servo_M5.write(v5);
}
if(Comando[0] == 'F')
{
v6 = strtol(Comando+1, NULL, 10);
v6 = constrain(v6,0,180);
servo_M6.write(v6);}
}

I'll have to marge car platform code and servo code and build app in MIT to control both of them in the same time.

Too much code!

Please remember to use code tags when posting code, and try to use the IDE's auto format tool on the code, before posting

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