Loading [MathJax]/extensions/TeX/AMSsymbols.js

May 26, 2012

Android + Bluetooth + Arduino. Proyecto

Bueno gente aquí les presento mi proyecto final, consiste en manejar mediante un dispositvo móvil con S.O Android un carrito mediante Bluetooth.

Para realizar la conexión entre el carrito y el dispositivo móvil utilicé un módulo bluetooth (el Rn-42) que se encuentra conectado a un Arduino UNO. El dispositivo móvil (en mi caso una tablet iconia A200) mandaba señales al arduino el cual movía el motor del carrito.

Los movimientos que realiza el carrito son hacia adelante y hacia atrás mediante el uso de botones, en este caso una interfaz gráfica que tiene un volante y el pedal de acelerador y freno. Mi idea era manejar el carrito mediante acelerómetro pero por cuestiones de tiempo no alcancé a implentarlo.

Bueno aquí les dejo una pequeña presentación y el codigo que utilicé.


Bueno aquí les dejo el sketch de arduino que permitirá la conexión bluetooth entre el móvil y el carrito


boolean state;
//char comparador
char mov;
void setup(){
Serial.begin(115200);
state = false;
//pines de salida
pinMode(13, OUTPUT);//apagado
digitalWrite(13, LOW);
pinMode(12, OUTPUT);//adelante
digitalWrite(12, LOW);
pinMode(11, OUTPUT);//atras
digitalWrite(11, LOW);
}
///////////////////////////////////////////////////////
void loop(){
if(Serial.available() > 0){
mov = Serial.read();
Serial.println(mov);
//Adelante
if(mov == 'a'){
//digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
//digitalWrite(11, LOW);
}
//Atras
if(mov == 'b'){
//digitalWrite(13, HIGH);
//digitalWrite(12, LOW);
digitalWrite(11, HIGH);
}
if(mov == 'd'){
delay(1000);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
}//fin if
}//fin loop
view raw bluetooth.ino hosted with ❤ by GitHub
Y aquí el código de android para la interfaz y la conexión bluetooth con el arduino. Cabe aclarar que este código lo realicé en processing.
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.lang.reflect.Method;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
////VARIABLES////////
private static final int REQUEST_ENABLE_BT = 3;
int state;
PFont font1;
PFont font2;
String error;
ArrayList Devices;
InputStream ins;
OutputStream ons;
BluetoothAdapter Adapter;
BluetoothDevice Device;
BluetoothSocket socket;
byte value;
//Images
PImage Accel;
PImage Brake;
PImage Wheel;
PImage Bg;
PImage Back;
///////////////////////////////////////////////////////////////////
BroadcastReceiver receptor = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
println("on Receive");
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice Device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
println(Device.getName() + " " + Device.getAddress());
Devices.add(Device);
}
else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
state = 1;
println("Search begins");
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
state = 2;
println("Search ends");
}
}
};
///////////////////////////////////////////////////////////////////
void setup(){
frameRate(25);
font1 = createFont("Liberation Sans", 28, true);
font2 = createFont("Liberation Sans", 34, true);
stroke(255);
}
///////////////////////////////////////////////////////////////////
void draw(){
switch(state){
case 0:
menu("Elija una opción", color(0,0,0));
break;
case 1:
deviceList("Buscando Dispositivos", color(255,255,255));
break;
case 2:
deviceList("Elija un dispositivo", color(255,255,255));
break;
case 3:
connectedDevice();
break;
case 4:
Gui();
break;
case 5:
showError();
break;
}
}
///////////////////////////////////////////////////////////////////
void menu(String title, color c){
background(255);
textFont(font2);
fill(c);
text(title,width/2 - 90, 50);
stroke(0, 0, 0);
fill(200, 190, 192);
rect(width - 230, height - 550, 210, 70);
fill(0, 0, 0);
text("Start", width - 160, height - 500);
stroke(0, 0, 0);
fill(200, 190, 192);
rect(width - 230, height - 470, 210, 70);
fill(0, 0, 0);
text("Connection", width - 200, height - 420);
stroke(0, 0, 0);
fill(200, 190, 192);
rect(width - 230, height - 390, 210, 70);
fill(0, 0, 0);
text("Minigame", width - 190, height - 340);
Bg = loadImage("fondo.png");
image(Bg, width/2 - Bg.width/2, height/3);
if(mousePressed){
if(mouseX > width - 230 && mouseX < width - 20 && mouseY >height - 550 && mouseY < height - 480){
try{
Gui();
//println("funciona");
}catch(Exception ex){
state = 5;
error = ex.toString();
println(error);
}
}
if(mouseX > width - 230 && mouseX < width - 20 && mouseY > height - 470 && mouseY < height - 400){
try{
state = 2;
}catch(Exception ex){
state = 5;
error = ex.toString();
println(error);
}
}
if(mouseX > width - 230 && mouseX < width - 20 && mouseY > height - 340 && mouseY < height - 270){
try{
println("funciona");
}catch(Exception ex){
state = 5;
error = ex.toString();
println(error);
}
}
}
}
///////////////////////////////////////////////////////////////////
void deviceList(String text, color c){
background(0);
textFont(font1);
fill(c);
text(text,0,20);
Devices = new ArrayList();
for (BluetoothDevice Device: Adapter.getBondedDevices()){
Devices.add(Device);
}
if(Devices != null){
for(int index = 0; index < Devices.size(); index++){
BluetoothDevice Device = (BluetoothDevice) Devices.get(index);
fill(255,255,0);
int Position = 50 + (index * 55);
if(Device.getName() != null){
text(Device.getName(), 0 ,Position);
}
fill(180,180,255);
text(Device.getAddress(), 0, Position + 20);
fill(255);
line(0, Position + 30, 319, Position + 30);
}
}
}
///////////////////////////////////////////////////////////////////
void connectedDevice(){
try{
socket = Device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
ins = socket.getInputStream();
ons = socket.getOutputStream();
state = 4;
}
catch(Exception ex) {
state = 5;
error = ex.toString();
println(error);
}
}
///////////////////////////////////////////////////////////////////
void onStart(){
super.onStart();
println("starting...");
Adapter = BluetoothAdapter.getDefaultAdapter();
if (Adapter != null){
if (!Adapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}else{
begin();
}
}
}
////////////////////////////////////////////////////////////////
void onActivityResult (int requestCode, int resultCode, Intent data){
println("In: onActivityResult");
if(resultCode == RESULT_OK){
println("Result OK");
begin();
}else{
println("Result Canceled");
state = 5;
error = "Bluetooth isn't enabled";
}
}
/////////////////////////////////////////////////////////////////
void onStop(){
println("stopping...");
if(socket != null){
try{
socket.close();
}catch(IOException ex){
println(ex);
}
}
super.onStop();
}
//////////////////////////////////////////////////////////////////
void begin(){
state = 0;
}
/////////////////////////////////////////////////////////////////
void mouseReleased(){
switch(state){
case 2:
checkElection();
break;
case 4:
checkButton();
/*try{
ons.write('d');
}catch(Exception ex){
state = 5;
error = ex.toString();
println(error);
}*/
break;
}
}
///////////////////////////////////////////////////////////////////
void buttonPressed(){
}
/////////////////////////////////////////////////////////////////////////////////////
void checkElection(){
int Choose = (mouseY - 50)/55;
if(Choose < Devices.size()){
Device = (BluetoothDevice) Devices.get(Choose);
println(Device.getName());
state = 3;
}
}
///////////////////////////////////////////////////////////////////
void checkButton(){
try{
ons.write('d');
}catch(Exception ex){
error = ex.toString();
println(error);
}
}
/////////////////////////////////////////////////////////////////////////////////////
void Gui(){
try{
while(ins.available() > 0){
value = (byte)ins.read();
}
}catch(Exception ex){
state = 4;
error = ex.toString();
println(error);
}
background(255);//fondo blanco
//carga las imagenes y se les asignan coordenadas
Accel = loadImage("acelerador.png");
image(Accel, width/2 + 2*Accel.width, height/2 - Accel.height/2);
Brake = loadImage("freno.png");
image(Brake, width/2 + Brake.width, height/2 - Brake.height/2);
Wheel = loadImage("volante.png");
image(Wheel, width/2 - Wheel.width, height/2 - Wheel.height/2);
Back = loadImage("atras.png");
image(Back, width/9, height - 80);
//Si la imagen es presionada...
//Acelerador
if(mousePressed){
if(mouseX > width/2 + 2 * Accel.width && mouseX < width/2 + 3*Accel.width && mouseY > height/2 - Accel.height/2 && mouseY < height/2 + Accel.height/2){
try{
ons.write('a');//manda al arduino un char 'a' para comparar
//println("adelante");
}catch(Exception ex){
state = 5;
error = ex.toString();
println(error);
}
}
//reversa
if(mouseX > width/2 + Brake.width && mouseX < width/2 + 2*Brake.width && mouseY > height/2 - Brake.height/2 && mouseY < height/2 + Brake.height/2){
try{
ons.write('b');//manda al arduino un char 'b' para comparar
//println("reversa");
}catch(Exception ex){
state = 5;
error = ex.toString();
println(error);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////
void showError(){
background(0);
fill(255, 255, 0);
textFont(font1);
textAlign(CENTER);
translate(width / 2, height / 2);
rotate(3 * PI / 2);
text(error, 210, 248);
}
/////////////////////////////////////////////////////////////////////////////////////
view raw carrito.pde hosted with ❤ by GitHub

3 comments:

  1. http://webdelcire.com/wordpress/archives/1045 aqui esta mejor explicado

    ReplyDelete
  2. que programa usaste para llegar a ello??
    y como lo usaste?

    ReplyDelete