Código de exemplo para o Joystick Bluetooth 2.1
Autor: Marcelo Borges dos Santos | Publicado: 2015-05-12T15:26:00-03:00


&nbsp; &nbsp;Código fonte de exemplo para utilizar o Joystick Bluetooth com o módulo bluetooth HC-05 e com o Arduino 











/*&nbsp;***************************************************************************
Joystick&nbsp;Bluetooth&nbsp;-&nbsp;Código&nbsp;de&nbsp;exemplo&nbsp;para&nbsp;arduino
Autor:&nbsp;Marcelo&nbsp;Borges&nbsp;dos&nbsp;Santos
e-mail:&nbsp;mbsborges@gmail.com
blog:&nbsp;http://mbsborges.blogspot.com.br

Ultima&nbsp;alteração:&nbsp;12&nbsp;de&nbsp;maio&nbsp;de&nbsp;2015&nbsp;às&nbsp;15:08
******************************************************************************/
//Biblioteca
#include&nbsp;&lt;SoftwareSerial.h&gt;
//Pinos&nbsp;para&nbsp;a&nbsp;virtual&nbsp;serial&nbsp;(RX,&nbsp;TX)
SoftwareSerial Bluetooth(2, 3);
//Saída&nbsp;PWM
int dimmer = 5;
//Pino&nbsp;para&nbsp;leitura&nbsp;da&nbsp;tensão&nbsp;da&nbsp;bateria
int bateria = A0;
//String&nbsp;para&nbsp;armazenar&nbsp;a&nbsp;leitura&nbsp;dos&nbsp;dados
String readString = "";
//variável&nbsp;para&nbsp;receber&nbsp;os&nbsp;caracteres&nbsp;vindos&nbsp;da&nbsp;serial
byte byteRead;

void setup() {
&nbsp;&nbsp;Bluetooth.begin(9600);
&nbsp;&nbsp;Serial.begin(9600);
&nbsp;&nbsp;//Pino de saída
&nbsp;&nbsp;pinMode(dimmer, OUTPUT);
&nbsp;&nbsp;//Mensagem de inicialização
&nbsp;&nbsp;Serial.println("Mbsborges - Joystick Bluetooth");
&nbsp;&nbsp;Serial.println("http://mbsborges.blogspot.com.br/");
}

void loop() {
&nbsp;&nbsp;//Verificando se tem algum byte disponível na serial
&nbsp;&nbsp;while (Bluetooth.available() &gt; 0) {
&nbsp;&nbsp;&nbsp;&nbsp;//lendo byte disponível
&nbsp;&nbsp;&nbsp;&nbsp;byteRead&nbsp;=&nbsp;Bluetooth.read();
&nbsp;&nbsp;&nbsp;&nbsp;//Verificando se é fim de linha
&nbsp;&nbsp;&nbsp;&nbsp;if (byteRead == '\n') {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("\nOk!");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Imprimindo a mensagem completa
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(readString);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Tratamento dos dados para a execução dos comandos recebidos
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tratamentoDados();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Limpando String de leitura
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readString = "";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Covertendo os bytes recebidos em caracteres e os concatenando
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readString += char(byteRead);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Imprimindo caractere recebido
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Serial.print (char(byteRead));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}

void tratamentoDados() {
&nbsp;&nbsp;if (readString.indexOf("level") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;//teste com numeros randômicos
&nbsp;&nbsp;&nbsp;&nbsp;//int randNumber = random(0, 101);
&nbsp;&nbsp;&nbsp;&nbsp;//Coneversão da leitura da porta analógica A0 para valores dentro da escala de 0-100
&nbsp;&nbsp;&nbsp;&nbsp;int randNumber = map(analogRead(bateria), 0, 1023, 0, 100);
&nbsp;&nbsp;&nbsp;&nbsp;//exibição do valor a ser enviado para o aplicativo
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(randNumber);
&nbsp;&nbsp;&nbsp;&nbsp;//Enviando o valor para o aplicativo
&nbsp;&nbsp;&nbsp;&nbsp;//Formato da mensagem: _bat=number=bat    
&nbsp;&nbsp;&nbsp;&nbsp;//number é um numero inteiro e varia de 0-100
&nbsp;&nbsp;&nbsp;&nbsp;Bluetooth.print("_bat=" + String(randNumber) + "=bat"); 
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;if (readString.indexOf("slider=") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;//Obter valor do slider
&nbsp;&nbsp;&nbsp;&nbsp;readString = readString.substring(readString.indexOf("slider=") + 7, readString.indexOf("=slider"));
&nbsp;&nbsp;&nbsp;&nbsp;//Imprimindo valor do slider
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(readString);
&nbsp;&nbsp;&nbsp;&nbsp;//Ajustando a intensidade do led de acordo com o valor lido do slider
&nbsp;&nbsp;&nbsp;&nbsp;analogWrite(dimmer, map(readString.toInt(), 0, 100, 0, 255));
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("up") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("UP");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("down") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("DOWN");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("left") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("LEFT");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("right") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("RIGHT");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("stop") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("STOP");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("cmd1") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("CMD1");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("cmd2") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("CMD2");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("cmd3") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("CMD3");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("cmd4") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("CMD4");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("cmd5") == 0){
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("CMD5");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;else if (readString.indexOf("cmd6") == 0){ 
&nbsp;&nbsp;&nbsp;&nbsp;Serial.println("CMD6");
&nbsp;&nbsp;&nbsp;&nbsp;}
}