dissabte, 25 de maig del 2013

Processing, 2 botons, Arduino 1 led

Crearem una finestra amb dos botons, apretant un encenem el led, apretant l'altre, l'apagem:

Codi processing:

// Activem el serial:
import processing.serial.*;
Serial port;

//configurem els colors:
color currentcolor;
//creem totes les figures que necesitem:
RectButton rect1, rect2;
//començem amb el ratolí despremut:
boolean locked = false;

//configurem tot el necessari:
void setup() {
  //configurem finestra (pixels) i color:
  size(200, 200);
  color baseColor = color(102, 102, 102);
  currentcolor = baseColor;
  
  //busquem tots els ports serials disponibles i escollirem el nostre entre []:
  println(Serial.list()); 
  port = new Serial(this, Serial.list()[1], 9600);

  //definim i creem el primer rectangle:
  int x = 30; //posició a partir del costat esquerre
  int y = 100; //alçada
  int size = 50; //costat x costat del rectangle
  color buttoncolor = color(153, 102, 102); //color base del botó
  color highlight = color(102, 51, 51); //color quan passem el ratolí per damunt
  rect1 = new RectButton(x, y, size, buttoncolor, highlight); //creem el botó1

  //definim i creem el segon rectangle:
  x = 90; //posició a partir del costat esquerre
  y = 100; //alçada
  size = 50; //costat x costat del rectangle
  buttoncolor = color(153, 153, 153); //color base del botó
  highlight = color(102, 102, 102); //color quan passem el ratolí per damunt
  rect2 = new RectButton(x, y, size, buttoncolor, highlight); //creem el botó2
}
void draw() {
  background(currentcolor); //definim el color de fons
  stroke(255);
  update(mouseX, mouseY); //actualitzarem la posició SEMPRE
  rect1.display(); //actualitzem el botó1
  rect2.display(); //actualitzem el botó2
}
void update(int x, int y) { //definim el update()
  if(locked == false) { //mirem si està el mouse apretat
    rect1.update(); //en cas afirmatiu, fem update als botons
    rect2.update();
  } else {
    locked = false; //en cas contrari, locked continua sent false
  }
  //enviarem per serial el "H" o "L" per activar el LED o desactivar-lo
  if(mousePressed) {
    if(rect1.pressed()) {            
      currentcolor = rect1.basecolor; //background del mateix color del botó
      port.write('H');  //activarem el LED
    } else if(rect2.pressed()) { //si es el segon rectangle que està apagat:   
      currentcolor = rect2.basecolor;  //canviarem el color de fons
      port.write('L'); //desactivarem el LED
    }
  }
}
class Button { //definirem la configuració interior de  botó.XXXXX
  int x, y; //necesitem varies dades
  int size;
  color basecolor, highlightcolor;
  color currentcolor;
  boolean over = false;
  boolean pressed = false;   
  void update() //primer botó.update():
  {
    if(over()){ //canviarem el color si el ratolí està sobre el botó o no:
      currentcolor = highlightcolor; //si està sobre, el botó serà de color highlight
    }else{ //sino:
      currentcolor = basecolor;  //el color serà el base 
    }
  }
  boolean pressed() //que farà si apretem o no:
  {
    if(over) { //si apretem damunt:
      locked = true; //la dada serà 1
      return true;
    } else { //sino:
      locked = false; //la dada serà 0
      return false;
    }    
  }
  boolean over() //ens retorna si està damunt o no del botó
  { 
    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() //ens retorna si està damunt o no del botó
  {
    if( overRect(x, y, size, size) ) { //amb el .over sabem de quin botó es tracta
      over = true;
      return true;
    } else {
      over = false;
      return false;
    }
  }
  void display() //mostrem el botó a la pantalla
  {
    stroke(255); //color de contorn:
    fill(currentcolor); //color de l'interior
    rect(x, y, size, size); //forma del rectangle
  }
}
boolean overRect(int x, int y, int width, int height) { //mirem si està el ratolí per damunt o no
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true; //en cas afirmatiu, retorna SI
  } else {
    return false; //en cas negatiu, retorna NO
  }
}


Codi Arduino:

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}

Cap comentari:

Publica un comentari a l'entrada