import processing.serial.*; Serial port; // Create an object from Serial class String val; // Data received from the serial port boolean firstContact = false; boolean button = false; int x = 150; int y = 150; int w = 50; int h = 50; void setup() { port = new Serial(this, "/dev/cu.usbmodem1101", 115200); port.bufferUntil('\n'); size(360, 360); background(0); } void draw() { if (button) { background(255); stroke(0); } else { background(0); stroke(255); } fill(175); rect(x,y,w,h); } void serialEvent( Serial port) { val = port.readStringUntil('\n'); if (val != null) { val = trim(val); println(val); if (firstContact == false) { if (val.equals("A")) { port.clear(); firstContact = true; port.write("A"); println("contact"); } } else { println(val); if (mousePressed == true) { port.write('1'); //send a 1 if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { button = !button; } println("1"); } port.write("A"); } } } //<>//