//Check if the mouse is over a rectangle and write the status to the serial port import processing.serial.*; Serial port; float side=100; void setup(){ size(400,400); noStroke(); frameRate(10); port=new Serial(this, "/dev/ttyUSB0", 9600); } void draw(){ background(255); if (mouseOverRect() == true) { //if mouse is over the square, fill(204); //change the color and port.write('a'); //send 'spin' to indicate mouse is over square println('a'); } else { //If mouse is not over square, fill(0); //change color and port.write('b'); //send 'stop' println('b'); } rectMode(CENTER); rect(width/2,height/2,side, side,6); //Draw a square, rounded corners } boolean mouseOverRect(){ //Test if mouse is over square return ((mouseX>=(width-side)/2) && (mouseX<=((width-side)/2)+side) && (mouseY>=(height-side)/2) && (mouseY<=((height-side)/2)+side)); }