lunes, 11 de enero de 2016

Bluetooth con Arduino y Processing

En estre proyecto estaremos utilizando :


1- ARDUINO UNO
2- BLUETOOTH
3- PROCESSING (Software)  https://processing.org

Circuito de coneccion electrica:


Arduino Sketsh:

_______________________________________________________________

char val; // variable to receive data from the serial port

int ledpin = 13; // LED connected to pin 13 (on-board LED)

void setup() {
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  Serial.begin(9600);       // start serial communication at 9600bps
}

void loop() {
  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }
  if( val == 'H' )               // if 'H' was received
  {
    digitalWrite(ledpin, HIGH);  // turn ON the LED
  } else { 
    digitalWrite(ledpin, LOW);   // otherwise turn it OFF
  }
  delay(100);                    // wait 100ms for next reading
}

______________________________________________________


PROCESSING

__________________________________________________


//import class to set up serial connection with wiring board
import processing.serial.*;
Serial port;
//button setup
color currentcolor;
RectButton rect1, rect2;
boolean locked = false;
void setup() {
  //set up window
  size(200, 200);
  color baseColor = color(#D8D6D6);
  currentcolor = baseColor;
  // Lista todos los puertos serie disponibles en el panel de salida.
   // Usted tendrá que elegir el puerto que la tarjeta
   // Conectado a partir de esta lista. El primer puerto en la lista es
   // Puerto # 0 y el tercer puerto en la lista es el puerto # 2. 
  println(Serial.list()); 
 // Abrir el puerto que la tarjeta Wiring está conectado a (en este caso 1
   // Que es el segundo puerto abierto en la matriz)
   // Asegúrese de abrir el puerto al mismo cableado de velocidad está utilizando (9600bps)
  port = new Serial(this, Serial.list()[2], 9600);
  // Definir y crear botón rectángulo # 1
  int x = 30;
  int y = 75;
  int size = 50;
  color buttoncolor = color(#14C949);
  color highlight = color(102, 51, 51); 
  rect1 = new RectButton(x, y, size, buttoncolor, highlight);
  // Definir y crear botón rectángulo # 2
  x = 120;
  y = 75; 
  size = 50;
  buttoncolor = color(#FF030B);
  highlight = color(102, 102, 102); 
  rect2 = new RectButton(x, y, size, buttoncolor, highlight);
}
void draw() {
  background(currentcolor);
  stroke(255);
  update(mouseX, mouseY);
  rect1.display();
  rect2.display();
}
void update(int x, int y) {
  if(locked == false) {
    rect1.update();
    rect2.update();
  } else {
    locked = false;
  }
  // LED encendido y apagado si los botones presionados donde
   // H = en (alta) y L = apagado (bajo)
  if(mousePressed) {
    if(rect1.pressed()) {            //ON button
      currentcolor = rect1.basecolor;
      port.write('H');
    } else if(rect2.pressed()) {    //OFF button
      currentcolor = rect2.basecolor;
      port.write('L');
    }
  }
}
class Button {
  int x, y;
  int size;
  color basecolor, highlightcolor;
  color currentcolor;
  boolean over = false;
  boolean pressed = false;   
  void update() 
  {
    if(over()) {
      currentcolor = highlightcolor;
    } else {
      currentcolor = basecolor;
    }
  }
  boolean pressed() 
  {
    if(over) {
      locked = true;
      return true;
    } else {
      locked = false;
      return false;
    }    
  }
  boolean over() 
  { 
    return true; 
  }
  void display() 
  { 
  }
}
class RectButton extends Button {
  RectButton(int ix, int iy, int isize, color icolor, color ihighlight) 
  {
    x = ix;
    y = iy;
    size = isize;
    basecolor = icolor;
    highlightcolor = ihighlight;
    currentcolor = basecolor;
  }
  boolean over() 
  {
    if( overRect(x, y, size, size) ) {
      over = true;
      return true;
    } else {
      over = false;
      return false;
    }
  }
  void display() 
  {
    stroke(255);
    fill(currentcolor);
    rect(x, y, size, size);
  }
}
boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

________________________________________________________

Informacion Relacionada:      http://playground.arduino.cc/Learning/Tutorial01

Hasta la proxima.


No hay comentarios.: