No, this method should be fine. Are you having issues with the Android side or the Arduino side?
Added:
I do see that your using Strings and upon testing your code, I noticed your not clearing "received" once it has been used. By you not clearing the String, it continues to concatenate the incoming chars to the String, and this could pose serious memory issues.
I suggest you use C strings (char Array) and the strtok() function.
More:
Here is my test results:
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(7, 8); // RX, TX
int direcao[] = {
9, 10};
int motor[] = {
5, 6};
int ledPin = 13;
int veloc = 0;
String received = "";
char chrreceived;
//Funcao que faz parar
void para(){
digitalWrite(ledPin, LOW);
digitalWrite(motor[0], LOW);
digitalWrite(motor[1], LOW);
digitalWrite(direcao[0], LOW);
digitalWrite(direcao[1], LOW);
delay(25);
}
//Funcao que faz andar para frente
void marchafrente(int velocidade = 255){
analogWrite(motor[0], velocidade);
digitalWrite(motor[1], LOW);
}
//Funcao que faz andar para tras
void marchare(int velocidade = 255){
digitalWrite(motor[0], LOW);
analogWrite(motor[0], velocidade);
}
//Funcao que faz mover para a esquerda
void moveesquerda(int velocidade = 255){
digitalWrite(direcao[0], LOW);
analogWrite(direcao[1], velocidade);
delay(100);
digitalWrite(direcao[0], LOW);
digitalWrite(direcao[1], LOW);
}
//Funcao que faz mover para a direita
void movedireita(int velocidade = 255){
analogWrite(direcao[1], velocidade);
digitalWrite(direcao[1], LOW);
delay(100);
digitalWrite(direcao[0], LOW);
digitalWrite(direcao[1], LOW);
}
void setup() {
//Configurando dados seriais
Serial.begin(9600);
bluetooth.begin(9600);
//Configurando pino do led
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { //bluetooth
while(Serial.available() > 0) {//bluetooth
chrreceived = Serial.read(); //bluetooth
received.concat(chrreceived);
//Serial.println(received);
}
if (received.substring(0,2) == "VE") {
veloc = received.substring(2).toInt();
Serial.print("Left: ");
Serial.println(veloc);
moveesquerda(veloc);
}
else if (received.substring(0,2) == "VD")
{
Serial.println();
veloc = received.substring(2).toInt();
Serial.print("Right: ");
Serial.println(veloc);
movedireita(veloc);
}
else {
Serial.print("Key: ");
Serial.println(chrreceived);
switch(chrreceived){
//FRENTE
case '8':
case 'w':
case 'W':;
marchafrente();
break;
//TRÁS
case '2':
case 's':
case 'S':
marchare();
break;
//ESQUERDA
case '4':
case 'a':
case 'A':
moveesquerda();
break;
//DIREITA
case '6':
case 'd':
case 'D':
movedireita();
break;
//PARADO
case '5':
case 'p':
case 'P':
para();
break;
//LIGA LED
case 'L':
case 'l':
case '7':
digitalWrite(ledPin, HIGH);
break;
//DESLIGA LED
case 'K':
case 'k':
case '9':
digitalWrite(ledPin, LOW);
break;
}
}
}
received = "";
delay(100);
}
Output:
Key: 8
Right: 111
Left: 444
Key: w
Key: W
Key: L
Key: 1
Right: 12
Left: 153