Arduino + Bluetooth + Android + Motors

Hello Guys,
I would like to aid you, I'm on a project in a controlled car for Android. For this I am using an Arduino Uno board, with two 12V DC motors, one for traction and one for the wheels as well as an HC-05 bluetooth module.
I created an application in App Inventor at MIT, where I am using the orientation sensor to mess the front wheels, but I'm unsure of how to make software for it using the arduino and android with the orientation sensor ...

Thank's!

How are you going to aid us? Looks like we need to aid you. Do you have any code you can show us?

Hello HazardsMind,

I have the following code in arduino ....

#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 (bluetooth.available() >  0) { 
    while(bluetooth.available()) {
      chrreceived = bluetooth.read(); 
      received.concat(chrreceived);
    }

    Serial.println(received);    
    Serial.println();

    if (received.substring(0,2) == "VE") {
      veloc = received.substring(2).toInt();
      moveesquerda(veloc);
    } 
    else
      if (received.substring(0,2) == "VD") {
        Serial.println();
        veloc = received.substring(2).toInt();

        movedireita(veloc);
      }
      else {

        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;
        } 
      } 
  }
  Serial.println();  
  delay(100);  
}

Hi, could you give me an example string you will be sending? Also a minor correction, and you should take out the delays.

void movedireita(int velocidade = 255){
analogWrite(direcao[1], velocidade);
digitalWrite(direcao[1], LOW);

Hi,
I am sending the following string: "VE134" or "VE111" or "VD100" ....
Basically sending VE when I move left and VD when I move to the right, the number that follows is how many degrees the android device turned, if I'm not mistaken the "roll" attribute of the orientation sensor.

This was a way that I thought, but if you have another idea, with very different code, please I'm open to suggestions ....

Thanks!!!

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