/** * Simple Write. * * Check if the mouse is over a rectangle and writes the status to the serial port. * This example works with the Wiring / Arduino program that follows below. */ import processing.serial.*; Serial myPort; // Create object from Serial class int val, lastval; // Data received from the serial port static int RED = 3; static int YELLOW = 2; static int GREEN = 1; static byte[] codes = { 0x00, 0x01, 0x02, 0x04 }; static byte[] blinkcodes = { 0x00, 0x08, 0x10, 0x20 }; int state = 0; void setup() { size(200, 500); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; println(portName); myPort = new Serial(this, portName, 9600); lastval=0; state=0; } void draw() { background(0); val = mouseOverRect(); if (val!=lastval) { // myPort.write(codes[val]); // state=state+codes[val]; // println(binary(state, 8)); myPort.write(state|codes[val]); lastval=val; } noFill(); stroke(255, 0, 0); rect(50, 25, 100, 100); // Draw a square stroke(255, 255, 0); rect(50, 150, 100, 100); // Draw a square stroke(0, 255, 0); rect(50, 275, 100, 100); // Draw a square } int mouseOverRect() { // Test if mouse is over square if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 25) && (mouseY <= 125)) return RED; // Red if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 150) && (mouseY <= 250)) return YELLOW; // Yellow if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 275) && (mouseY <= 375)) return GREEN; // Green return 0; } void mousePressed() { // set state to whichever area was currently clicked val=mouseOverRect(); if(val==0) { state=0; } else { state^=codes[val]; } println(binary(state, 8)); myPort.write(state); lastval=val; }